diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 00000000..16e9fab7 --- /dev/null +++ b/alembic.ini @@ -0,0 +1,86 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +# script_location = alembic +script_location = app\\database\\alembic + +# template used to generate migration files +file_template = %%(year)d-%%(month).2d-%%(day).2d_%%(rev)s_%%(slug)s + +# timezone to use when rendering the date +# within the migration file as well as the filename. +# string value is passed to dateutil.tz.gettz() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; this defaults +# to alembic/versions. When using multiple version +# directories, initial revisions must be specified with --version-path +# version_locations = %(here)s/bar %(here)s/bat alembic/versions + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +# sqlalchemy.url = driver://user:pass@localhost/dbname + + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks=black +# black.type=console_scripts +# black.entrypoint=black +# black.options=-l 79 + +# 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/app/database/alembic/README b/app/database/alembic/README new file mode 100644 index 00000000..7239903d --- /dev/null +++ b/app/database/alembic/README @@ -0,0 +1,14 @@ +Generic single-database configuration. + +# following command will run all migration script and bring it to latest version +alembic upgrade head + +# If we like to incrementally upgrade and check for some errors +alembic upgrade +1 + +# To undo last migration +alembic downgrade -1 + +# To get more information +alembic current +alembic history - verbose \ No newline at end of file diff --git a/app/database/alembic/__init__.py b/app/database/alembic/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/database/alembic/env.py b/app/database/alembic/env.py new file mode 100644 index 00000000..bca6fab9 --- /dev/null +++ b/app/database/alembic/env.py @@ -0,0 +1,79 @@ +from logging.config import fileConfig +import os + +from alembic import context +from sqlalchemy import create_engine + +from app import config as app_config +from app.database.models import Base + + +SQLALCHEMY_DATABASE_URL = os.getenv( + "DATABASE_CONNECTION_STRING", app_config.DEVELOPMENT_DATABASE_STRING) + +# 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, disable_existing_loggers=False) + +# add your model's MetaData object here +# for 'autogenerate' support +target_metadata = Base.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 = SQLALCHEMY_DATABASE_URL + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + 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. + + """ + + connectable = create_engine(SQLALCHEMY_DATABASE_URL) + + with connectable.connect() as connection: + context.configure( + connection=connection, target_metadata=target_metadata, + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/app/database/alembic/script.py.mako b/app/database/alembic/script.py.mako new file mode 100644 index 00000000..2c015630 --- /dev/null +++ b/app/database/alembic/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/app/database/alembic/versions/91b42971b0df_.py b/app/database/alembic/versions/91b42971b0df_.py new file mode 100644 index 00000000..18a5d836 --- /dev/null +++ b/app/database/alembic/versions/91b42971b0df_.py @@ -0,0 +1,135 @@ +"""empty message + +Revision ID: 91b42971b0df +Revises: +Create Date: 2021-02-06 16:15:07.861957 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import sqlite + +# revision identifiers, used by Alembic. +revision = '91b42971b0df' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index('ix_categories_id', table_name='categories') + op.drop_table('categories') + op.drop_index('ix_invitations_id', table_name='invitations') + op.drop_table('invitations') + op.drop_index('ix_users_id', table_name='users') + op.drop_table('users') + op.drop_index('ix_quotes_id', table_name='quotes') + op.drop_table('quotes') + op.drop_index('ix_wikipedia_events_id', table_name='wikipedia_events') + op.drop_table('wikipedia_events') + op.drop_index('ix_zodiac-signs_id', table_name='zodiac-signs') + op.drop_table('zodiac-signs') + op.drop_index('ix_events_id', table_name='events') + op.drop_table('events') + op.drop_index('ix_user_event_id', table_name='user_event') + op.drop_table('user_event') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('user_event', + sa.Column('id', sa.INTEGER(), nullable=False), + sa.Column('user_id', sa.INTEGER(), nullable=True), + sa.Column('event_id', sa.INTEGER(), nullable=True), + sa.ForeignKeyConstraint(['event_id'], ['events.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('ix_user_event_id', 'user_event', ['id'], unique=False) + op.create_table('events', + sa.Column('id', sa.INTEGER(), nullable=False), + sa.Column('title', sa.VARCHAR(), nullable=False), + sa.Column('start', sa.DATETIME(), nullable=False), + sa.Column('end', sa.DATETIME(), nullable=False), + sa.Column('content', sa.VARCHAR(), nullable=True), + sa.Column('location', sa.VARCHAR(), nullable=True), + sa.Column('color', sa.VARCHAR(), nullable=True), + sa.Column('owner_id', sa.INTEGER(), nullable=True), + sa.Column('category_id', sa.INTEGER(), nullable=True), + sa.ForeignKeyConstraint( + ['category_id'], ['categories.id'], ), + sa.ForeignKeyConstraint(['owner_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('ix_events_id', 'events', ['id'], unique=False) + op.create_table('zodiac-signs', + sa.Column('id', sa.INTEGER(), nullable=False), + sa.Column('name', sa.VARCHAR(), nullable=False), + sa.Column('start_month', sa.INTEGER(), nullable=False), + sa.Column('start_day_in_month', + sa.INTEGER(), nullable=False), + sa.Column('end_month', sa.INTEGER(), nullable=False), + sa.Column('end_day_in_month', + sa.INTEGER(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('ix_zodiac-signs_id', 'zodiac-signs', ['id'], unique=False) + op.create_table('wikipedia_events', + sa.Column('id', sa.INTEGER(), nullable=False), + sa.Column('date_', sa.VARCHAR(), nullable=False), + sa.Column('wikipedia', sa.VARCHAR(), nullable=False), + sa.Column('events', sqlite.JSON(), nullable=True), + sa.Column('date_inserted', sa.DATETIME(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('ix_wikipedia_events_id', + 'wikipedia_events', ['id'], unique=False) + op.create_table('quotes', + sa.Column('id', sa.INTEGER(), nullable=False), + sa.Column('text', sa.VARCHAR(), nullable=False), + sa.Column('author', sa.VARCHAR(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('ix_quotes_id', 'quotes', ['id'], unique=False) + op.create_table('users', + sa.Column('id', sa.INTEGER(), nullable=False), + sa.Column('username', sa.VARCHAR(), nullable=False), + sa.Column('email', sa.VARCHAR(), nullable=False), + sa.Column('password', sa.VARCHAR(), nullable=False), + sa.Column('full_name', sa.VARCHAR(), nullable=True), + sa.Column('language', sa.VARCHAR(), nullable=True), + sa.Column('description', sa.VARCHAR(), nullable=True), + sa.Column('avatar', sa.VARCHAR(), nullable=True), + sa.Column('telegram_id', sa.VARCHAR(), nullable=True), + sa.Column('is_active', sa.BOOLEAN(), nullable=True), + sa.CheckConstraint('is_active IN (0, 1)'), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('email'), + sa.UniqueConstraint('telegram_id'), + sa.UniqueConstraint('username') + ) + op.create_index('ix_users_id', 'users', ['id'], unique=False) + op.create_table('invitations', + sa.Column('id', sa.INTEGER(), nullable=False), + sa.Column('status', sa.VARCHAR(), nullable=False), + sa.Column('recipient_id', sa.INTEGER(), nullable=True), + sa.Column('event_id', sa.INTEGER(), nullable=True), + sa.Column('creation', sa.DATETIME(), nullable=True), + sa.ForeignKeyConstraint(['event_id'], ['events.id'], ), + sa.ForeignKeyConstraint(['recipient_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('ix_invitations_id', 'invitations', ['id'], unique=False) + op.create_table('categories', + sa.Column('id', sa.INTEGER(), nullable=False), + sa.Column('name', sa.VARCHAR(), nullable=False), + sa.Column('color', sa.VARCHAR(), nullable=False), + sa.Column('user_id', sa.INTEGER(), nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('user_id', 'name', 'color') + ) + op.create_index('ix_categories_id', 'categories', ['id'], unique=False) + # ### end Alembic commands ###