|
a |
|
b/scripts/migrate_project_database.py |
|
|
1 |
import os
|
|
|
2 |
import sys
|
|
|
3 |
import shutil
|
|
|
4 |
import logging
|
|
|
5 |
|
|
|
6 |
from pylons import c
|
|
|
7 |
from ming.orm import session
|
|
|
8 |
from allura import model as M
|
|
|
9 |
|
|
|
10 |
log = logging.getLogger(__name__)
|
|
|
11 |
|
|
|
12 |
MONGO_HOME=os.environ.get('MONGO_HOME', '/usr')
|
|
|
13 |
MONGO_DUMP=os.path.join(MONGO_HOME, 'bin/mongodump')
|
|
|
14 |
MONGO_RESTORE=os.path.join(MONGO_HOME, 'bin/mongorestore')
|
|
|
15 |
|
|
|
16 |
def main():
|
|
|
17 |
if len(sys.argv) > 1:
|
|
|
18 |
shortnames = sys.argv[1:]
|
|
|
19 |
else:
|
|
|
20 |
shortnames = [ p.shortname for p in M.Project.query.find(dict(is_root=True)) ]
|
|
|
21 |
for pname in shortnames:
|
|
|
22 |
migrate_project_database(pname)
|
|
|
23 |
|
|
|
24 |
def migrate_project_database(pname):
|
|
|
25 |
c.project = project = M.Project.query.get(shortname=pname)
|
|
|
26 |
if project is None:
|
|
|
27 |
log.fatal('Project %s not found', pname)
|
|
|
28 |
print 'Project %s not found' % pname
|
|
|
29 |
return 2
|
|
|
30 |
if project.database_uri is not None:
|
|
|
31 |
log.fatal('Project %s is already migrated to %s', pname, project.database_uri)
|
|
|
32 |
print 'Project %s already migrated to %s' % (pname, project.database_uri)
|
|
|
33 |
return 2
|
|
|
34 |
M.ProjectRole.query.update(dict(project_id=None), dict(project_id=c.project._id))
|
|
|
35 |
conn = M.session.main_doc_session.db.connection
|
|
|
36 |
host = '%s:%s' % (conn.host, conn.port)
|
|
|
37 |
dirname = os.tempnam()
|
|
|
38 |
try:
|
|
|
39 |
log.info('Backing up %s to %s', pname, dirname)
|
|
|
40 |
assert 0 == os.system('%s --host %s --db %s -o %s' % (
|
|
|
41 |
MONGO_DUMP, host, project.database, dirname))
|
|
|
42 |
assert 0 == os.system('%s --host %s --db project-data %s/%s ' % (
|
|
|
43 |
MONGO_RESTORE, host, dirname, project.database))
|
|
|
44 |
database = project.database
|
|
|
45 |
for p in M.Project.query.find(dict(database=database)):
|
|
|
46 |
p.database = ''
|
|
|
47 |
p.database_uri = 'ming://%s/project-data' % host
|
|
|
48 |
project.ensure_project_indexes()
|
|
|
49 |
session(project).flush()
|
|
|
50 |
conn.drop_database(database)
|
|
|
51 |
finally:
|
|
|
52 |
if os.path.exists(dirname):
|
|
|
53 |
shutil.rmtree(dirname)
|
|
|
54 |
return 0
|
|
|
55 |
|
|
|
56 |
def pm(etype, value, tb): # pragma no cover
|
|
|
57 |
import pdb, traceback
|
|
|
58 |
try:
|
|
|
59 |
from IPython.ipapi import make_session; make_session()
|
|
|
60 |
from IPython.Debugger import Pdb
|
|
|
61 |
sys.stderr.write('Entering post-mortem IPDB shell\n')
|
|
|
62 |
p = Pdb(color_scheme='Linux')
|
|
|
63 |
p.reset()
|
|
|
64 |
p.setup(None, tb)
|
|
|
65 |
p.print_stack_trace()
|
|
|
66 |
sys.stderr.write('%s: %s\n' % ( etype, value))
|
|
|
67 |
p.cmdloop()
|
|
|
68 |
p.forget()
|
|
|
69 |
# p.interaction(None, tb)
|
|
|
70 |
except ImportError:
|
|
|
71 |
sys.stderr.write('Entering post-mortem PDB shell\n')
|
|
|
72 |
traceback.print_exception(etype, value, tb)
|
|
|
73 |
pdb.post_mortem(tb)
|
|
|
74 |
|
|
|
75 |
sys.excepthook = pm
|
|
|
76 |
|
|
|
77 |
if __name__ == '__main__':
|
|
|
78 |
sys.exit(main())
|