updates database design for mariadb
This commit is contained in:
1
database/migrations/README
Normal file
1
database/migrations/README
Normal file
@ -0,0 +1 @@
|
||||
Generic single-database configuration.
|
45
database/migrations/alembic.ini
Normal file
45
database/migrations/alembic.ini
Normal file
@ -0,0 +1,45 @@
|
||||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# template used to generate migration files
|
||||
# file_template = %%(rev)s_%%(slug)s
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
96
database/migrations/env.py
Normal file
96
database/migrations/env.py
Normal file
@ -0,0 +1,96 @@
|
||||
from __future__ import with_statement
|
||||
|
||||
import logging
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
|
||||
from alembic import context
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
logger = logging.getLogger('alembic.env')
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
from flask import current_app
|
||||
config.set_main_option(
|
||||
'sqlalchemy.url',
|
||||
str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
|
||||
target_metadata = current_app.extensions['migrate'].db.metadata
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url, target_metadata=target_metadata, literal_binds=True
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online():
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
|
||||
# this callback is used to prevent an auto-migration from being generated
|
||||
# when there are no changes to the schema
|
||||
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
|
||||
def process_revision_directives(context, revision, directives):
|
||||
if getattr(config.cmd_opts, 'autogenerate', False):
|
||||
script = directives[0]
|
||||
if script.upgrade_ops.is_empty():
|
||||
directives[:] = []
|
||||
logger.info('No changes in schema detected.')
|
||||
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section),
|
||||
prefix='sqlalchemy.',
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
process_revision_directives=process_revision_directives,
|
||||
**current_app.extensions['migrate'].configure_args
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
24
database/migrations/script.py.mako
Normal file
24
database/migrations/script.py.mako
Normal file
@ -0,0 +1,24 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade():
|
||||
${downgrades if downgrades else "pass"}
|
34
database/migrations/versions/1e8205594ac1_.py
Normal file
34
database/migrations/versions/1e8205594ac1_.py
Normal file
@ -0,0 +1,34 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 1e8205594ac1
|
||||
Revises: aeab4aff199b
|
||||
Create Date: 2020-05-27 16:57:54.384047
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '1e8205594ac1'
|
||||
down_revision = 'aeab4aff199b'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('user', sa.Column('userid', sa.String(length=64), nullable=True))
|
||||
op.create_index(op.f('ix_user_userid'), 'user', ['userid'], unique=True)
|
||||
op.drop_index('ix_user_username', table_name='user')
|
||||
op.create_index(op.f('ix_user_username'), 'user', ['username'], unique=False)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index(op.f('ix_user_username'), table_name='user')
|
||||
op.create_index('ix_user_username', 'user', ['username'], unique=True)
|
||||
op.drop_index(op.f('ix_user_userid'), table_name='user')
|
||||
op.drop_column('user', 'userid')
|
||||
# ### end Alembic commands ###
|
23
database/migrations/versions/3b829a27bc337.py
Normal file
23
database/migrations/versions/3b829a27bc337.py
Normal file
@ -0,0 +1,23 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 3b829a27bc337
|
||||
Revises: 1e8205594ac1
|
||||
Create Date: 2020-05-27 19:30:54.384047
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '3b829a27bc337'
|
||||
down_revision = '1e8205594ac1'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.alter_column('User', 'google_credentials', existing_type=sa.Relationship(), new_column_name='google_token')
|
||||
|
||||
def downgrade():
|
||||
op.alter_column('User', 'google_token', existing_type=sa.Relationship(), new_column_name='google_credentials')
|
72
database/migrations/versions/aeab4aff199b_.py
Normal file
72
database/migrations/versions/aeab4aff199b_.py
Normal file
@ -0,0 +1,72 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: aeab4aff199b
|
||||
Revises:
|
||||
Create Date: 2020-05-27 15:23:20.611265
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'aeab4aff199b'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('user',
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column('username', sa.String(length=64), nullable=True),
|
||||
sa.Column('email', sa.String(length=120), nullable=True),
|
||||
sa.Column('profile_pic', sa.String(length=256), nullable=True),
|
||||
sa.Column('password_hash', sa.String(length=128), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_user_email'), 'user', ['email'], unique=True)
|
||||
op.create_index(op.f('ix_user_username'), 'user', ['username'], unique=True)
|
||||
op.create_table('calendar',
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('calendar_id', sa.String(length=256), nullable=False),
|
||||
sa.Column('name', sa.String(length=256), nullable=True),
|
||||
sa.Column('toggle', sa.String(length=8), nullable=True),
|
||||
sa.Column('color', sa.String(length=16), nullable=True),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
||||
sa.PrimaryKeyConstraint('id', 'calendar_id')
|
||||
)
|
||||
op.create_index(op.f('ix_calendar_name'), 'calendar', ['name'], unique=False)
|
||||
op.create_index(op.f('ix_calendar_user_id'), 'calendar', ['user_id'], unique=False)
|
||||
op.create_table('device',
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=True),
|
||||
sa.Column('deviceName', sa.String(length=64), nullable=True),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('deviceName')
|
||||
)
|
||||
op.create_table('google_token',
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=True),
|
||||
sa.Column('token', sa.String(length=256), nullable=True),
|
||||
sa.Column('refresh_token', sa.String(length=256), nullable=True),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('google_token')
|
||||
op.drop_table('device')
|
||||
op.drop_index(op.f('ix_calendar_user_id'), table_name='calendar')
|
||||
op.drop_index(op.f('ix_calendar_name'), table_name='calendar')
|
||||
op.drop_table('calendar')
|
||||
op.drop_index(op.f('ix_user_username'), table_name='user')
|
||||
op.drop_index(op.f('ix_user_email'), table_name='user')
|
||||
op.drop_table('user')
|
||||
# ### end Alembic commands ###
|
@ -8,13 +8,15 @@ def load_user(id):
|
||||
return User.query.get(id)
|
||||
|
||||
class User(UserMixin, db.Model):
|
||||
id = db.Column(db.String(64), primary_key=True)
|
||||
username = db.Column(db.String(64), index=True, unique=True)
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
userid = db.Column(db.String(64), index=True, unique=True)
|
||||
username = db.Column(db.String(64), index=True)
|
||||
email = db.Column(db.String(120), index=True, unique=True)
|
||||
profile_pic = db.Column(db.String(256))
|
||||
password_hash = db.Column(db.String(128))
|
||||
calendarJson = db.Column(db.String)
|
||||
google_credentials = db.Column(db.String)
|
||||
google_token = db.relationship('GoogleToken', uselist=False, backref = 'user')
|
||||
calendars = db.relationship('Calendar', backref='user', lazy=True)
|
||||
devices = db.relationship('Device', backref='user')
|
||||
|
||||
def __repr__(self):
|
||||
return '<User {}>'.format(self.username)
|
||||
@ -25,53 +27,11 @@ class User(UserMixin, db.Model):
|
||||
def checkPassword(self, password):
|
||||
return check_password_hash(self.password_hash, password)
|
||||
|
||||
def setJson(self, jsonObject):
|
||||
self.calendarJson = json.dumps(jsonObject)
|
||||
db.session.commit()
|
||||
|
||||
def getJson(self):
|
||||
return json.loads(self.calendarJson)
|
||||
|
||||
def setGoogleCredentials(self, credentials):
|
||||
self.google_credentials = json.dumps(credentials)
|
||||
db.session.commit()
|
||||
def updateCalendar(self, calendar_id, toggle=None, color=None):
|
||||
|
||||
def getGoogleCredentials(self):
|
||||
|
||||
if self.google_credentials is None:
|
||||
print("no credentials", flush=True)
|
||||
return None
|
||||
return json.loads(self.google_credentials)
|
||||
|
||||
class Calendar(db.Model):
|
||||
usr_id = db.Column(db.String(21), index=True)
|
||||
calendar_id = db.Column(db.String(256), primary_key=True)
|
||||
name = db.Column(db.String(256), index=True)
|
||||
toggle = db.Column(db.String(8))
|
||||
color = db.Column(db.String(16))
|
||||
|
||||
def getCalendars(self, user_id):
|
||||
calendars = self.query.filter(Calendar.usr_id==user_id)
|
||||
|
||||
return calendars
|
||||
|
||||
def getCalendar(self, user_id, calendar_id):
|
||||
calendars = self.query.filter(self.usr_id==user_id, self.calendar_id==calendar_id)
|
||||
|
||||
calendar = None
|
||||
for c in calendars:
|
||||
calendar = c
|
||||
|
||||
if not calendar:
|
||||
return None
|
||||
|
||||
return calendar
|
||||
|
||||
@staticmethod
|
||||
def updateCalendar(user_id, calendar_id, toggle=None, color=None):
|
||||
|
||||
calendar = Calendar.query.filter(Calendar.usr_id==user_id, Calendar.calendar_id==calendar_id).first()
|
||||
|
||||
for calendar in self.calendars:
|
||||
if calendar.calendar_id == calendar_id:
|
||||
break
|
||||
|
||||
print("updating", flush=True)
|
||||
if(toggle != None):
|
||||
@ -83,12 +43,28 @@ class Calendar(db.Model):
|
||||
calendar.color = color
|
||||
db.session.commit()
|
||||
|
||||
def create(self, user_id, calendar_id, name, color, toggle = 'True'):
|
||||
newcal = Calendar(usr_id=user_id, calendar_id=calendar_id, name=name, toggle=toggle, color=color)
|
||||
|
||||
db.session.add(newcal)
|
||||
db.session.commit()
|
||||
|
||||
def hasCalendar(self, calendar_id):
|
||||
for calendar in self.calendars:
|
||||
if calendar.calendar_id == calendar_id:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
class GoogleToken(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||
token = db.Column(db.String(256))
|
||||
refresh_token = db.Column(db.String(256))
|
||||
|
||||
class Device(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
deviceId = db.Column(db.String(128), index=True)
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||
deviceName = db.Column(db.String(64), unique=True)
|
||||
|
||||
class Calendar(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True, nullable=False)
|
||||
calendar_id = db.Column(db.String(256), primary_key=True)
|
||||
name = db.Column(db.String(256), index=True)
|
||||
toggle = db.Column(db.String(8))
|
||||
color = db.Column(db.String(16))
|
||||
|
Reference in New Issue
Block a user