This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
commands.py
163 lines (131 loc) · 3.95 KB
/
commands.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import click
from sqlalchemy_utils import create_database, database_exists, drop_database
from .migrations import Migration
def migration(ctx):
return Migration(ctx.obj["cli"].web())
def get_db(ctx):
return ctx.obj["cli"].web()["db"]
@click.group()
def db():
"""Perform database migrations and utilities"""
pass
@db.command()
@click.pass_context
def init(ctx):
"""Creates a new migration repository."""
migration(ctx).init()
@db.command()
@click.option("-m", "--message", help="Revision message", required=True)
@click.option(
"--branch-label", help="Specify a branch label to apply to the new revision"
)
@click.pass_context
def migrate(ctx, message: str, branch_label: str):
"""Autogenerate a new revision file
alias for 'revision --autogenerate'
"""
return migration(ctx).revision(
message, autogenerate=True, branch_label=branch_label
)
@db.command()
@click.option("-m", "--message", help="Revision message", required=True)
@click.option(
"--branch-label", help="Specify a branch label to apply to the new revision"
)
@click.option(
"--autogenerate",
default=False,
is_flag=True,
help=(
"Populate revision script with candidate migration "
"operations, based on comparison of database to model"
),
)
@click.pass_context
def revision(ctx, message: str, branch_label: str, autogenerate: bool):
"""Autogenerate a new revision file"""
return migration(ctx).revision(
message, autogenerate=autogenerate, branch_label=branch_label
)
@db.command()
@click.option("--revision", default="heads")
@click.option(
"--drop-tables",
default=False,
is_flag=True,
help="Drop tables before applying migrations",
)
@click.pass_context
def upgrade(ctx, revision: str, drop_tables: bool):
"""Upgrade to a later version"""
if drop_tables:
_drop_tables(ctx)
migration(ctx).upgrade(revision)
click.echo(f"upgraded successfully to {revision}")
@db.command()
@click.option("--revision", help="Revision id", required=True)
@click.pass_context
def downgrade(ctx, revision: str):
"""Downgrade to a previous version"""
migration(ctx).downgrade(revision)
click.echo(f"downgraded successfully to {revision}")
@db.command()
@click.option("--revision", default="heads")
@click.pass_context
def show(ctx, revision: str):
"""Show revision ID and creation date"""
click.echo(migration(ctx).show(revision))
@db.command()
@click.pass_context
def history(ctx):
"""List changeset scripts in chronological order"""
click.echo(migration(ctx).history())
@db.command()
@click.option("--verbose/--quiet", default=False)
@click.pass_context
def current(ctx, verbose: bool):
"""Show revision ID and creation date"""
click.echo(migration(ctx).current(verbose))
@db.command()
@click.argument("dbname", nargs=1)
@click.option(
"--force", default=False, is_flag=True, help="Force removal of an existing database"
)
@click.pass_context
def create(ctx, dbname: str, force: str):
"""Creates a new database"""
engine = get_db(ctx).sync_engine
url = engine.url.set(database=dbname)
store = str(url)
if database_exists(store):
if force:
drop_database(store)
else:
return click.echo(f"database {dbname} already available")
create_database(store)
click.echo(f"database {dbname} created")
@db.command()
@click.option(
"--db",
default=False,
is_flag=True,
help="List tables in database rather than in sqlalchemy metadata",
)
@click.pass_context
def tables(ctx, db):
"""List all tables managed by the app"""
d = get_db(ctx)
if db:
tables = d.sync_engine.table_names()
else:
tables = d.metadata.tables
for name in sorted(tables):
click.echo(name)
@db.command()
@click.pass_context
def drop(ctx):
"""Drop all tables in database"""
_drop_tables(ctx)
def _drop_tables(ctx):
get_db(ctx).drop_all_schemas()
click.echo("tables dropped")