diff options
| author | christian.simms <unknown> | 2008-03-22 14:17:31 +0000 |
|---|---|---|
| committer | christian.simms <unknown> | 2008-03-22 14:17:31 +0000 |
| commit | 373a111c2c629ec0d92b01bf1c4dc8e2cdd5d2d7 (patch) | |
| tree | 26a9ec393336fa342071146d2e944491bb077a42 /migrate/versioning/script | |
| parent | bf45f7da6d4301f323a8b333251b3b56b50ab265 (diff) | |
| download | sqlalchemy-migrate-373a111c2c629ec0d92b01bf1c4dc8e2cdd5d2d7.tar.gz | |
add experimental support for comparing metadata against database (issue #12)
Diffstat (limited to 'migrate/versioning/script')
| -rw-r--r-- | migrate/versioning/script/py.py | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/migrate/versioning/script/py.py b/migrate/versioning/script/py.py index 4a980dc..f373131 100644 --- a/migrate/versioning/script/py.py +++ b/migrate/versioning/script/py.py @@ -1,6 +1,6 @@ import shutil import migrate.run -from migrate.versioning import exceptions +from migrate.versioning import exceptions, genmodel, schemadiff from migrate.versioning.base import operations from migrate.versioning.template import template from migrate.versioning.script import base @@ -20,6 +20,37 @@ class PythonScript(base.BaseScript): shutil.copy(src,path) @classmethod + def script_python_changes(cls,path,engine,model,repository,**opts): + """Create a migration script""" + cls.require_notfound(path) + + # Compute differences. + if isinstance(repository, basestring): + from migrate.versioning.repository import Repository # oh dear, an import cycle! + repository=Repository(repository) + if isinstance(model, basestring): # TODO: centralize this code? + # Assume model is of form "mod1.mod2.varname". + varname = model.split('.')[-1] + modules = '.'.join(model.split('.')[:-1]) + module = __import__(modules, globals(), {}, ['dummy-not-used'], -1) + model = getattr(module, varname) + diff = schemadiff.getDiffOfModelAgainstDatabase(model, engine, excludeTables=[repository.version_table]) + upgradeDecls, upgradeCommands = genmodel.ModelGenerator(diff).toUpgradePython() + #downgradeCommands = genmodel.ModelGenerator(diff).toDowngradePython() + + # TODO: Use the default script template (defined in the template + # module) for now, but we might want to allow people to specify a + # different one later. + template_file = None + src = template.get_script(template_file) + contents = open(src).read() + search = 'def upgrade():' + 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) + + @classmethod def verify_module(cls,path): """Ensure this is a valid script, or raise InvalidScriptError""" # Try to import and get the upgrade() func |
