-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
executable file
·85 lines (64 loc) · 2.48 KB
/
deploy.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
#!/usr/bin/env python
import os
os.environ.setdefault('PLANTMONITOR_MODULE_SETTINGS', 'conf.settings.devel')
import datetime
import click
from yamlns import namespace as ns
from conf.logging_configuration import LOGGING
import logging
import logging.config
logging.config.dictConfig(LOGGING)
logger = logging.getLogger("plantmonitor")
from ORM.pony_manager import PonyManager
from ORM.db_utils import setupDatabase, dropTables
from ORM.migrations import migrateLegacyToPony
from pony import orm
from meteologica.plantmonitor_db import (
PlantmonitorDB,
PlantmonitorDBError,
)
# ```
# psql -U postgres -h localhost
# CREATE database plants;
# \c plants
# CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
# ```
def legacyMigrate(skipList=[]):
configdbns = ns.load('conf/configlegacydb.yaml')
migrateLegacyToPony(configdbns, skipList=skipList)
@click.command()
@click.option('--generate/--nogenerate', default=True, help='Regenerate mapping')
@click.option('--create/--nocreate', default=True, help='Regenerate tables')
@click.option('--destroy/--nodestroy', default=False, help='Drop tables')
@click.option('--timescale/--notimescale', default=True, help='Timescale the Registry tables.')
@click.option('--migrate/--nomigrate', default=False, help='Run the migration scripts using conf/configdb.yaml')
@click.option('--sqldebug/--nosqldebug', default=False, help='print all sql issued by the ORM')
@click.option('--noplants/--plants', default=False, help='create plants')
@click.option('--noinverters/--inverters', default=False, help='migrate inverters')
@click.option('--nometers/--meters', default=False, help='migrate meters')
@click.option('--nosensors/--sensors', default=False, help='migrate sensors')
def deployDatabase(generate, create, destroy, timescale, migrate, sqldebug, noplants, noinverters, nometers, nosensors):
if sqldebug:
orm.set_sql_debug(True)
if destroy:
dropTables()
skipList = []
if noplants:
skipList += ['plants']
if noinverters:
skipList += ['inverters']
if nometers:
skipList += ['meters']
if nosensors:
skipList += ['sensors']
if generate:
print('database setup')
setupDatabase(create_tables=create, timescale_tables=timescale, drop_tables=destroy)
with orm.db_session:
dsn = database.get_connection().dsn
print(dsn)
print('database created')
if migrate:
legacyMigrate(skipList)
if __name__ == "__main__":
deployDatabase()