summaryrefslogtreecommitdiff
path: root/migrate/versioning
diff options
context:
space:
mode:
authorchristian.simms <unknown>2008-04-06 20:58:37 +0000
committerchristian.simms <unknown>2008-04-06 20:58:37 +0000
commit8af121c421f45a108894f56c8f713ae455410020 (patch)
tree0fef911c6c38f1fb8f149bb1848e0b5ecadcbf85 /migrate/versioning
parentc13931b6b911dde2b3732c23428da7be64786a9e (diff)
downloadsqalchemy-migrate-8af121c421f45a108894f56c8f713ae455410020.tar.gz
Change make_update_script_for_model shell command to compare two versions of Python model (issue #12); add shell test for new diff'ing apis
Diffstat (limited to 'migrate/versioning')
-rw-r--r--migrate/versioning/api.py10
-rw-r--r--migrate/versioning/schemadiff.py13
-rw-r--r--migrate/versioning/script/py.py9
3 files changed, 21 insertions, 11 deletions
diff --git a/migrate/versioning/api.py b/migrate/versioning/api.py
index 32ed33f..e65d7d5 100644
--- a/migrate/versioning/api.py
+++ b/migrate/versioning/api.py
@@ -301,18 +301,18 @@ def create_model(url,repository,**opts):
engine=create_engine(url)
print cls_schema.create_model(engine,repository)
-def make_update_script_for_model(path,url,model,repository,**opts):
- """%prog make_update_script_for_model PATH URL MODEL REPOSITORY_PATH
+def make_update_script_for_model(url,oldmodel,model,repository,**opts):
+ """%prog make_update_script_for_model URL OLDMODEL MODEL REPOSITORY_PATH
- Create a script changing the current (old) database to the current (new) Python model.
+ Create a script changing the old Python model to the new (current) Python model, sending to stdout.
NOTE: This is EXPERIMENTAL.
""" # TODO: get rid of EXPERIMENTAL label
engine=create_engine(url)
try:
- cls_script_python.make_update_script_for_model(path,engine,model,repository,**opts)
+ print cls_script_python.make_update_script_for_model(engine,oldmodel,model,repository,**opts)
except exceptions.PathFoundError,e:
- raise exceptions.KnownError("The path %s already exists"%e.args[0])
+ raise exceptions.KnownError("The path %s already exists"%e.args[0]) # TODO: get rid of this? if we don't add back path param
def update_db_from_model(url,model,repository,**opts):
"""%prog update_db_from_model URL MODEL REPOSITORY_PATH
diff --git a/migrate/versioning/schemadiff.py b/migrate/versioning/schemadiff.py
index 1005986..78ff4dc 100644
--- a/migrate/versioning/schemadiff.py
+++ b/migrate/versioning/schemadiff.py
@@ -8,17 +8,26 @@ def getDiffOfModelAgainstDatabase(model, conn, excludeTables=None):
'''
return SchemaDiff(model, conn, excludeTables)
+def getDiffOfModelAgainstModel(oldmodel, model, conn, excludeTables=None):
+ ''' Return differences of model against database.
+ Returned object will evaluate to True if there are differences else False.
+ '''
+ return SchemaDiff(model, conn, excludeTables, oldmodel=oldmodel)
+
class SchemaDiff(object):
''' Differences of model against database. '''
- def __init__(self, model, conn, excludeTables=None):
+ def __init__(self, model, conn, excludeTables=None, oldmodel=None):
''' Parameter model is your Python model's metadata and conn is an active database connection. '''
self.model = model
self.conn = conn
if not excludeTables: excludeTables = [] # [] can't be default value in Python parameter
self.excludeTables = excludeTables
- self.reflected_model = sqlalchemy.MetaData(conn, reflect=True)
+ if oldmodel:
+ self.reflected_model = oldmodel
+ else:
+ self.reflected_model = sqlalchemy.MetaData(conn, reflect=True)
self.tablesMissingInDatabase, self.tablesMissingInModel, self.tablesWithDiff = [], [], []
self.colDiffs = {}
self.compareModelToDatabase()
diff --git a/migrate/versioning/script/py.py b/migrate/versioning/script/py.py
index 7add4f4..1f2685a 100644
--- a/migrate/versioning/script/py.py
+++ b/migrate/versioning/script/py.py
@@ -20,16 +20,17 @@ class PythonScript(base.BaseScript):
shutil.copy(src,path)
@classmethod
- def make_update_script_for_model(cls,path,engine,model,repository,**opts):
+ def make_update_script_for_model(cls,engine,oldmodel,model,repository,**opts):
"""Create a migration script"""
- cls.require_notfound(path)
+ #cls.require_notfound(path) # TODO: yank?
# Compute differences.
if isinstance(repository, basestring):
from migrate.versioning.repository import Repository # oh dear, an import cycle!
repository=Repository(repository)
+ oldmodel = loadModel(oldmodel)
model = loadModel(model)
- diff = schemadiff.getDiffOfModelAgainstDatabase(model, engine, excludeTables=[repository.version_table])
+ diff = schemadiff.getDiffOfModelAgainstModel(oldmodel, model, engine, excludeTables=[repository.version_table])
upgradeDecls, upgradeCommands = genmodel.ModelGenerator(diff).toUpgradePython()
#downgradeCommands = genmodel.ModelGenerator(diff).toDowngradePython()
@@ -43,7 +44,7 @@ class PythonScript(base.BaseScript):
contents = contents.replace(search, upgradeDecls + '\n\n' + search, 1)
if upgradeCommands: contents = contents.replace(' pass', upgradeCommands, 1)
#if downgradeCommands: contents = contents.replace(' pass', downgradeCommands, 1) # TODO
- open(path, 'w').write(contents)
+ return contents # TODO: reinstate? open(path, 'w').write(contents)
@classmethod
def verify_module(cls,path):