37
37
def get_engine (database = 'main' , context = None ):
38
38
if database == 'main' :
39
39
return db_session .get_engine (context = context )
40
+
40
41
if database == 'api' :
41
42
return db_session .get_api_engine ()
42
43
43
44
45
+ def find_migrate_repo (database = 'main' ):
46
+ """Get the path for the migrate repository."""
47
+ global _REPOSITORY
48
+ rel_path = 'migrate_repo'
49
+ if database == 'api' :
50
+ rel_path = os .path .join ('api_migrations' , 'migrate_repo' )
51
+ path = os .path .join (os .path .abspath (os .path .dirname (__file__ )), rel_path )
52
+ assert os .path .exists (path )
53
+ if _REPOSITORY .get (database ) is None :
54
+ _REPOSITORY [database ] = Repository (path )
55
+ return _REPOSITORY [database ]
56
+
57
+
44
58
def db_sync (version = None , database = 'main' , context = None ):
45
59
"""Migrate the database to `version` or the most recent version."""
46
60
if version is not None :
@@ -50,18 +64,17 @@ def db_sync(version=None, database='main', context=None):
50
64
raise exception .NovaException (_ ("version should be an integer" ))
51
65
52
66
current_version = db_version (database , context = context )
53
- repository = _find_migrate_repo (database )
67
+ repository = find_migrate_repo (database )
68
+ engine = get_engine (database , context = context )
54
69
if version is None or version > current_version :
55
- return versioning_api .upgrade (get_engine (database , context = context ),
56
- repository , version )
70
+ return versioning_api .upgrade (engine , repository , version )
57
71
else :
58
- return versioning_api .downgrade (get_engine (database , context = context ),
59
- repository , version )
72
+ return versioning_api .downgrade (engine , repository , version )
60
73
61
74
62
75
def db_version (database = 'main' , context = None ):
63
76
"""Display the current database version."""
64
- repository = _find_migrate_repo (database )
77
+ repository = find_migrate_repo (database )
65
78
66
79
# NOTE(mdbooth): This is a crude workaround for races in _db_version. The 2
67
80
# races we have seen in practise are:
@@ -96,20 +109,17 @@ def db_version(database='main', context=None):
96
109
97
110
98
111
def _db_version (repository , database , context ):
112
+ engine = get_engine (database , context = context )
99
113
try :
100
- return versioning_api .db_version (get_engine (database , context = context ),
101
- repository )
114
+ return versioning_api .db_version (engine , repository )
102
115
except versioning_exceptions .DatabaseNotControlledError as exc :
103
116
meta = sqlalchemy .MetaData ()
104
- engine = get_engine (database , context = context )
105
117
meta .reflect (bind = engine )
106
118
tables = meta .tables
107
119
if len (tables ) == 0 :
108
- db_version_control (INIT_VERSION [database ],
109
- database ,
110
- context = context )
111
- return versioning_api .db_version (
112
- get_engine (database , context = context ), repository )
120
+ db_version_control (
121
+ INIT_VERSION [database ], database , context = context )
122
+ return versioning_api .db_version (engine , repository )
113
123
else :
114
124
LOG .exception (exc )
115
125
# Some pre-Essex DB's may not be version controlled.
@@ -124,22 +134,7 @@ def db_initial_version(database='main'):
124
134
125
135
126
136
def db_version_control (version = None , database = 'main' , context = None ):
127
- repository = _find_migrate_repo (database )
128
- versioning_api .version_control (get_engine (database , context = context ),
129
- repository ,
130
- version )
137
+ repository = find_migrate_repo (database )
138
+ engine = get_engine (database , context = context )
139
+ versioning_api .version_control (engine , repository , version )
131
140
return version
132
-
133
-
134
- def _find_migrate_repo (database = 'main' ):
135
- """Get the path for the migrate repository."""
136
- global _REPOSITORY
137
- rel_path = 'migrate_repo'
138
- if database == 'api' :
139
- rel_path = os .path .join ('api_migrations' , 'migrate_repo' )
140
- path = os .path .join (os .path .abspath (os .path .dirname (__file__ )),
141
- rel_path )
142
- assert os .path .exists (path )
143
- if _REPOSITORY .get (database ) is None :
144
- _REPOSITORY [database ] = Repository (path )
145
- return _REPOSITORY [database ]
0 commit comments