diff --git a/migrate/versioning/api.py b/migrate/versioning/api.py index e65d7d5..3bca613 100644 --- a/migrate/versioning/api.py +++ b/migrate/versioning/api.py @@ -318,10 +318,11 @@ def update_db_from_model(url,model,repository,**opts): """%prog update_db_from_model URL MODEL REPOSITORY_PATH Modify the database to match the structure of the current Python model. + This also sets the db_version number to the latest in the repository. NOTE: This is EXPERIMENTAL. """ # TODO: get rid of EXPERIMENTAL label engine=create_engine(url) - cls_schema.update_db_from_model(engine,model,repository) - + schema = cls_schema(engine,repository) + schema.update_db_from_model(model) diff --git a/migrate/versioning/schema.py b/migrate/versioning/schema.py index 8045f07..7dc3643 100644 --- a/migrate/versioning/schema.py +++ b/migrate/versioning/schema.py @@ -112,15 +112,16 @@ class ControlledSchema(object): diff = schemadiff.getDiffOfModelAgainstDatabase(MetaData(), engine, excludeTables=[repository.version_table]) return genmodel.ModelGenerator(diff).toPython() - @classmethod - def update_db_from_model(cls,engine,model,repository): + def update_db_from_model(self,model): """Modify the database to match the structure of the current Python model.""" - if isinstance(repository, basestring): - repository=Repository(repository) + if isinstance(self.repository, basestring): + self.repository=Repository(self.repository) model = loadModel(model) - diff = schemadiff.getDiffOfModelAgainstDatabase(model, engine, excludeTables=[repository.version_table]) - return genmodel.ModelGenerator(diff).applyModel() + diff = schemadiff.getDiffOfModelAgainstDatabase(model, self.engine, excludeTables=[self.repository.version_table]) + genmodel.ModelGenerator(diff).applyModel() + update = self.table.update(self.table.c.repository_id == str(self.repository.id)) + self.engine.execute(update, version=int(self.repository.latest)) def drop(self): """Remove version control from a database""" diff --git a/test/versioning/test_shell.py b/test/versioning/test_shell.py index 8e4c3db..441f5bb 100644 --- a/test/versioning/test_shell.py +++ b/test/versioning/test_shell.py @@ -466,13 +466,13 @@ class TestShellDatabase(Shell,fixture.DB): model_path = self.tmp_named('testmodel.py') # Create empty repository. + self.meta = MetaData(self.engine, reflect=True) + self.meta.drop_all() # in case junk tables are lying around in the test database self.assertSuccess(self.cmd('create',repos_path,repos_name)) self.exitcode(self.cmd('drop_version_control',self.url,repos_path)) self.assertSuccess(self.cmd('version_control',self.url,repos_path)) self.assertEquals(self.cmd_version(repos_path),0) self.assertEquals(self.cmd_db_version(self.url,repos_path),0) - self.meta = MetaData(self.engine, reflect=True) - self.meta.drop_all() # in case junk tables are lying around in the test database # Setup helper script. model_module = 'testmodel.meta' @@ -506,6 +506,8 @@ class TestShellDatabase(Shell,fixture.DB): # Update db to latest model. output, exitcode = self.output_and_exitcode('python %s update_db_from_model' % script_path) self.assertEquals(output, "") + self.assertEquals(self.cmd_version(repos_path),0) + self.assertEquals(self.cmd_db_version(self.url,repos_path),0) # version did not get bumped yet because new version not yet committed output, exitcode = self.output_and_exitcode('python %s compare_model_to_db' % script_path) self.assertEquals(output, "No schema diffs") output, exitcode = self.output_and_exitcode('python %s create_model' % script_path) @@ -550,6 +552,8 @@ class TestShellDatabase(Shell,fixture.DB): #self.assertEquals(output, "") output, exitcode = self.output_and_exitcode('python %s commit %s' % (script_path, upgrade_script_path)) self.assertEquals(output, "") + output, exitcode = self.output_and_exitcode('python %s update_db_from_model' % script_path) # bump the db_version + self.assertEquals(output, "") self.assertEquals(self.cmd_version(repos_path),1) - #self.assertEquals(self.cmd_db_version(self.url,repos_path),1) TODO finish + self.assertEquals(self.cmd_db_version(self.url,repos_path),1)