gitscope
https://dev.kronoz.win/r/marzneshin.git
app/db/migrations/env.py · v0.3.2
Source
1 a47eb4c Dawsh 2y ago from logging.config import fileConfig
2 a47eb4c Dawsh 2y ago
3 8111eb7 Dawsh 2y ago from alembic import context
4 a47eb4c Dawsh 2y ago from sqlalchemy import engine_from_config
5 a47eb4c Dawsh 2y ago from sqlalchemy import pool
6 a47eb4c Dawsh 2y ago
7 8111eb7 Dawsh 2y ago from app.config.env import SQLALCHEMY_DATABASE_URL
8 a47eb4c Dawsh 2y ago from app.db.base import Base
9 a47eb4c Dawsh 2y ago
10 a47eb4c Dawsh 2y ago # this is the Alembic Config object, which provides
11 a47eb4c Dawsh 2y ago # access to the values within the .ini file in use.
12 a47eb4c Dawsh 2y ago config = context.config
13 01cdf2e Dawsh 2y ago config.set_main_option("sqlalchemy.url", SQLALCHEMY_DATABASE_URL)
14 a47eb4c Dawsh 2y ago
15 a47eb4c Dawsh 2y ago # Interpret the config file for Python logging.
16 a47eb4c Dawsh 2y ago # This line sets up loggers basically.
17 a47eb4c Dawsh 2y ago if config.config_file_name is not None:
18 a47eb4c Dawsh 2y ago fileConfig(config.config_file_name)
19 a47eb4c Dawsh 2y ago
20 a47eb4c Dawsh 2y ago # add your model's MetaData object here
21 a47eb4c Dawsh 2y ago # for 'autogenerate' support
22 a47eb4c Dawsh 2y ago # from myapp import mymodel
23 a47eb4c Dawsh 2y ago # target_metadata = mymodel.Base.metadata
24 a47eb4c Dawsh 2y ago target_metadata = Base.metadata
25 a47eb4c Dawsh 2y ago
26 a47eb4c Dawsh 2y ago # other values from the config, defined by the needs of env.py,
27 a47eb4c Dawsh 2y ago # can be acquired:
28 a47eb4c Dawsh 2y ago # my_important_option = config.get_main_option("my_important_option")
29 a47eb4c Dawsh 2y ago # ... etc.
30 a47eb4c Dawsh 2y ago
31 a47eb4c Dawsh 2y ago
32 a47eb4c Dawsh 2y ago def run_migrations_offline() -> None:
33 a47eb4c Dawsh 2y ago """Run migrations in 'offline' mode.
34 a47eb4c Dawsh 2y ago
35 a47eb4c Dawsh 2y ago This configures the context with just a URL
36 a47eb4c Dawsh 2y ago and not an Engine, though an Engine is acceptable
37 a47eb4c Dawsh 2y ago here as well. By skipping the Engine creation
38 a47eb4c Dawsh 2y ago we don't even need a DBAPI to be available.
39 a47eb4c Dawsh 2y ago
40 a47eb4c Dawsh 2y ago Calls to context.execute() here emit the given string to the
41 a47eb4c Dawsh 2y ago script output.
42 a47eb4c Dawsh 2y ago
43 a47eb4c Dawsh 2y ago """
44 a47eb4c Dawsh 2y ago url = config.get_main_option("sqlalchemy.url")
45 a47eb4c Dawsh 2y ago context.configure(
46 a47eb4c Dawsh 2y ago url=url,
47 a47eb4c Dawsh 2y ago target_metadata=target_metadata,
48 a47eb4c Dawsh 2y ago literal_binds=True,
49 a47eb4c Dawsh 2y ago dialect_opts={"paramstyle": "named"},
50 a47eb4c Dawsh 2y ago )
51 a47eb4c Dawsh 2y ago
52 a47eb4c Dawsh 2y ago with context.begin_transaction():
53 a47eb4c Dawsh 2y ago context.run_migrations()
54 a47eb4c Dawsh 2y ago
55 a47eb4c Dawsh 2y ago
56 a47eb4c Dawsh 2y ago def run_migrations_online() -> None:
57 a47eb4c Dawsh 2y ago """Run migrations in 'online' mode.
58 a47eb4c Dawsh 2y ago
59 a47eb4c Dawsh 2y ago In this scenario we need to create an Engine
60 a47eb4c Dawsh 2y ago and associate a connection with the context.
61 a47eb4c Dawsh 2y ago
62 a47eb4c Dawsh 2y ago """
63 a47eb4c Dawsh 2y ago connectable = engine_from_config(
64 a47eb4c Dawsh 2y ago config.get_section(config.config_ini_section),
65 a47eb4c Dawsh 2y ago prefix="sqlalchemy.",
66 a47eb4c Dawsh 2y ago poolclass=pool.NullPool,
67 a47eb4c Dawsh 2y ago )
68 a47eb4c Dawsh 2y ago
69 a47eb4c Dawsh 2y ago with connectable.connect() as connection:
70 f1ab16c Dawsh 2y ago context.configure(
71 f1ab16c Dawsh 2y ago connection=connection, target_metadata=target_metadata
72 f1ab16c Dawsh 2y ago )
73 a47eb4c Dawsh 2y ago
74 a47eb4c Dawsh 2y ago with context.begin_transaction():
75 a47eb4c Dawsh 2y ago context.run_migrations()
76 a47eb4c Dawsh 2y ago
77 a47eb4c Dawsh 2y ago
78 a47eb4c Dawsh 2y ago if context.is_offline_mode():
79 a47eb4c Dawsh 2y ago run_migrations_offline()
80 a47eb4c Dawsh 2y ago else:
81 a47eb4c Dawsh 2y ago run_migrations_online()