Skip to content

Commit 59a206a

Browse files
committed
Format Python migrations code
1 parent 5230b36 commit 59a206a

9 files changed

+106
-88
lines changed

migrations/env.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,31 @@
1212
# Interpret the config file for Python logging.
1313
# This line sets up loggers basically.
1414
fileConfig(config.config_file_name)
15-
logger = logging.getLogger('alembic.env')
15+
logger = logging.getLogger("alembic.env")
1616

1717

1818
def get_engine():
1919
try:
2020
# this works with Flask-SQLAlchemy<3 and Alchemical
21-
return current_app.extensions['migrate'].db.get_engine()
21+
return current_app.extensions["migrate"].db.get_engine()
2222
except (TypeError, AttributeError):
2323
# this works with Flask-SQLAlchemy>=3
24-
return current_app.extensions['migrate'].db.engine
24+
return current_app.extensions["migrate"].db.engine
2525

2626

2727
def get_engine_url():
2828
try:
29-
return get_engine().url.render_as_string(hide_password=False).replace(
30-
'%', '%%')
29+
return get_engine().url.render_as_string(hide_password=False).replace("%", "%%")
3130
except AttributeError:
32-
return str(get_engine().url).replace('%', '%%')
31+
return str(get_engine().url).replace("%", "%%")
3332

3433

3534
# add your model's MetaData object here
3635
# for 'autogenerate' support
3736
# from myapp import mymodel
3837
# target_metadata = mymodel.Base.metadata
39-
config.set_main_option('sqlalchemy.url', get_engine_url())
40-
target_db = current_app.extensions['migrate'].db
38+
config.set_main_option("sqlalchemy.url", get_engine_url())
39+
target_db = current_app.extensions["migrate"].db
4140

4241
# other values from the config, defined by the needs of env.py,
4342
# can be acquired:
@@ -46,7 +45,7 @@ def get_engine_url():
4645

4746

4847
def get_metadata():
49-
if hasattr(target_db, 'metadatas'):
48+
if hasattr(target_db, "metadatas"):
5049
return target_db.metadatas[None]
5150
return target_db.metadata
5251

@@ -64,9 +63,8 @@ def run_migrations_offline():
6463
6564
"""
6665
url = config.get_main_option("sqlalchemy.url")
67-
context.configure(
68-
url=url, target_metadata=get_metadata(), literal_binds=True
69-
)
66+
context.configure(url=url, target_metadata=get_metadata(),
67+
literal_binds=True)
7068

7169
with context.begin_transaction():
7270
context.run_migrations()
@@ -84,23 +82,21 @@ def run_migrations_online():
8482
# when there are no changes to the schema
8583
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
8684
def process_revision_directives(context, revision, directives):
87-
if getattr(config.cmd_opts, 'autogenerate', False):
85+
if getattr(config.cmd_opts, "autogenerate", False):
8886
script = directives[0]
8987
if script.upgrade_ops.is_empty():
9088
directives[:] = []
91-
logger.info('No changes in schema detected.')
89+
logger.info("No changes in schema detected.")
9290

93-
conf_args = current_app.extensions['migrate'].configure_args
91+
conf_args = current_app.extensions["migrate"].configure_args
9492
if conf_args.get("process_revision_directives") is None:
9593
conf_args["process_revision_directives"] = process_revision_directives
9694

9795
connectable = get_engine()
9896

9997
with connectable.connect() as connection:
10098
context.configure(
101-
connection=connection,
102-
target_metadata=get_metadata(),
103-
**conf_args
99+
connection=connection, target_metadata=get_metadata(), **conf_args
104100
)
105101

106102
with context.begin_transaction():

migrations/versions/5da7b534611d_add_number_of_tasks_completed_in_a_day_.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@
55
Create Date: 2025-01-04 18:52:22.404495
66
77
"""
8+
89
from alembic import op
910
import sqlalchemy as sa
1011

1112

1213
# revision identifiers, used by Alembic.
13-
revision = '5da7b534611d'
14-
down_revision = 'c0fcd66c3df6'
14+
revision = "5da7b534611d"
15+
down_revision = "c0fcd66c3df6"
1516
branch_labels = None
1617
depends_on = None
1718

