|
| 1 | +import logging |
1 | 2 | import os |
2 | 3 | from logging.config import fileConfig |
3 | 4 |
|
|
9 | 10 |
|
10 | 11 | load_dotenv() |
11 | 12 |
|
| 13 | +log = logging.getLogger("alembic") |
| 14 | +log.info("Entering env.py for alembic migration") |
12 | 15 | # this is the Alembic Config object, which provides |
13 | 16 | # access to the values within the .ini file in use. |
14 | 17 | config = context.config |
| 18 | +log.info("Finished setting up alembic config object") |
15 | 19 |
|
16 | 20 | # Interpret the config file for Python logging. |
17 | 21 | # This line sets up loggers basically. |
18 | 22 | if config.config_file_name is not None: |
19 | | - fileConfig(config.config_file_name) |
| 23 | + fileConfig(config.config_file_name, disable_existing_loggers=False) |
20 | 24 |
|
21 | 25 | # add your model's MetaData object here |
22 | 26 | # for 'autogenerate' support |
23 | 27 | # from myapp import mymodel |
24 | 28 | # target_metadata = mymodel.Base.metadata |
25 | 29 |
|
| 30 | +log.info("Pulling model metadata") |
| 31 | + |
26 | 32 | target_metadata = Base.metadata |
27 | 33 |
|
28 | 34 | # other values from the config, defined by the needs of env.py, |
29 | 35 | # can be acquired: |
30 | 36 | # my_important_option = config.get_main_option("my_important_option") |
31 | 37 | # ... etc. |
32 | 38 | config.set_main_option("sqlalchemy.url", os.environ["POSTGRES_DATABASE_URL"]) |
| 39 | +log.info("Finished migration env config setup") |
33 | 40 |
|
34 | 41 |
|
35 | 42 | def run_migrations_offline() -> None: |
@@ -69,13 +76,19 @@ def run_migrations_online() -> None: |
69 | 76 | prefix="sqlalchemy.", |
70 | 77 | poolclass=pool.NullPool, |
71 | 78 | ) |
| 79 | + try: |
| 80 | + log.info("Established database connection") |
| 81 | + with connectable.connect() as connection: |
| 82 | + context.configure(connection=connection, target_metadata=target_metadata) |
72 | 83 |
|
73 | | - with connectable.connect() as connection: |
74 | | - context.configure(connection=connection, target_metadata=target_metadata) |
| 84 | + with context.begin_transaction(): |
| 85 | + context.run_migrations() |
| 86 | + log.info("Finished running migrations in alembic env") |
| 87 | + except Exception as e: |
| 88 | + log.error(e) |
75 | 89 |
|
76 | | - with context.begin_transaction(): |
77 | | - context.run_migrations() |
78 | 90 |
|
| 91 | +log.info("Starting up migration env") |
79 | 92 |
|
80 | 93 | if context.is_offline_mode(): |
81 | 94 | run_migrations_offline() |
|
0 commit comments