forked from google/python-spanner-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.py
More file actions
81 lines (65 loc) · 2.92 KB
/
scripts.py
File metadata and controls
81 lines (65 loc) · 2.92 KB
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
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Entry point for spanner_orm scripts."""
import argparse
from typing import Any
from spanner_orm import api
from spanner_orm.admin import migration_executor
from spanner_orm.admin import migration_manager
def generate(args: Any) -> None:
manager = migration_manager.MigrationManager(args.directory)
manager.generate(args.name)
def migrate(args: Any) -> None:
connection = api.SpannerConnection(args.instance, args.database)
executor = migration_executor.MigrationExecutor(connection, args.directory)
executor.migrate(args.name)
def rollback(args: Any) -> None:
connection = api.SpannerConnection(args.instance, args.database)
executor = migration_executor.MigrationExecutor(connection, args.directory)
executor.rollback(args.name)
def main(as_module: bool = False) -> None:
prog = 'spanner-orm' if as_module else None
parser = argparse.ArgumentParser(prog=prog)
# 'subcommand' is actually required, but required subparsers are not supported
# for Python < 3.7.
subparsers = parser.add_subparsers(
dest='subcommand', title='subcommands', description='valid subcommands')
generate_parser = subparsers.add_parser(
'generate', help='Generate a new migration')
generate_parser.add_argument('name', help='Short name of the migration')
generate_parser.add_argument('--directory')
generate_parser.set_defaults(execute=generate)
migrate_parser = subparsers.add_parser(
'migrate', help='Execute unmigrated migrations')
migrate_parser.add_argument(
'--name', help='Stop migrating after this migration')
migrate_parser.add_argument('--directory')
migrate_parser.add_argument('instance', help='Name of Spanner instance')
migrate_parser.add_argument('database', help='Name of Spanner database')
migrate_parser.set_defaults(execute=migrate)
rollback_parser = subparsers.add_parser(
'rollback', help='Roll back migrated migrations')
rollback_parser.add_argument(
'name', help='Keep rolling back past this migration')
rollback_parser.add_argument('--directory')
rollback_parser.add_argument('instance', help='Name of Spanner instance')
rollback_parser.add_argument('database', help='Name of Spanner database')
rollback_parser.set_defaults(execute=rollback)
args = parser.parse_args()
if args.subcommand is None:
parser.print_help()
else:
args.execute(args)
if __name__ == '__main__':
main(as_module=True)