1819

1920
def upgrade():
2021
# ### commands auto generated by Alembic - please adjust! ###
21-
with op.batch_alter_table('user', schema=None) as batch_op:
22-
batch_op.add_column(sa.Column('daily_tasks_completed', sa.Integer(), nullable=False))
22+
with op.batch_alter_table("user", schema=None) as batch_op:
23+
batch_op.add_column(
24+
sa.Column("daily_tasks_completed", sa.Integer(), nullable=False)
25+
)
2326

2427
# ### end Alembic commands ###
2528

2629

2730
def downgrade():
2831
# ### commands auto generated by Alembic - please adjust! ###
29-
with op.batch_alter_table('user', schema=None) as batch_op:
30-
batch_op.drop_column('daily_tasks_completed')
32+
with op.batch_alter_table("user", schema=None) as batch_op:
33+
batch_op.drop_column("daily_tasks_completed")
3134

3235
# ### end Alembic commands ###

migrations/versions/5e8982aea1ef_add_number_of_tasks_completed_column.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,30 @@
55
Create Date: 2025-01-04 18:50:49.963825
66
77
"""
8+
89
from alembic import op
910
import sqlalchemy as sa
1011

1112

1213
# revision identifiers, used by Alembic.
13-
revision = '5e8982aea1ef'
14-
down_revision = '6b7325da9331'
14+
revision = "5e8982aea1ef"
15+
down_revision = "6b7325da9331"
1516
branch_labels = None
1617
depends_on = None
1718

1819

1920
def upgrade():
2021
# ### commands auto generated by Alembic - please adjust! ###
21-
with op.batch_alter_table('user', schema=None) as batch_op:
22-
batch_op.add_column(sa.Column('tasks_completed', sa.Integer(), nullable=False))
22+
with op.batch_alter_table("user", schema=None) as batch_op:
23+
batch_op.add_column(sa.Column("tasks_completed",
24+
sa.Integer(), nullable=False))
2325

2426
# ### end Alembic commands ###
2527

2628

2729
def downgrade():
2830
# ### commands auto generated by Alembic - please adjust! ###
29-
with op.batch_alter_table('user', schema=None) as batch_op:
30-
batch_op.drop_column('tasks_completed')
31+
with op.batch_alter_table("user", schema=None) as batch_op:
32+
batch_op.drop_column("tasks_completed")
3133

3234
# ### end Alembic commands ###

migrations/versions/6b7325da9331_add_task_completed_column.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,30 @@
55
Create Date: 2025-01-04 18:49:58.913626
66
77
"""
8+
89
from alembic import op
910
import sqlalchemy as sa
1011

1112

1213
# revision identifiers, used by Alembic.
13-
revision = '6b7325da9331'
14-
down_revision = '8f05b938c89f'
14+
revision = "6b7325da9331"
15+
down_revision = "8f05b938c89f"
1516
branch_labels = None
1617
depends_on = None
1718

1819

1920
def upgrade():
2021
# ### commands auto generated by Alembic - please adjust! ###
21-
with op.batch_alter_table('task', schema=None) as batch_op:
22-
batch_op.add_column(sa.Column('completed', sa.Boolean(), nullable=False))
22+
with op.batch_alter_table("task", schema=None) as batch_op:
23+
batch_op.add_column(
24+
sa.Column("completed", sa.Boolean(), nullable=False))
2325

2426
# ### end Alembic commands ###
2527

2628

2729
def downgrade():
2830
# ### commands auto generated by Alembic - please adjust! ###
29-
with op.batch_alter_table('task', schema=None) as batch_op:
30-
batch_op.drop_column('completed')
31+
with op.batch_alter_table("task", schema=None) as batch_op:
32+
batch_op.drop_column("completed")
3133

3234
# ### end Alembic commands ###

