update alembic/env.py
Browse files- alembic/env.py +75 -0
alembic/env.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from logging.config import fileConfig
|
| 2 |
+
|
| 3 |
+
from sqlalchemy import engine_from_config
|
| 4 |
+
from sqlalchemy import pool
|
| 5 |
+
|
| 6 |
+
from alembic import context
|
| 7 |
+
import sys
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
sys.path.append(str(Path(__file__).parent.parent))
|
| 11 |
+
|
| 12 |
+
from app.database import Base
|
| 13 |
+
from app.db_models import User, ProxySource, Proxy, ValidationHistory
|
| 14 |
+
|
| 15 |
+
config = context.config
|
| 16 |
+
|
| 17 |
+
if config.config_file_name is not None:
|
| 18 |
+
fileConfig(config.config_file_name)
|
| 19 |
+
|
| 20 |
+
target_metadata = Base.metadata
|
| 21 |
+
|
| 22 |
+
# other values from the config, defined by the needs of env.py,
|
| 23 |
+
# can be acquired:
|
| 24 |
+
# my_important_option = config.get_main_option("my_important_option")
|
| 25 |
+
# ... etc.
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def run_migrations_offline() -> None:
|
| 29 |
+
"""Run migrations in 'offline' mode.
|
| 30 |
+
|
| 31 |
+
This configures the context with just a URL
|
| 32 |
+
and not an Engine, though an Engine is acceptable
|
| 33 |
+
here as well. By skipping the Engine creation
|
| 34 |
+
we don't even need a DBAPI to be available.
|
| 35 |
+
|
| 36 |
+
Calls to context.execute() here emit the given string to the
|
| 37 |
+
script output.
|
| 38 |
+
|
| 39 |
+
"""
|
| 40 |
+
url = config.get_main_option("sqlalchemy.url")
|
| 41 |
+
context.configure(
|
| 42 |
+
url=url,
|
| 43 |
+
target_metadata=target_metadata,
|
| 44 |
+
literal_binds=True,
|
| 45 |
+
dialect_opts={"paramstyle": "named"},
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
with context.begin_transaction():
|
| 49 |
+
context.run_migrations()
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def run_migrations_online() -> None:
|
| 53 |
+
"""Run migrations in 'online' mode.
|
| 54 |
+
|
| 55 |
+
In this scenario we need to create an Engine
|
| 56 |
+
and associate a connection with the context.
|
| 57 |
+
|
| 58 |
+
"""
|
| 59 |
+
connectable = engine_from_config(
|
| 60 |
+
config.get_section(config.config_ini_section, {}),
|
| 61 |
+
prefix="sqlalchemy.",
|
| 62 |
+
poolclass=pool.NullPool,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
with connectable.connect() as connection:
|
| 66 |
+
context.configure(connection=connection, target_metadata=target_metadata)
|
| 67 |
+
|
| 68 |
+
with context.begin_transaction():
|
| 69 |
+
context.run_migrations()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
if context.is_offline_mode():
|
| 73 |
+
run_migrations_offline()
|
| 74 |
+
else:
|
| 75 |
+
run_migrations_online()
|