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