migrations/versions/868a910249be_create_user_and_task_tables.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,55 @@
55
Create Date: 2025-01-04 18:44:02.793639
66
77
"""
8+
89
from alembic import op
910
import sqlalchemy as sa
1011

1112

1213
# revision identifiers, used by Alembic.
13-
revision = '868a910249be'
14+
revision = "868a910249be"
1415
down_revision = None
1516
branch_labels = None
1617
depends_on = None
1718

1819

1920
def upgrade():
2021
# ### commands auto generated by Alembic - please adjust! ###
21-
op.create_table('user',
22-
sa.Column('id', sa.Integer(), nullable=False),
23-
sa.Column('username', sa.String(length=80), nullable=False),
24-
sa.Column('xp', sa.Float(), nullable=False),
25-
sa.Column('xp_required', sa.Float(), nullable=False),
26-
sa.Column('total_xp', sa.Float(), nullable=False),
27-
sa.Column('level', sa.Integer(), nullable=False),
28-
sa.PrimaryKeyConstraint('id'),
29-
sa.UniqueConstraint('id'),
30-
sa.UniqueConstraint('username')
22+
op.create_table(
23+
"user",
24+
sa.Column("id", sa.Integer(), nullable=False),
25+
sa.Column("username", sa.String(length=80), nullable=False),
26+
sa.Column("xp", sa.Float(), nullable=False),
27+
sa.Column("xp_required", sa.Float(), nullable=False),
28+
sa.Column("total_xp", sa.Float(), nullable=False),
29+
sa.Column("level", sa.Integer(), nullable=False),
30+
sa.PrimaryKeyConstraint("id"),
31+
sa.UniqueConstraint("id"),
32+
sa.UniqueConstraint("username"),
3133
)
32-
op.create_table('task',
33-
sa.Column('id', sa.Integer(), nullable=False),
34-
sa.Column('name', sa.String(length=80), nullable=False),
35-
sa.Column('original_due_date', sa.Date(), nullable=False),
36-
sa.Column('due_date', sa.Date(), nullable=False),
37-
sa.Column('priority', sa.Integer(), nullable=False),
38-
sa.Column('difficulty', sa.Integer(), nullable=False),
39-
sa.Column('repeat_interval', sa.Integer(), nullable=False),
40-
sa.Column('repeat_often', sa.Integer(), nullable=False),
41-
sa.Column('user_id', sa.Integer(), nullable=True),
42-
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
43-
sa.PrimaryKeyConstraint('id'),
44-
sa.UniqueConstraint('id')
34+
op.create_table(
35+
"task",
36+
sa.Column("id", sa.Integer(), nullable=False),
37+
sa.Column("name", sa.String(length=80), nullable=False),
38+
sa.Column("original_due_date", sa.Date(), nullable=False),
39+
sa.Column("due_date", sa.Date(), nullable=False),
40+
sa.Column("priority", sa.Integer(), nullable=False),
41+
sa.Column("difficulty", sa.Integer(), nullable=False),
42+
sa.Column("repeat_interval", sa.Integer(), nullable=False),
43+
sa.Column("repeat_often", sa.Integer(), nullable=False),
44+
sa.Column("user_id", sa.Integer(), nullable=True),
45+
sa.ForeignKeyConstraint(
46+
["user_id"],
47+
["user.id"],
48+
),
49+
sa.PrimaryKeyConstraint("id"),
50+
sa.UniqueConstraint("id"),
4551
)
4652
# ### end Alembic commands ###
4753

4854

4955
def downgrade():
5056
# ### commands auto generated by Alembic - please adjust! ###
51-
op.drop_table('task')
52-
op.drop_table('user')
57+
op.drop_table("task")
58+
op.drop_table("user")
5359
# ### end Alembic commands ###

migrations/versions/8f05b938c89f_add_times_completed_column.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,30 @@
55
Create Date: 2025-01-04 18:49:08.275615
66
77
"""
8+
89
from alembic import op
910
import sqlalchemy as sa
1011

1112

1213
# revision identifiers, used by Alembic.
13-
revision = '8f05b938c89f'
14-
down_revision = 'df154647d001'
14+
revision = "8f05b938c89f"
15+
down_revision = "df154647d001"
1516
branch_labels = None
1617
depends_on = None
1718

