forked from metabrainz/acousticbrainz-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
157 lines (119 loc) · 5.24 KB
/
manage.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from __future__ import print_function
import db
import db.cache
import db.dump
import db.stats
import db.dump_manage
from webserver import create_app
import subprocess
import os
import click
import config
ADMIN_SQL_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'admin', 'sql')
cli = click.Group()
@cli.command()
@click.option("--host", "-h", default="0.0.0.0", show_default=True)
@click.option("--port", "-p", default=8080, show_default=True)
@click.option("--debug", "-d", type=bool,
help="Turns debugging mode on or off. If specified, overrides "
"'DEBUG' value in the config file.")
def runserver(host, port, debug):
create_app().run(host=host, port=port, debug=debug,
extra_files=config.RELOAD_ON_FILES)
def _run_psql(script, database=None):
script = os.path.join(ADMIN_SQL_DIR, script)
command = ['psql', '-p', config.PG_PORT, '-U', config.PG_SUPER_USER, '-f', script]
if database:
command.extend(['-d', database])
exit_code = subprocess.call(command)
return exit_code
@cli.command()
@click.option("--force", "-f", is_flag=True, help="Drop existing database and user.")
@click.argument("archive", type=click.Path(exists=True), required=False)
def init_db(archive, force):
"""Initializes database and imports data if needed.
This process involves several steps:
1. Table structure is created.
2. Data is imported from the archive if it is specified.
3. Primary keys and foreign keys are created.
4. Indexes are created.
Data dump needs to be a .tar.xz archive produced by export command.
More information about populating a PostgreSQL database efficiently can be
found at http://www.postgresql.org/docs/current/static/populate.html.
"""
if force:
exit_code = _run_psql('drop_db.sql')
if exit_code != 0:
raise Exception('Failed to drop existing database and user! Exit code: %i' % exit_code)
print('Creating user and a database...')
exit_code = _run_psql('create_db.sql')
if exit_code != 0:
raise Exception('Failed to create new database and user! Exit code: %i' % exit_code)
print('Creating database extensions...')
exit_code = _run_psql('create_extensions.sql', 'acousticbrainz')
if exit_code != 0:
raise Exception('Failed to create database extensions! Exit code: %i' % exit_code)
db.init_db_engine(config.SQLALCHEMY_DATABASE_URI)
print('Creating types...')
db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_types.sql'))
print('Creating tables...')
db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_tables.sql'))
if archive:
print('Importing data...')
db.dump.import_db_dump(archive)
else:
print('Skipping data importing.')
print('Creating primary and foreign keys...')
db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_primary_keys.sql'))
db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_foreign_keys.sql'))
print('Creating indexes...')
db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_indexes.sql'))
print("Done!")
@cli.command()
@click.option("--force", "-f", is_flag=True, help="Drop existing database and user.")
def init_test_db(force=False):
"""Same as `init_db` command, but creates a database that will be used to
run tests and doesn't import data (no need to do that).
`SQLALCHEMY_TEST_URI` must be defined in the config file.
"""
if force:
exit_code = _run_psql('drop_test_db.sql')
if exit_code != 0:
raise Exception('Failed to drop existing database and user! Exit code: %i' % exit_code)
print('Creating database and user for testing...')
exit_code = _run_psql('create_test_db.sql')
if exit_code != 0:
raise Exception('Failed to create new database and user! Exit code: %i' % exit_code)
exit_code = _run_psql('create_extensions.sql', 'ab_test')
if exit_code != 0:
raise Exception('Failed to create database extensions! Exit code: %i' % exit_code)
db.init_db_engine(config.SQLALCHEMY_TEST_URI)
db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_types.sql'))
db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_tables.sql'))
db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_primary_keys.sql'))
db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_foreign_keys.sql'))
db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_indexes.sql'))
print("Done!")
@cli.command()
@click.argument("archive", type=click.Path(exists=True))
def import_data(archive):
"""Imports data dump into the database."""
db.init_db_engine(config.SQLALCHEMY_DATABASE_URI)
print('Importing data...')
db.dump.import_db_dump(archive)
cli.add_command(db.dump_manage.cli, name="dump")
@cli.command()
def compute_stats():
"""Compute any outstanding hourly stats and add to the database."""
db.init_db_engine(config.SQLALCHEMY_DATABASE_URI)
import datetime
import pytz
db.stats.compute_stats(datetime.datetime.now(pytz.utc))
@cli.command()
def cache_stats():
"""Compute recent stats and add to memcache."""
db.init_db_engine(config.SQLALCHEMY_DATABASE_URI)
db.cache.init(config.MEMCACHED_SERVERS)
db.stats.add_stats_to_cache()
if __name__ == '__main__':
cli()