-
Notifications
You must be signed in to change notification settings - Fork 52
feat: add alembic configuration #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yammesicka
merged 11 commits into
PythonFreeCourse:develop
from
ellenc345:feature/ellen/add-alembic
Feb 22, 2021
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f1cc12b
feat: add alembic configuration
ellenc345 b3bbb06
fix: test failed basuse lint i guess
ellenc345 22180af
fix: why test failesss
ellenc345 a17bcb2
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
ellenc345 f662d54
feat: add date to alembic template
ellenc345 568844c
fix import due to code update on upstream
ellenc345 9865cbc
feat: use our logger
ellenc345 e842199
fix : move module into database folder
ellenc345 b772dbb
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
ellenc345 14ee318
fix: reorder imports
ellenc345 e6c4375
fix: add comma
ellenc345 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
yammesicka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ### |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move all the alembic directory structure into
app/database/alembic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If possible, move the configurations to
config.py.example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please alborate why do that?
this file is runing when you call the alembic into action. this is the standart config that is well documented and it is clear to maintain it. if you whant to override things you can alwayys do it in alembic/env.py file.
but as I see it i wouldn't mix that config with the rest of the custom code configuration.
Ill be glad to hear your opinion about that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to avoid making the user edit multiple configuration files, but now I see that the user shouldn't really touch anything here, so I guess it's cool