##// END OF EJS Templates
db: ensure migrations are executed and steps are tested
marcink -
r2653:1c433ba9 default
parent child Browse files
Show More
@@ -126,7 +126,7 b' class DbManage(object):'
126 log.info("Deleting (%s) cache keys now...", total)
126 log.info("Deleting (%s) cache keys now...", total)
127 CacheKey.delete_all_cache()
127 CacheKey.delete_all_cache()
128
128
129 def upgrade(self):
129 def upgrade(self, version=None):
130 """
130 """
131 Upgrades given database schema to given revision following
131 Upgrades given database schema to given revision following
132 all needed steps, to perform the upgrade
132 all needed steps, to perform the upgrade
@@ -157,9 +157,9 b' class DbManage(object):'
157 db_uri = self.dburi
157 db_uri = self.dburi
158
158
159 try:
159 try:
160 curr_version = api.db_version(db_uri, repository_path)
160 curr_version = version or api.db_version(db_uri, repository_path)
161 msg = ('Found current database under version '
161 msg = ('Found current database db_uri under version '
162 'control with version %s' % curr_version)
162 'control with version {}'.format(curr_version))
163
163
164 except (RuntimeError, DatabaseNotControlledError):
164 except (RuntimeError, DatabaseNotControlledError):
165 curr_version = 1
165 curr_version = 1
@@ -86,7 +86,8 b' class DBBackend(object):'
86 _store = os.path.dirname(os.path.abspath(__file__))
86 _store = os.path.dirname(os.path.abspath(__file__))
87 _type = None
87 _type = None
88 _base_ini_config = [{'app:main': {'vcs.start_server': 'false',
88 _base_ini_config = [{'app:main': {'vcs.start_server': 'false',
89 'startup.import_repos': 'false'}}]
89 'startup.import_repos': 'false',
90 'is_test': 'False'}}]
90 _db_url = [{'app:main': {'sqlalchemy.db1.url': ''}}]
91 _db_url = [{'app:main': {'sqlalchemy.db1.url': ''}}]
91 _base_db_name = 'rhodecode_test_db_backend'
92 _base_db_name = 'rhodecode_test_db_backend'
92
93
@@ -152,6 +153,9 b' class DBBackend(object):'
152 print(self.stderr)
153 print(self.stderr)
153 raise AssertionError('non 0 retcode:{}'.format(self.p.returncode))
154 raise AssertionError('non 0 retcode:{}'.format(self.p.returncode))
154
155
156 def assert_correct_output(self, stdout, version):
157 assert 'UPGRADE FOR STEP {} COMPLETED'.format(version) in stdout
158
155 def setup_rhodecode_db(self, ini_params=None, env=None):
159 def setup_rhodecode_db(self, ini_params=None, env=None):
156 if not ini_params:
160 if not ini_params:
157 ini_params = self._base_ini_config
161 ini_params = self._base_ini_config
@@ -167,7 +171,7 b' class DBBackend(object):'
167 if not os.path.isdir(self._repos_git_lfs_store):
171 if not os.path.isdir(self._repos_git_lfs_store):
168 os.makedirs(self._repos_git_lfs_store)
172 os.makedirs(self._repos_git_lfs_store)
169
173
170 self.execute(
174 return self.execute(
171 "rc-setup-app {0} --user=marcink "
175 "rc-setup-app {0} --user=marcink "
172 "--email=marcin@rhodeocode.com --password={1} "
176 "--email=marcin@rhodeocode.com --password={1} "
173 "--repos={2} --force-yes".format(
177 "--repos={2} --force-yes".format(
@@ -183,7 +187,8 b' class DBBackend(object):'
183 with test_ini as ini_file:
187 with test_ini as ini_file:
184 if not os.path.isdir(self._repos_location):
188 if not os.path.isdir(self._repos_location):
185 os.makedirs(self._repos_location)
189 os.makedirs(self._repos_location)
186 self.execute(
190
191 return self.execute(
187 "rc-upgrade-db {0} --force-yes".format(ini_file))
192 "rc-upgrade-db {0} --force-yes".format(ini_file))
188
193
189 def setup_db(self):
194 def setup_db(self):
@@ -226,12 +231,11 b' class SQLiteDBBackend(DBBackend):'
226
231
227 def import_dump(self, dumpname):
232 def import_dump(self, dumpname):
228 dump = os.path.join(self.fixture_store, dumpname)
233 dump = os.path.join(self.fixture_store, dumpname)
229 shutil.copy(
234 target = os.path.join(self._basetemp, '{0.db_name}.sqlite'.format(self))
230 dump,
235 return self.execute('cp -v {} {}'.format(dump, target))
231 os.path.join(self._basetemp, '{0.db_name}.sqlite'.format(self)))
232
236
233 def teardown_db(self):
237 def teardown_db(self):
234 self.execute("rm -rf {}.sqlite".format(
238 return self.execute("rm -rf {}.sqlite".format(
235 os.path.join(self._basetemp, self.db_name)))
239 os.path.join(self._basetemp, self.db_name)))
236
240
237
241
@@ -246,16 +250,16 b' class MySQLDBBackend(DBBackend):'
246 # mysqldump -uroot -pqweqwe $TEST_DB_NAME
250 # mysqldump -uroot -pqweqwe $TEST_DB_NAME
247 self._db_url = [{'app:main': {
251 self._db_url = [{'app:main': {
248 'sqlalchemy.db1.url': self.connection_string}}]
252 'sqlalchemy.db1.url': self.connection_string}}]
249 self.execute("mysql -v -u{} -p{} -e 'create database '{}';'".format(
253 return self.execute("mysql -v -u{} -p{} -e 'create database '{}';'".format(
250 self.user, self.password, self.db_name))
254 self.user, self.password, self.db_name))
251
255
252 def import_dump(self, dumpname):
256 def import_dump(self, dumpname):
253 dump = os.path.join(self.fixture_store, dumpname)
257 dump = os.path.join(self.fixture_store, dumpname)
254 self.execute("mysql -u{} -p{} {} < {}".format(
258 return self.execute("mysql -u{} -p{} {} < {}".format(
255 self.user, self.password, self.db_name, dump))
259 self.user, self.password, self.db_name, dump))
256
260
257 def teardown_db(self):
261 def teardown_db(self):
258 self.execute("mysql -v -u{} -p{} -e 'drop database '{}';'".format(
262 return self.execute("mysql -v -u{} -p{} -e 'drop database '{}';'".format(
259 self.user, self.password, self.db_name))
263 self.user, self.password, self.db_name))
260
264
261
265
@@ -271,18 +275,18 b' class PostgresDBBackend(DBBackend):'
271 self._db_url = [{'app:main': {
275 self._db_url = [{'app:main': {
272 'sqlalchemy.db1.url':
276 'sqlalchemy.db1.url':
273 self.connection_string}}]
277 self.connection_string}}]
274 self.execute("PGPASSWORD={} psql -U {} -h localhost "
278 return self.execute("PGPASSWORD={} psql -U {} -h localhost "
275 "-c 'create database '{}';'".format(
279 "-c 'create database '{}';'".format(
276 self.password, self.user, self.db_name))
280 self.password, self.user, self.db_name))
277
281
278 def teardown_db(self):
282 def teardown_db(self):
279 self.execute("PGPASSWORD={} psql -U {} -h localhost "
283 return self.execute("PGPASSWORD={} psql -U {} -h localhost "
280 "-c 'drop database if exists '{}';'".format(
284 "-c 'drop database if exists '{}';'".format(
281 self.password, self.user, self.db_name))
285 self.password, self.user, self.db_name))
282
286
283 def import_dump(self, dumpname):
287 def import_dump(self, dumpname):
284 dump = os.path.join(self.fixture_store, dumpname)
288 dump = os.path.join(self.fixture_store, dumpname)
285 self.execute(
289 return self.execute(
286 "PGPASSWORD={} psql -U {} -h localhost -d {} -1 "
290 "PGPASSWORD={} psql -U {} -h localhost -d {} -1 "
287 "-f {}".format(
291 "-f {}".format(
288 self.password, self.user, self.db_name, dump))
292 self.password, self.user, self.db_name, dump))
@@ -59,5 +59,7 b' def _run_migration_test(db_backend, dump'
59 db_backend.assert_returncode_success()
59 db_backend.assert_returncode_success()
60
60
61 db_backend.import_dump(dumpname)
61 db_backend.import_dump(dumpname)
62 db_backend.upgrade_database()
62 stdout, stderr = db_backend.upgrade_database()
63
64 db_backend.assert_correct_output(stdout+stderr, version='16')
63 db_backend.assert_returncode_success()
65 db_backend.assert_returncode_success()
General Comments 0
You need to be logged in to leave comments. Login now