initializedb.py
2.81 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
82
83
84
85
86
87
import os
import transaction
import sys
import subprocess
import logging
from tangsel.base import Base
from sqlalchemy import engine_from_config
from pyramid.paster import (get_appsettings, setup_logging,)
# from tangsel.base.models import (DBSession as ModuleDBSession, Base as ModuleBase)
from .. import PbbDBSession as ModuleDBSession, PbbBase as ModuleBase
from tangsel.base.scripts.initializedb import append_csv, read_file, \
reset_sequences, restore_csv, alembic_run, create_schema
from tangsel.base.models import Permission, DBSession
log = logging.getLogger(__name__)
# from ..resources import *
# from ..ref import *
# from tangsel.pbb.models.payment import PbbPayment, PbbReversal
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):
print("Usage: %s <config_uri> [init|drop]" % argv[0])
sys.exit(1)
def main(argv=sys.argv):
if len(argv) < 2 or len(argv) > 3:
usage(argv)
config_uri = argv[1]
setup_logging(config_uri)
settings = get_appsettings(config_uri)
bin_path = os.path.split(sys.executable)[0]
if "lib_dir" in settings and settings["lib_dir"]:
lib_dir = rf"{settings['lib_dir']}"
import cx_Oracle
cx_Oracle.init_oracle_client(lib_dir=lib_dir)
log.debug(lib_dir)
pbb_engine = engine_from_config(settings, 'pbb.')
ModuleDBSession.configure(bind=pbb_engine)
ModuleBase.metadata.bind = pbb_engine
if len(argv)>2 and len(argv)<4:
create_schema(pbb_engine, "pbb")
if argv[2] == 'drop':
print('****DROP PBB Models****')
ModuleBase.metadata.drop_all(pbb_engine, )
print('****PBB Models DROPED****')
elif argv[2] == 'init':
print('****INIT PBB Models****')
ModuleBase.metadata.create_all(pbb_engine)
print('****PBB Models CREATED****')
# ModuleBase.metadata.create_all()
# init_model()
# reset_sequences()
return
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
Base.metadata.create_all(bind=engine)
# ModuleBase.metadata.create_all(pbb_engine)
# alembic_run(config_uri, 'alembic_pbb')
print('>>Append Table')
# append_csv(Route, 'routes.csv', ['kode'], get_file_func=get_file, update_exist=False)
# append_csv(Permission, 'permissions.csv', ['perm_name'], get_file_func=get_file)
# restore_csv(AsetKategori, 'kategoris.csv', get_file, ModuleDBSession)
# restore_csv(Departemen, 'kode_lokasi.csv', get_file, ModuleDBSession)
# restore_csv(AsetKategori, 'kode_barang.csv', get_file, ModuleDBSession)
transaction.commit()
print('>>Appended')
print('****PBB Models CREATED****')