Skip to content

Commit d16a393

Browse files
committed
Add migrations DB function (for Flask-Migrate)
1 parent e949f99 commit d16a393

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed
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
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from __future__ import with_statement
2+
3+
import logging
4+
from logging.config import fileConfig
5+
from flask import current_app
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+
# add your model's MetaData object here
18+
# for 'autogenerate' support
19+
# from myapp import mymodel
20+
# target_metadata = mymodel.Base.metadata
21+
config.set_main_option(
22+
'sqlalchemy.url',
23+
str(current_app.extensions['migrate'].db.get_engine().url).replace(
24+
'%', '%%'))
25+
target_metadata = current_app.extensions['migrate'].db.metadata
26+
27+
# other values from the config, defined by the needs of env.py,
28+
# can be acquired:
29+
# my_important_option = config.get_main_option("my_important_option")
30+
# ... etc.
31+
32+
33+
def run_migrations_offline():
34+
"""Run migrations in 'offline' mode.
35+
36+
This configures the context with just a URL
37+
and not an Engine, though an Engine is acceptable
38+
here as well. By skipping the Engine creation
39+
we don't even need a DBAPI to be available.
40+
41+
Calls to context.execute() here emit the given string to the
42+
script output.
43+
44+
"""
45+
url = config.get_main_option("sqlalchemy.url")
46+
context.configure(
47+
url=url, target_metadata=target_metadata, literal_binds=True
48+
)
49+
50+
with context.begin_transaction():
51+
context.run_migrations()
52+
53+
54+
def run_migrations_online():
55+
"""Run migrations in 'online' mode.
56+
57+
In this scenario we need to create an Engine
58+
and associate a connection with the context.
59+
60+
"""
61+
62+
# this callback is used to prevent an auto-migration from being generated
63+
# when there are no changes to the schema
64+
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
65+
def process_revision_directives(context, revision, directives):
66+
if getattr(config.cmd_opts, 'autogenerate', False):
67+
script = directives[0]
68+
if script.upgrade_ops.is_empty():
69+
directives[:] = []
70+
logger.info('No changes in schema detected.')
71+
72+
connectable = current_app.extensions['migrate'].db.get_engine()
73+
74+
with connectable.connect() as connection:
75+
context.configure(
76+
connection=connection,
77+
target_metadata=target_metadata,
78+
process_revision_directives=process_revision_directives,
79+
**current_app.extensions['migrate'].configure_args
80+
)
81+
82+
with context.begin_transaction():
83+
context.run_migrations()
84+
85+
86+
if context.is_offline_mode():
87+
run_migrations_offline()
88+
else:
89+
run_migrations_online()

auth_service/requirements.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Flask==2.0.1
2+
Flask-And-Redis==1.0.0
3+
Flask-Cors==3.0.10
4+
Flask-JWT-Extended==3.24.1
5+
Flask-SQLAlchemy==2.5.1
6+
gunicorn==20.1.0
7+
Jinja2==3.0.1
8+
marshmallow==3.12.2
9+
greenlet==1.1.0
10+
SQLAlchemy==1.4.21
11+
Flask-Migrate==3.1.0
12+
mysqlclient==2.0.3

auth_service/server.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from auth_app.app import create_app
2+
3+
app = create_app()
4+
if __name__ == '__main__':
5+
"""
6+
Main Application
7+
python manage.py
8+
"""
9+
app.run(host='0.0.0.0', port=5012)

0 commit comments

Comments
 (0)