Skip to content

Commit 8cfa848

Browse files
add Flask-Migrate to existing project
1 parent c4f5127 commit 8cfa848

File tree

8 files changed

+268
-4
lines changed

8 files changed

+268
-4
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ Copy `.env.example` and rename to `.env`. Provide your database URL to the `SQLA
2828
Create database tables:
2929

3030
```bash
31-
flask --app app shell
32-
>>> with app.app_context():
33-
>>> db.create_all()
31+
flask db upgrade head
3432
```
3533

3634
Start the server: (Runs on 127.0.0.1:5000)

app/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
22

33
from flask import Flask
4+
from flask_migrate import Migrate
45
from flask_sqlalchemy import SQLAlchemy
56
from dotenv import load_dotenv
67
from flasgger import Swagger
8+
from sqlalchemy import MetaData
79

810

911
app = Flask(__name__)
@@ -12,7 +14,17 @@
1214
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("SQLALCHEMY_DATABASE_URI")
1315
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1416

15-
db = SQLAlchemy(app)
17+
# PostgreSQL-compatible naming convention
18+
naming_convention = {
19+
"ix": "%(table_name)s_%(column_0_name)s_idx", # Indexes
20+
"uq": "%(table_name)s_%(column_0_name)s_key", # Unique constraints
21+
"ck": "%(table_name)s_%(constraint_name)s_check", # Check constraints
22+
"fk": "%(table_name)s_%(column_0_name)s_fkey", # Foreign keys
23+
"pk": "%(table_name)s_pkey" # Primary keys
24+
}
25+
metadata = MetaData(naming_convention=naming_convention)
26+
db = SQLAlchemy(app, metadata=metadata)
27+
migrate = Migrate(app, db)
1628

1729
from app import routes
1830

migrations/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Single-database configuration for Flask.

migrations/alembic.ini

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# template used to generate migration files
5+
# file_template = %%(rev)s_%%(slug)s
6+
7+
# set to 'true' to run the environment during
8+
# the 'revision' command, regardless of autogenerate
9+
# revision_environment = false
10+
11+
12+
# Logging configuration
13+
[loggers]
14+
keys = root,sqlalchemy,alembic,flask_migrate
15+
16+
[handlers]
17+
keys = console
18+
19+
[formatters]
20+
keys = generic
21+
22+
[logger_root]
23+
level = WARN
24+
handlers = console
25+
qualname =
26+
27+
[logger_sqlalchemy]
28+
level = WARN
29+
handlers =
30+
qualname = sqlalchemy.engine
31+
32+
[logger_alembic]
33+
level = INFO
34+
handlers =
35+
qualname = alembic
36+
37+
[logger_flask_migrate]
38+
level = INFO
39+
handlers =
40+
qualname = flask_migrate
41+
42+
[handler_console]
43+
class = StreamHandler
44+
args = (sys.stderr,)
45+
level = NOTSET
46+
formatter = generic
47+
48+
[formatter_generic]
49+
format = %(levelname)-5.5s [%(name)s] %(message)s
50+
datefmt = %H:%M:%S

migrations/env.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import logging
2+
from logging.config import fileConfig
3+
4+
from flask import current_app
5+
6+
from alembic import context
7+
8+
# this is the Alembic Config object, which provides
9+
# access to the values within the .ini file in use.
10+
config = context.config
11+
12+
# Interpret the config file for Python logging.
13+
# This line sets up loggers basically.
14+
fileConfig(config.config_file_name)
15+
logger = logging.getLogger('alembic.env')
16+
17+
18+
def get_engine():
19+
try:
20+
# this works with Flask-SQLAlchemy<3 and Alchemical
21+
return current_app.extensions['migrate'].db.get_engine()
22+
except (TypeError, AttributeError):
23+
# this works with Flask-SQLAlchemy>=3
24+
return current_app.extensions['migrate'].db.engine
25+
26+
27+
def get_engine_url():
28+
try:
29+
return get_engine().url.render_as_string(hide_password=False).replace(
30+
'%', '%%')
31+
except AttributeError:
32+
return str(get_engine().url).replace('%', '%%')
33+
34+
35+
# add your model's MetaData object here
36+
# for 'autogenerate' support
37+
# from myapp import mymodel
38+
# target_metadata = mymodel.Base.metadata
39+
config.set_main_option('sqlalchemy.url', get_engine_url())
40+
target_db = current_app.extensions['migrate'].db
41+
42+
# other values from the config, defined by the needs of env.py,
43+
# can be acquired:
44+
# my_important_option = config.get_main_option("my_important_option")
45+
# ... etc.
46+
47+
48+
def get_metadata():
49+
if hasattr(target_db, 'metadatas'):
50+
return target_db.metadatas[None]
51+
return target_db.metadata
52+
53+
54+
def run_migrations_offline():
55+
"""Run migrations in 'offline' mode.
56+
57+
This configures the context with just a URL
58+
and not an Engine, though an Engine is acceptable
59+
here as well. By skipping the Engine creation
60+
we don't even need a DBAPI to be available.
61+
62+
Calls to context.execute() here emit the given string to the
63+
script output.
64+
65+
"""
66+
url = config.get_main_option("sqlalchemy.url")
67+
context.configure(
68+
url=url, target_metadata=get_metadata(), literal_binds=True
69+
)
70+
71+
with context.begin_transaction():
72+
context.run_migrations()
73+
74+
75+
def run_migrations_online():
76+
"""Run migrations in 'online' mode.
77+
78+
In this scenario we need to create an Engine
79+
and associate a connection with the context.
80+
81+
"""
82+
83+
# this callback is used to prevent an auto-migration from being generated
84+
# when there are no changes to the schema
85+
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
86+
def process_revision_directives(context, revision, directives):
87+
if getattr(config.cmd_opts, 'autogenerate', False):
88+
script = directives[0]
89+
if script.upgrade_ops.is_empty():
90+
directives[:] = []
91+
logger.info('No changes in schema detected.')
92+
93+
conf_args = current_app.extensions['migrate'].configure_args
94+
if conf_args.get("process_revision_directives") is None:
95+
conf_args["process_revision_directives"] = process_revision_directives
96+
97+
connectable = get_engine()
98+
99+
with connectable.connect() as connection:
100+
context.configure(
101+
connection=connection,
102+
target_metadata=get_metadata(),
103+
**conf_args
104+
)
105+
106+
with context.begin_transaction():
107+
context.run_migrations()
108+
109+
110+
if context.is_offline_mode():
111+
run_migrations_offline()
112+
else:
113+
run_migrations_online()

migrations/script.py.mako

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
${imports if imports else ""}
11+
12+
# revision identifiers, used by Alembic.
13+
revision = ${repr(up_revision)}
14+
down_revision = ${repr(down_revision)}
15+
branch_labels = ${repr(branch_labels)}
16+
depends_on = ${repr(depends_on)}
17+
18+
19+
def upgrade():
20+
${upgrades if upgrades else "pass"}
21+
22+
23+
def downgrade():
24+
${downgrades if downgrades else "pass"}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Initial migration
2+
3+
Revision ID: c2ba8ccc9a6f
4+
Revises:
5+
Create Date: 2025-01-17 17:06:38.552656
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'c2ba8ccc9a6f'
14+
down_revision = None
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.create_table('category',
22+
sa.Column('id', sa.Integer(), nullable=False),
23+
sa.Column('name', sa.String(length=200), nullable=False),
24+
sa.Column('created_at', sa.DateTime(), nullable=True),
25+
sa.PrimaryKeyConstraint('id', name=op.f('category_pkey')),
26+
sa.UniqueConstraint('name', name=op.f('category_name_key'))
27+
)
28+
op.create_table('product',
29+
sa.Column('id', sa.Integer(), nullable=False),
30+
sa.Column('name', sa.String(length=200), nullable=False),
31+
sa.Column('description', sa.String(length=500), nullable=True),
32+
sa.Column('created_at', sa.DateTime(), nullable=True),
33+
sa.PrimaryKeyConstraint('id', name=op.f('product_pkey')),
34+
sa.UniqueConstraint('name', name=op.f('product_name_key'))
35+
)
36+
op.create_table('subcategory',
37+
sa.Column('id', sa.Integer(), nullable=False),
38+
sa.Column('name', sa.String(length=200), nullable=False),
39+
sa.Column('created_at', sa.DateTime(), nullable=True),
40+
sa.PrimaryKeyConstraint('id', name=op.f('subcategory_pkey')),
41+
sa.UniqueConstraint('name', name=op.f('subcategory_name_key'))
42+
)
43+
op.create_table('category_subcategory',
44+
sa.Column('category_id', sa.Integer(), nullable=True),
45+
sa.Column('subcategory_id', sa.Integer(), nullable=True),
46+
sa.ForeignKeyConstraint(['category_id'], ['category.id'], name=op.f('category_subcategory_category_id_fkey')),
47+
sa.ForeignKeyConstraint(['subcategory_id'], ['subcategory.id'], name=op.f('category_subcategory_subcategory_id_fkey'))
48+
)
49+
op.create_table('subcategory_product',
50+
sa.Column('subcategory_id', sa.Integer(), nullable=True),
51+
sa.Column('product_id', sa.Integer(), nullable=True),
52+
sa.ForeignKeyConstraint(['product_id'], ['product.id'], name=op.f('subcategory_product_product_id_fkey')),
53+
sa.ForeignKeyConstraint(['subcategory_id'], ['subcategory.id'], name=op.f('subcategory_product_subcategory_id_fkey'))
54+
)
55+
# ### end Alembic commands ###
56+
57+
58+
def downgrade():
59+
# ### commands auto generated by Alembic - please adjust! ###
60+
op.drop_table('subcategory_product')
61+
op.drop_table('category_subcategory')
62+
op.drop_table('subcategory')
63+
op.drop_table('product')
64+
op.drop_table('category')
65+
# ### end Alembic commands ###

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Flask-SQLAlchemy==3.1.1
44
psycopg2-binary==2.9.10
55
python-dotenv==1.0.1
66
flasgger==0.9.7.1
7+
Flask-Migrate==4.1.0

0 commit comments

Comments
 (0)