-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from xsnippet/alembic
Use alembic for schema migrations
- Loading branch information
Showing
8 changed files
with
257 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
/target | ||
__pycache__ |
This file contains 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,36 @@ | ||
[alembic] | ||
script_location = src/storage/sql/migrations/ | ||
|
||
[loggers] | ||
keys = root,sqlalchemy,alembic | ||
|
||
[handlers] | ||
keys = console | ||
|
||
[formatters] | ||
keys = generic | ||
|
||
[logger_root] | ||
level = WARN | ||
handlers = console | ||
qualname = | ||
|
||
[logger_sqlalchemy] | ||
level = INFO | ||
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 |
3 changes: 0 additions & 3 deletions
3
src/storage/sql/migrations/2020-07-25-065258_create_snippets/down.sql
This file was deleted.
Oops, something went wrong.
59 changes: 0 additions & 59 deletions
59
src/storage/sql/migrations/2020-07-25-065258_create_snippets/up.sql
This file was deleted.
Oops, something went wrong.
This file contains 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,53 @@ | ||
import logging.config | ||
import os | ||
|
||
from alembic import context | ||
from sqlalchemy import create_engine, pool | ||
|
||
|
||
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. | ||
""" | ||
|
||
context.configure( | ||
url=os.environ['DATABASE_URL'], | ||
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( | ||
os.environ['DATABASE_URL'], | ||
poolclass=pool.NullPool, | ||
) | ||
with connectable.connect() as connection: | ||
context.configure(connection=connection) | ||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
|
||
logging.config.fileConfig(context.config.config_file_name) | ||
if context.is_offline_mode(): | ||
run_migrations_offline() | ||
else: | ||
run_migrations_online() |
This file contains 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"} |
107 changes: 107 additions & 0 deletions
107
src/storage/sql/migrations/versions/1bf4e4e7b24f_initial_schema.py
This file contains 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,107 @@ | ||
"""Initial schema | ||
Revision ID: 1bf4e4e7b24f | ||
Revises: | ||
Create Date: 2020-07-26 08:46:28.752972 | ||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
revision = '1bf4e4e7b24f' | ||
down_revision = None | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
metadata = sa.MetaData() | ||
|
||
sa.Table( | ||
'snippets', metadata, | ||
|
||
# an internal autoincrementing identifier; only used in foreign keys | ||
sa.Column('id', sa.Integer, primary_key=True), | ||
# a short unique snippet identifier visible to users | ||
sa.Column('slug', sa.String(32), nullable=False), | ||
|
||
sa.Column('title', sa.Text), | ||
sa.Column('syntax', sa.Text), | ||
sa.Column('created_at', sa.DateTime(timezone=True), | ||
server_default=sa.func.now(), nullable=False), | ||
sa.Column('updated_at', sa.DateTime(timezone=True), | ||
server_default=sa.func.now(), nullable=False), | ||
|
||
# slugs must be unique (this will also automatically create a unique index) | ||
sa.UniqueConstraint('slug', name='uq_slug'), | ||
|
||
# will be used for pagination; slug guarantees uniqueness of the sorting key | ||
sa.Index('snippets_created_at_slug', 'created_at', 'slug'), | ||
sa.Index('snippets_updated_at_slug', 'updated_at', 'slug'), | ||
) | ||
|
||
sa.Table( | ||
'changesets', metadata, | ||
|
||
# an internal autoincrementing identifier; only used in foreign keys | ||
sa.Column('id', sa.Integer, primary_key=True), | ||
sa.Column('snippet_id', sa.Integer, nullable=False), | ||
|
||
# numeric index used to determine the ordering of changesets for a given snippet | ||
sa.Column('version', sa.Integer, | ||
server_default=sa.text('0'), nullable=False), | ||
sa.Column('content', sa.Text, nullable=False), | ||
sa.Column('created_at', sa.DateTime(timezone=True), | ||
server_default=sa.func.now(), nullable=False), | ||
sa.Column('updated_at', sa.DateTime(timezone=True), | ||
server_default=sa.func.now(), nullable=False), | ||
|
||
# there can be multiple changesets per snippet; changesets should be | ||
# deleted when the parent snippet is deleted | ||
sa.ForeignKeyConstraint(['snippet_id'], ['snippets.id'], | ||
ondelete='CASCADE', name='fk_snippet', | ||
use_alter=False), | ||
# each changeset is supposed to have a unique version number | ||
sa.UniqueConstraint('snippet_id', 'version', | ||
name='uq_version'), | ||
# sanity check: do not allow empty changesets | ||
sa.CheckConstraint('LENGTH(content) > 0', | ||
name='check_not_empty'), | ||
# sanity check: version numbers are non-negative integers | ||
sa.CheckConstraint('version >= 0', | ||
name='check_non_negative_version') | ||
|
||
) | ||
|
||
# tags could have been associated with snippets as M:M via an auxiliary table, | ||
# but Diesel only supports child-parent associations, so let's do that instead | ||
sa.Table( | ||
'tags', metadata, | ||
|
||
# an internal autoincrementing identifier; only used in foreign keys | ||
sa.Column('id', sa.Integer, primary_key=True), | ||
sa.Column('snippet_id', sa.Integer, nullable=False), | ||
|
||
sa.Column('value', sa.Text, nullable=False), | ||
|
||
# there can be multiple tags per snippet; tags should be deleted when | ||
# the parent snippet is deleted | ||
sa.ForeignKeyConstraint(['snippet_id'], ['snippets.id'], | ||
ondelete='CASCADE', | ||
name='fk_snippet'), | ||
|
||
# do not allow to abuse the tags for storing too much data | ||
sa.CheckConstraint('LENGTH(value) < 128', name='check_length'), | ||
# do not allow repeated tags per snippet | ||
sa.UniqueConstraint('snippet_id', 'value', name='uq_snippet_tag'), | ||
) | ||
|
||
metadata.create_all(op.get_bind()) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table('tags') | ||
op.drop_table('changesets') | ||
op.drop_table('snippets') |