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