Create env.py
taylor_socfortress committed
Jul 10, 2023 at 16:49 UTC
d45bf52d107d4f6bcf81d595267385ed98cfb468
1 file changed
+106
backend/migrations/env.py
new
+106
@@ -0,0 +1,106 @@
1
+import logging
2
+from logging.config import fileConfig
3
+
4
+from alembic import context
5
+from flask import current_app
6
+
7
+# this is the Alembic Config object, which provides
8
+# access to the values within the .ini file in use.
9
+config = context.config
10
+
11
+# Interpret the config file for Python logging.
12
+# This line sets up loggers basically.
13
+fileConfig(config.config_file_name)
14
+logger = logging.getLogger("alembic.env")
15
+
16
+
17
+def get_engine():
18
+ try:
19
+ # this works with Flask-SQLAlchemy<3 and Alchemical
20
+ return current_app.extensions["migrate"].db.get_engine()
21
+ except TypeError:
22
+ # this works with Flask-SQLAlchemy>=3
23
+ return current_app.extensions["migrate"].db.engine
24
+
25
+
26
+def get_engine_url():
27
+ try:
28
+ return get_engine().url.render_as_string(hide_password=False).replace("%", "%%")
29
+ except AttributeError:
30
+ return str(get_engine().url).replace("%", "%%")
31
+
32
+
33
+# add your model's MetaData object here
34
+# for 'autogenerate' support
35
+# from myapp import mymodel
36
+# target_metadata = mymodel.Base.metadata
37
+config.set_main_option("sqlalchemy.url", get_engine_url())
38
+target_db = current_app.extensions["migrate"].db
39
+
40
+# other values from the config, defined by the needs of env.py,
41
+# can be acquired:
42
+# my_important_option = config.get_main_option("my_important_option")
43
+# ... etc.
44
+
45
+
46
+def get_metadata():
47
+ if hasattr(target_db, "metadatas"):
48
+ return target_db.metadatas[None]
49
+ return target_db.metadata
50
+
51
+
52
+def run_migrations_offline():
53
+ """Run migrations in 'offline' mode.
54
+
55
+ This configures the context with just a URL
56
+ and not an Engine, though an Engine is acceptable
57
+ here as well. By skipping the Engine creation
58
+ we don't even need a DBAPI to be available.
59
+
60
+ Calls to context.execute() here emit the given string to the
61
+ script output.
62
+
63
+ """
64
+ url = config.get_main_option("sqlalchemy.url")
65
+ context.configure(url=url, target_metadata=get_metadata(), literal_binds=True)
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
+
79
+ # this callback is used to prevent an auto-migration from being generated
80
+ # when there are no changes to the schema
81
+ # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
82
+ def process_revision_directives(context, revision, directives):
83
+ if getattr(config.cmd_opts, "autogenerate", False):
84
+ script = directives[0]
85
+ if script.upgrade_ops.is_empty():
86
+ directives[:] = []
87
+ logger.info("No changes in schema detected.")
88
+
89
+ connectable = get_engine()
90
+
91
+ with connectable.connect() as connection:
92
+ context.configure(
93
+ connection=connection,
94
+ target_metadata=get_metadata(),
95
+ process_revision_directives=process_revision_directives,
96
+ **current_app.extensions["migrate"].configure_args,
97
+ )
98
+
99
+ with context.begin_transaction():
100
+ context.run_migrations()
101
+
102
+
103
+if context.is_offline_mode():
104
+ run_migrations_offline()
105
+else:
106
+ run_migrations_online()