1819

1920
def upgrade():
2021
# ### commands auto generated by Alembic - please adjust! ###
21-
with op.batch_alter_table('task', schema=None) as batch_op:
22-
batch_op.add_column(sa.Column('times_completed', sa.Integer(), nullable=False))
22+
with op.batch_alter_table("task", schema=None) as batch_op:
23+
batch_op.add_column(sa.Column("times_completed",
24+
sa.Integer(), nullable=False))
2325

2426
# ### end Alembic commands ###
2527

2628

2729
def downgrade():
2830
# ### commands auto generated by Alembic - please adjust! ###
29-
with op.batch_alter_table('task', schema=None) as batch_op:
30-
batch_op.drop_column('times_completed')
31+
with op.batch_alter_table("task", schema=None) as batch_op:
32+
batch_op.drop_column("times_completed")
3133

3234
# ### end Alembic commands ###

migrations/versions/c0fcd66c3df6_add_daily_streak_column.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,34 @@
55
Create Date: 2025-01-04 18:51:36.383519
66
77
"""
8+
89
from alembic import op
910
import sqlalchemy as sa
1011

1112

1213
# revision identifiers, used by Alembic.
13-
revision = 'c0fcd66c3df6'
14-
down_revision = '5e8982aea1ef'
14+
revision = "c0fcd66c3df6"
15+
down_revision = "5e8982aea1ef"
1516
branch_labels = None
1617
depends_on = None
1718

1819

1920
def upgrade():
2021
# ### commands auto generated by Alembic - please adjust! ###
21-
with op.batch_alter_table('user', schema=None) as batch_op:
22-
batch_op.add_column(sa.Column('last_completion_date', sa.Date(), nullable=False))
23-
batch_op.add_column(sa.Column('daily_streak', sa.Integer(), nullable=False))
22+
with op.batch_alter_table("user", schema=None) as batch_op:
23+
batch_op.add_column(
24+
sa.Column("last_completion_date", sa.Date(), nullable=False)
25+
)
26+
batch_op.add_column(
27+
sa.Column("daily_streak", sa.Integer(), nullable=False))
2428

2529
# ### end Alembic commands ###
2630

2731

2832
def downgrade():
2933
# ### commands auto generated by Alembic - please adjust! ###
30-
with op.batch_alter_table('user', schema=None) as batch_op:
31-
batch_op.drop_column('daily_streak')
32-
batch_op.drop_column('last_completion_date')
34+
with op.batch_alter_table("user", schema=None) as batch_op:
35+
batch_op.drop_column("daily_streak")
36+
batch_op.drop_column("last_completion_date")
3337

3438
# ### end Alembic commands ###

migrations/versions/df154647d001_add_streak_column.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,29 @@
55
Create Date: 2025-01-04 18:48:09.868863
66
77
"""
8+
89
from alembic import op
910
import sqlalchemy as sa
1011

1112

1213
# revision identifiers, used by Alembic.
13-
revision = 'df154647d001'
14-
down_revision = '868a910249be'
14+
revision = "df154647d001"
15+
down_revision = "868a910249be"
1516
branch_labels = None
1617
depends_on = None
1718

1819

1920
def upgrade():
2021
# ### commands auto generated by Alembic - please adjust! ###
21-
with op.batch_alter_table('task', schema=None) as batch_op:
22-
batch_op.add_column(sa.Column('streak', sa.Integer(), nullable=False))
22+
with op.batch_alter_table("task", schema=None) as batch_op:
23+
batch_op.add_column(sa.Column("streak", sa.Integer(), nullable=False))
2324

2425
# ### end Alembic commands ###
2526

2627

2728
def downgrade():
2829
# ### commands auto generated by Alembic - please adjust! ###
29-
with op.batch_alter_table('task', schema=None) as batch_op:
30-
batch_op.drop_column('streak')
30+
with op.batch_alter_table("task", schema=None) as batch_op:
31+
batch_op.drop_column("streak")
3132

3233
# ### end Alembic commands ###

0 commit comments

Comments
 (0)