Repository navigation
Expand file tree
/
Copy pathppxml2db_init.py
More file actions
81 lines (64 loc) · 1.96 KB
/
Copy pathppxml2db_init.py
File metadata and controls
81 lines (64 loc) · 1.96 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
import sys
import argparse
import logging
from version import __version__
import dbhelper
SCHEMA = [
"account",
"account_attr",
"security",
"security_attr",
"security_event",
"security_prop",
"latest_price",
"price",
"watchlist",
"watchlist_security",
"xact",
"xact_unit",
"xact_cross_entry",
"taxonomy",
"taxonomy_category",
"taxonomy_data",
"taxonomy_assignment",
"taxonomy_assignment_data",
"dashboard",
"property",
"bookmark",
"attribute_type",
"config_set",
"config_entry",
]
_log = logging.getLogger(__name__)
def drop_table(table):
sql = "DROP TABLE IF EXISTS %s" % table
if args.dbtype == "pgsql":
sql += " CASCADE"
dbhelper.execute_dml(sql)
def main(args):
for table in SCHEMA:
with open(table + ".sql") as f:
sql = f.read()
if args.recreate:
drop_table(table)
if args.dbtype == "pgsql":
sql = sql.replace(
"INTEGER NOT NULL PRIMARY KEY",
"SERIAL NOT NULL PRIMARY KEY"
)
dbhelper.executescript(sql)
if __name__ == "__main__":
argp = argparse.ArgumentParser(description="Initialize database for ppxml2db")
argp.add_argument("db", help="output DB (filename/connect string)")
argp.add_argument("--dbtype", choices=("sqlite", "pgsql"), default="sqlite", help="select database type")
argp.add_argument("--recreate", action="store_true", help="delete existing tables")
argp.add_argument("--debug", action="store_true", help="enable debug logging")
argp.add_argument("--dry-run", action="store_true", help="don't commit changes to DB")
argp.add_argument("--version", action="version", version="%(prog)s " + __version__)
args = argp.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
dbhelper.init(args.dbtype, args.db)
main(args)
if not args.dry_run:
dbhelper.commit()