-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.py
142 lines (111 loc) · 3.47 KB
/
cli.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python3
""" CLI script for working with database """
import asyncio
from argparse import ArgumentParser
from os import listdir, getcwd, path
import aiopg
MIGRATION_DIRECTORY = path.join(getcwd(), 'migrations/')
INITIAL_MIGRATION = """CREATE TABLE IF NOT EXISTS migrations (
"id" SERIAL PRIMARY KEY,
"name" VARCHAR NOT NULL UNIQUE
)"""
FIND_MIGRATION = """SELECT * FROM migrations WHERE name='{name}'"""
REMEMBER_MIGRATION = """INSERT INTO migrations (name) VALUES ('{name}')"""
def get_migrations_list():
""" Return sorted list with files in migrations directory """
files = listdir(MIGRATION_DIRECTORY)
return sorted(files)
def get_migration(name):
""" Return text of the migration """
migration_path = path.join(MIGRATION_DIRECTORY, name)
with open(migration_path, 'r') as file:
return file.read()
async def migrate(conn, name):
""" Applying migration by name """
print('{}...'.format(name), end='\t')
script = get_migration(name)
async with conn.cursor() as cursor:
await cursor.execute(FIND_MIGRATION.format(name=name))
result = await cursor.fetchall()
if result:
print('skiped')
return
try:
await cursor.execute(script)
except Exception as err:
print('error!')
print(err)
raise SystemExit(1)
else:
print('ok')
await cursor.execute(REMEMBER_MIGRATION.format(name=name))
async def main(args):
""" Main function """
options = {
'database': args.postgres_database,
'user': args.postgres_user,
'host': args.postgres_host,
'port': args.postgres_port
}
if args.postgres_pass:
options['password'] = args.postgres_pass
if args.command == 'migrate':
conn = await aiopg.connect(**options)
async with conn.cursor() as cursor:
print('Initialization...', end='\t')
await cursor.execute(INITIAL_MIGRATION)
print('ok')
print('Migrating...')
for name in get_migrations_list():
await migrate(conn, name)
await conn.close()
if __name__ == "__main__":
PARSER = ArgumentParser()
PARSER.description = 'CLI for applying migration'
PARSER.prog = 'python3 cli.py'
PARSER.add_argument(
'--postgres-pass',
dest='postgres_pass',
type=str,
help='PostgreSQL user password',
default=None
)
PARSER.add_argument(
'--postgres-user',
dest='postgres_user',
type=str,
help='PostgreSQL user name',
default='postgres'
)
PARSER.add_argument(
'--postgres-host',
dest='postgres_host',
type=str,
help='PostgreSQL database host',
default='localhost'
)
PARSER.add_argument(
'--postgres-port',
dest='postgres_port',
type=int,
help='PostgreSQL database port',
default=5432
)
PARSER.add_argument(
'--postgres-database',
dest='postgres_database',
type=str,
help='PostgreSQL database name',
default='donkey-engine'
)
SUBPARSER = PARSER.add_subparsers(help='Command', dest='command')
SUBPARSER.add_parser(
'migrate',
help='Apply all migrations'
)
ARGS = PARSER.parse_args()
if not ARGS.command:
PARSER.print_help()
raise SystemExit()
LOOP = asyncio.get_event_loop()
LOOP.run_until_complete(main(ARGS))