-
Notifications
You must be signed in to change notification settings - Fork 2k
/
db.py
72 lines (56 loc) · 1.85 KB
/
db.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
# encoding: utf-8
import logging
import click
from ckan.cli import error_shout
log = logging.getLogger(__name__)
@click.group(name=u'db', short_help=u'Database commands')
def db():
pass
@db.command(u'init', short_help=u'Initialize the database')
def initdb():
u'''Initialising the database'''
log.info(u"Initialize the Database")
try:
import ckan.model as model
model.repo.init_db()
except Exception as e:
error_shout(e)
else:
click.secho(u'Initialising DB: SUCCESS', fg=u'green', bold=True)
PROMPT_MSG = u'This will delete all of your data!\nDo you want to continue?'
@db.command(u'clean', short_help=u'Clean the database')
@click.confirmation_option(prompt=PROMPT_MSG)
def cleandb():
u'''Cleaning the database'''
try:
import ckan.model as model
model.repo.clean_db()
except Exception as e:
error_shout(e)
else:
click.secho(u'Cleaning DB: SUCCESS', fg=u'green', bold=True)
@db.command(u'upgrade', short_help=u'Upgrade the database')
@click.option(u'-v', u'--version', help=u'Migration version')
def updatedb(version=None):
u'''Upgrading the database'''
try:
import ckan.model as model
model.repo.upgrade_db(version)
except Exception as e:
error_shout(e)
else:
click.secho(u'Upgrading DB: SUCCESS', fg=u'green', bold=True)
@db.command(u'version', short_help=u'Returns current version of data schema')
def version():
u'''Return current version'''
log.info(u"Returning current DB version")
try:
from ckan.model import Session
ver = Session.execute(u'select version from '
u'migrate_version;').fetchall()
click.secho(
u"Latest data schema version: {0}".format(ver[0][0]),
bold=True
)
except Exception as e:
error_shout(e)