diff --git a/src/server/app/main/settings.py b/src/server/app/main/settings.py index 8a0a05b..19474b7 100644 --- a/src/server/app/main/settings.py +++ b/src/server/app/main/settings.py @@ -50,7 +50,7 @@ class DevelopmentConfig(Config): # uncomment the line below to use postgres # SQLALCHEMY_DATABASE_URI = postgres_local_base # uncomment the line below to use mysql - # SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://:@:/' + SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:Anuj@1996@localhost/masai_oss' DEBUG = True # SQLALCHEMY_DATABASE_URI = "sqlite:///" + os.path.join( diff --git a/src/server/migrations/README b/src/server/migrations/README new file mode 100644 index 0000000..98e4f9c --- /dev/null +++ b/src/server/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/src/server/migrations/alembic.ini b/src/server/migrations/alembic.ini new file mode 100644 index 0000000..f8ed480 --- /dev/null +++ b/src/server/migrations/alembic.ini @@ -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 diff --git a/src/server/migrations/env.py b/src/server/migrations/env.py new file mode 100644 index 0000000..79b8174 --- /dev/null +++ b/src/server/migrations/env.py @@ -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', current_app.config.get( + 'SQLALCHEMY_DATABASE_URI').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() diff --git a/src/server/migrations/script.py.mako b/src/server/migrations/script.py.mako new file mode 100644 index 0000000..2c01563 --- /dev/null +++ b/src/server/migrations/script.py.mako @@ -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"} diff --git a/src/server/migrations/versions/640c6be52cef_.py b/src/server/migrations/versions/640c6be52cef_.py new file mode 100644 index 0000000..bced50d --- /dev/null +++ b/src/server/migrations/versions/640c6be52cef_.py @@ -0,0 +1,95 @@ +"""empty message + +Revision ID: 640c6be52cef +Revises: +Create Date: 2020-01-02 22:37:39.067828 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '640c6be52cef' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('child_2', + sa.Column('id', sa.Integer(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('left', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=80), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('parent_1', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('parent_3', + sa.Column('id', sa.Integer(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('right', + sa.Column('id', sa.Integer(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('users', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('public_id', sa.String(length=50), nullable=True), + sa.Column('username', sa.String(length=30), nullable=False), + sa.Column('email', sa.String(length=100), nullable=False), + sa.Column('name', sa.String(length=150), nullable=False), + sa.Column('admin', sa.Boolean(), nullable=False), + sa.Column('password_hash', sa.String(length=100), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('email'), + sa.UniqueConstraint('public_id'), + sa.UniqueConstraint('username') + ) + op.create_table('association', + sa.Column('left_id', sa.Integer(), nullable=True), + sa.Column('right_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['left_id'], ['left.id'], ), + sa.ForeignKeyConstraint(['right_id'], ['right.id'], ) + ) + op.create_table('child_1', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('parent_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['parent_id'], ['parent_1.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('child_3', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('parent_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['parent_id'], ['parent_3.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('parent_2', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('child_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['child_id'], ['child_2.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('parent_2') + op.drop_table('child_3') + op.drop_table('child_1') + op.drop_table('association') + op.drop_table('users') + op.drop_table('right') + op.drop_table('parent_3') + op.drop_table('parent_1') + op.drop_table('left') + op.drop_table('child_2') + # ### end Alembic commands ###