initializedb.py
1.7 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
import os
import transaction
import sys
import subprocess
from sqlalchemy import (
engine_from_config,
)
from pyramid.paster import (
get_appsettings,
setup_logging,
)
from opensipkd.base.models import (DBSession as ModuleDBSession, Base as ModuleBase)
from opensipkd.base.scripts.initializedb import append_csv, read_file, reset_sequences, create_schema
from opensipkd.base.models import Route
def get_file(filename):
base_dir = os.path.split(__file__)[0]
fullpath = os.path.join(base_dir, 'data', filename)
return open(fullpath)
def usage(argv):
pass
def main(argv=sys.argv):
def alembic_run(ini_file, url):
s = read_file(ini_file)
s = s.replace('{{db_url}}', url)
f = open('alembic.ini', 'w')
f.write(s)
f.close()
subprocess.call(command)
os.remove('alembic.ini')
if len(argv) != 2:
usage(argv)
config_uri = argv[1]
setup_logging(config_uri)
settings = get_appsettings(config_uri)
bin_path = os.path.split(sys.executable)[0]
alembic_bin = os.path.join(bin_path, 'alembic')
command = (alembic_bin, 'upgrade', 'head')
alembic_run('alembic.ini.tpl', settings['sqlalchemy.url'])
alembic_run('alembic_upgrade.ini.tpl', settings['sqlalchemy.url'])
engine = engine_from_config(settings, 'sqlalchemy.')
ModuleDBSession.configure(bind=engine)
# init_model()
ModuleBase.metadata.bind = engine
# create_schema(engine, 'pasar')
ModuleBase.metadata.create_all()
reset_sequences()
print('>>Append Table')
append_csv(Route, 'routes.csv', ['kode'], get_file_func=get_file, update_exist=True)
transaction.commit()
print('>>Appended')
print('****API CREATED****')