|
| 1 | +""" |
| 2 | +This file is auto generated when we run `alembic init alembic` but modified according to our needs. |
| 3 | +This Python script runs whenever the alembic migration tool is invoked. |
| 4 | +It contains instructions to configure and generate a SQLAlchemy engine, |
| 5 | +procure a connection from that engine along with a transaction, and then |
| 6 | +invoke the migration engine, using the connection as a source of database connectivity. |
| 7 | +""" |
| 8 | +import sys |
| 9 | +from logging.config import fileConfig |
| 10 | + |
| 11 | + |
| 12 | +from alembic import context |
| 13 | + |
| 14 | +from pbench.common.exceptions import BadConfig, ConfigFileNotSpecified |
| 15 | +from pbench.server.api.resources.database import Database |
| 16 | +from pbench.common.logger import get_pbench_logger |
| 17 | +from pbench.server.api import get_server_config |
| 18 | + |
| 19 | +# this is the Alembic Config object, which provides |
| 20 | +# access to the values within the .ini file in use. |
| 21 | +config = context.config |
| 22 | + |
| 23 | +# Interpret the config file for Python logging. |
| 24 | +# This line sets up loggers basically. |
| 25 | +fileConfig(config.config_file_name) |
| 26 | + |
| 27 | +# add your model's MetaData object here |
| 28 | +# for 'autogenerate' support |
| 29 | +# from myapp import mymodel |
| 30 | +# target_metadata = mymodel.Base.metadata |
| 31 | +target_metadata = [Database.Base.metadata] |
| 32 | + |
| 33 | +# other values from the config, defined by the needs of env.py, |
| 34 | +# can be acquired: |
| 35 | +# my_important_option = config.get_main_option("my_important_option") |
| 36 | +# ... etc. |
| 37 | + |
| 38 | + |
| 39 | +def run_migrations_offline(): |
| 40 | + """Run migrations in 'offline' mode. |
| 41 | +
|
| 42 | + This configures the context with just a URL |
| 43 | + and not an Engine, though an Engine is acceptable |
| 44 | + here as well. By skipping the Engine creation |
| 45 | + we don't even need a DBAPI to be available. |
| 46 | +
|
| 47 | + Calls to context.execute() here emit the given string to the |
| 48 | + script output. |
| 49 | +
|
| 50 | + """ |
| 51 | + # url = config.get_main_option("sqlalchemy.url") |
| 52 | + try: |
| 53 | + server_config = get_server_config() |
| 54 | + logger = get_pbench_logger(__name__, server_config) |
| 55 | + except (ConfigFileNotSpecified, BadConfig) as e: |
| 56 | + print(e) |
| 57 | + sys.exit(1) |
| 58 | + url = Database.get_engine_uri(server_config, logger) |
| 59 | + |
| 60 | + context.configure( |
| 61 | + url=url, |
| 62 | + target_metadata=target_metadata, |
| 63 | + literal_binds=True, |
| 64 | + dialect_opts={"paramstyle": "named"}, |
| 65 | + ) |
| 66 | + |
| 67 | + with context.begin_transaction(): |
| 68 | + context.run_migrations() |
| 69 | + |
| 70 | + |
| 71 | +def run_migrations_online(): |
| 72 | + """Run migrations in 'online' mode. |
| 73 | +
|
| 74 | + In this scenario we need to create an Engine |
| 75 | + and associate a connection with the context. |
| 76 | +
|
| 77 | + """ |
| 78 | + try: |
| 79 | + server_config = get_server_config() |
| 80 | + logger = get_pbench_logger(__name__, server_config) |
| 81 | + except (ConfigFileNotSpecified, BadConfig) as e: |
| 82 | + print(e) |
| 83 | + sys.exit(1) |
| 84 | + |
| 85 | + connectable = Database.init_engine(server_config, logger) |
| 86 | + # connectable = engine_from_config( |
| 87 | + # config.get_section(config.config_ini_section), |
| 88 | + # prefix="sqlalchemy.", |
| 89 | + # poolclass=pool.NullPool, |
| 90 | + # ) |
| 91 | + |
| 92 | + with connectable.connect() as connection: |
| 93 | + context.configure(connection=connection, target_metadata=target_metadata) |
| 94 | + |
| 95 | + with context.begin_transaction(): |
| 96 | + context.run_migrations() |
| 97 | + |
| 98 | + |
| 99 | +if context.is_offline_mode(): |
| 100 | + print("running migration offline") |
| 101 | + run_migrations_offline() |
| 102 | +else: |
| 103 | + run_migrations_online() |
| 104 | + print("running migration online") |
0 commit comments