-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_migration.py
51 lines (41 loc) · 1.31 KB
/
generate_migration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#
# generate_migration
#
import os
import alembic.config
import sys
from medium.database.sqldblib import engine
from alembic.config import Config
from alembic import command
import argparse
#
# this will execute
# alembic revision --autogenerate -m "message"
# where message is the first cli parameter
#
def generate_migration(message="NONE"):
import warnings
from sqlalchemy import exc as sa_exc
print(40*"-")
print(" generating migration: " + message)
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=sa_exc.SAWarning)
alembic_cfg = Config(os.path.join(os.path.dirname(__file__), "alembic.ini"))
script =command.revision(alembic_cfg, autogenerate=True, message=message)
#print("... " + str(dir(script)) + " done")
print(" rev: " +str(script.revision))
print(" path: " +str(script.path))
print(40*"-")
return script
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-n', "--name", action="store",
dest="message", help='-n my_first_migration',
default=None, required=True)
args = parser.parse_args()
if args.message:
generate_migration(message=args.message)
else:
print("You must give a migration name. -n <something>")
if __name__ == "__main__":
main()