This repository was archived by the owner on Nov 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdb_manage.py
More file actions
83 lines (70 loc) · 3 KB
/
Copy pathdb_manage.py
File metadata and controls
83 lines (70 loc) · 3 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
""" Wrapper over the command line migrate tool to better work with
config files. """
#To test: python -m assembl.scripts.db_manage
import argparse
import subprocess
import sys
import os
import time
from os.path import join, dirname, exists
from pyramid.paster import get_appsettings
import transaction
from sqlalchemy import create_engine as create_engine_sqla
from alembic.migration import MigrationContext
from ..lib.migration import bootstrap_db, bootstrap_db_data
from ..lib.sqla import configure_engine, mark_changed
from ..lib.zmqlib import configure_zmq
from ..indexing.changes import configure_indexing
from ..lib.config import set_config
from sqlalchemy.orm import sessionmaker
def main():
parser = argparse.ArgumentParser(description="Manage database bootstrap, backup and update.")
parser.add_argument("configuration", help="configuration file")
parser.add_argument("command", help="command", choices=['bootstrap', 'backup', 'alembic'])
parser.add_argument('alembic_args', nargs='*')
args = parser.parse_args()
settings = get_appsettings(args.configuration, 'assembl')
set_config(settings)
configure_zmq(settings['changes_socket'], False)
configure_indexing()
engine = configure_engine(settings, True)
if args.command == "bootstrap":
db = bootstrap_db(args.configuration)
# Commit data separately; otherwise, postgres blocks on trying
# creating the vocabularies' enums while the first creation
# above is uncommitted. (i.e. checking that the enum already
# exists fails.)
bootstrap_db_data(db)
elif args.command == "backup":
projectpath = dirname(dirname(dirname(__file__)))
dbdumps_dir = join(projectpath, "assembl_dumps")
if not exists(dbdumps_dir):
subprocess.check_call('mkdir -m700 ' + dbdumps_dir)
filename = 'db_%s.sql.pgdump' % time.strftime('%Y%m%d')
file_path = join(dbdumps_dir, filename)
pg_dump_path = subprocess.check_output(['which', 'pg_dump']).strip()
# Dump
subprocess.check_call(
[
pg_dump_path,
'--host=' + settings['db_host'],
'-U' + settings['db_user'],
'--format=custom',
'-b', settings['db_database'],
'-f', file_path],
env={"PGPASSWORD": settings['db_password']})
# Make symlink to latest
subprocess.check_call([
'ln', '-sf', file_path, 'assembl-backup.pgdump'])
elif args.command == "alembic":
context = MigrationContext.configure(engine.connect())
db_version = context.get_current_revision()
if not db_version:
sys.stderr.write('Database not initialized.\n'
'Try this: "assembl-db-manage %s bootstrap"\n'
% args.configuration)
sys.exit(2)
cmd = ['alembic', '-c', args.configuration] + args.alembic_args
print(subprocess.check_output(cmd))
if __name__ == '__main__':
main()