|
@@ -0,0 +1,79 @@
|
|
1
|
+from __future__ import with_statement
|
|
2
|
+import os
|
|
3
|
+from alembic import context
|
|
4
|
+from sqlalchemy import engine_from_config, pool
|
|
5
|
+from logging.config import fileConfig
|
|
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
|
+
|
|
15
|
+# add your model's MetaData object here
|
|
16
|
+# for 'autogenerate' support
|
|
17
|
+# from myapp import mymodel
|
|
18
|
+# target_metadata = mymodel.Base.metadata
|
|
19
|
+import piket_server.database.schema
|
|
20
|
+target_metadata = piket_server.database.schema.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
|
+data_home = os.environ.get('XDG_DATA_HOME', '~/.local/share')
|
|
27
|
+config_dir = os.path.join(data_home, 'piket_server')
|
|
28
|
+os.makedirs(os.path.expanduser(config_dir), mode=0o744, exist_ok=True)
|
|
29
|
+db_path = os.path.expanduser(os.path.join(config_dir, 'database.sqlite3'))
|
|
30
|
+
|
|
31
|
+config.file_config['alembic']['sqlalchemy.url'] = f'sqlite:///{db_path}'
|
|
32
|
+print(config.get_main_option('sqlalchemy.url'))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+def run_migrations_offline():
|
|
36
|
+ """Run migrations in 'offline' mode.
|
|
37
|
+
|
|
38
|
+ This configures the context with just a URL
|
|
39
|
+ and not an Engine, though an Engine is acceptable
|
|
40
|
+ here as well. By skipping the Engine creation
|
|
41
|
+ we don't even need a DBAPI to be available.
|
|
42
|
+
|
|
43
|
+ Calls to context.execute() here emit the given string to the
|
|
44
|
+ script output.
|
|
45
|
+
|
|
46
|
+ """
|
|
47
|
+ url = config.get_main_option("sqlalchemy.url")
|
|
48
|
+ context.configure(
|
|
49
|
+ url=url, target_metadata=target_metadata, literal_binds=True)
|
|
50
|
+
|
|
51
|
+ with context.begin_transaction():
|
|
52
|
+ context.run_migrations()
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+def run_migrations_online():
|
|
56
|
+ """Run migrations in 'online' mode.
|
|
57
|
+
|
|
58
|
+ In this scenario we need to create an Engine
|
|
59
|
+ and associate a connection with the context.
|
|
60
|
+
|
|
61
|
+ """
|
|
62
|
+ connectable = engine_from_config(
|
|
63
|
+ config.get_section(config.config_ini_section),
|
|
64
|
+ prefix='sqlalchemy.',
|
|
65
|
+ poolclass=pool.NullPool)
|
|
66
|
+
|
|
67
|
+ with connectable.connect() as connection:
|
|
68
|
+ context.configure(
|
|
69
|
+ connection=connection,
|
|
70
|
+ target_metadata=target_metadata
|
|
71
|
+ )
|
|
72
|
+
|
|
73
|
+ with context.begin_transaction():
|
|
74
|
+ context.run_migrations()
|
|
75
|
+
|
|
76
|
+if context.is_offline_mode():
|
|
77
|
+ run_migrations_offline()
|
|
78
|
+else:
|
|
79
|
+ run_migrations_online()
|