removed magical behavior with importing migrate_engine, now engine is passed to upgrade/downgrade functions

This commit is contained in:
iElectric 2009-06-12 13:26:01 +00:00
parent 8a8b1d2366
commit 03eb309b67
13 changed files with 87 additions and 63 deletions

View File

@ -1,7 +1,3 @@
*****************
API Documentation
*****************
Module :mod:`migrate.changeset`
===============================

View File

@ -1,5 +1,11 @@
Changelog
=========
0.5.5
-----
.. _backwards-055:
**Backward incompatible changes**:
- python upgrade/downgrade scripts do not import migrate_engine magically, but recieve engine as the only parameter to function
0.5.4
-----

View File

@ -1,6 +1,3 @@
Download and Development of SQLAlchemy Migrate
==============================================
Download
--------
@ -17,7 +14,7 @@ line::
migrate
This should list all available commands. *migrate help COMMAND* will
This should list all available commands. ``migrate help COMMAND`` will
display more information about each command.
If you'd like to be notified when new versions of SQLAlchemy Migrate

View File

@ -1,16 +1,35 @@
SQLAlchemy Migrate - SQLAlchemy schema change management
========================================================
:mod:`migrate` - SQLAlchemy Migrate (schema change management)
==============================================================
Inspired by Ruby on Rails' migrations, SQLAlchemy Migrate provides a
way to deal with database schema changes in SQLAlchemy_ projects.
.. module:: migrate
.. moduleauthor:: Evan Rosson
Migrate was started as part of `Google's Summer of Code`_ by Evan
Rosson, mentored by Jonathan LaCour.
:Author: Evan Rosson
:Maintainer: Domen Kozar <domenNO@SPAMdev.si>
:Source code: http://code.google.com/p/sqlalchemy-migrate/issues/list
:Issues: http://code.google.com/p/sqlalchemy-migrate/
:Version: |release|
The project was taken over by a small group of volunteers when Evan
had no free time for the project. It is now hosted as a `Google Code
project`_. During the hosting change the project was renamed to
SQLAlchemy Migrate.
.. topic:: Overview
Inspired by Ruby on Rails' migrations, SQLAlchemy Migrate provides a
way to deal with database schema changes in SQLAlchemy_ projects.
Migrate was started as part of `Google's Summer of Code`_ by Evan
Rosson, mentored by Jonathan LaCour.
The project was taken over by a small group of volunteers when Evan
had no free time for the project. It is now hosted as a `Google Code
project`_. During the hosting change the project was renamed to
SQLAlchemy Migrate.
.. warning::
**0.5.5** release breaks backward compatability, please read :ref:`changelog <backwards-055>` for more info.
Download and Development of SQLAlchemy Migrate
----------------------------------------------
.. toctree::
@ -29,13 +48,26 @@ versioning API is available as the :command:`migrate` command.
versioning
changeset
tools
api
changelog
.. _`google's summer of code`: http://code.google.com/soc
.. _`Google Code project`: http://code.google.com/p/sqlalchemy-migrate
.. _sqlalchemy: http://www.sqlalchemy.org
API Documentation
------------------
.. toctree::
api
Changelog
---------
.. toctree::
changelog
Indices and tables
==================

View File

@ -127,7 +127,7 @@ class ModelGenerator(object):
def toUpgradeDowngradePython(self, indent=' '):
''' Assume model is most current and database is out-of-date. '''
decls = ['meta = MetaData(migrate_engine)']
decls = ['meta = MetaData()']
for table in self.diff.tablesMissingInModel + \
self.diff.tablesMissingInDatabase:
decls.extend(self.getTableDefn(table))
@ -143,10 +143,12 @@ class ModelGenerator(object):
upgradeCommands.append("%(table)s.create()" % {'table': tableName})
downgradeCommands.append("%(table)s.drop()" % {'table': tableName})
pre_command = 'meta.bind(migrate_engine)'
return (
'\n'.join(decls),
'\n'.join(['%s%s' % (indent, line) for line in upgradeCommands]),
'\n'.join(['%s%s' % (indent, line) for line in downgradeCommands]))
'\n'.join([pre_command] + ['%s%s' % (indent, line) for line in upgradeCommands]),
'\n'.join([pre_command] + ['%s%s' % (indent, line) for line in downgradeCommands]))
def applyModel(self):
"""Apply model to current database."""

View File

@ -74,7 +74,7 @@ class PythonScript(base.BaseScript):
f.close()
# generate source
search = 'def upgrade():'
search = 'def upgrade(migrate_engine):'
contents = contents.replace(search, '\n\n'.join((decls, search)), 1)
if upgradeCommands:
contents = contents.replace(' pass', upgradeCommands, 1)
@ -136,12 +136,8 @@ class PythonScript(base.BaseScript):
raise exceptions.ScriptError("%d is not a valid step" % step)
funcname = base.operations[op]
migrate.migrate_engine = engine
#migrate.run.migrate_engine = migrate.migrate_engine = engine
func = self._func(funcname)
func()
migrate.migrate_engine = None
#migrate.run.migrate_engine = migrate.migrate_engine = None
func(engine)
@property
def module(self):

View File

@ -1,11 +1,11 @@
from sqlalchemy import *
from migrate import *
def upgrade():
# Upgrade operations go here. Don't create your own engine; use the engine
# named 'migrate_engine' imported from migrate.
def upgrade(migrate_engine):
# Upgrade operations go here. Don't create your own engine; bind migrate_engine
# to your metadata
pass
def downgrade():
def downgrade(migrate_engine):
# Operations to reverse the above upgrade go here.
pass

View File

@ -1,12 +0,0 @@
from sqlalchemy import *
from migrate import *
logsql=True
def upgrade():
# Upgrade operations go here. Don't create your own engine; use the engine
# named 'migrate_engine' imported from migrate.
pass
def downgrade():
# Operations to reverse the above upgrade go here.
pass

View File

@ -161,7 +161,7 @@ class Version(object):
# TODO: maybe add force Python parameter?
ret = self.python
assert ret is not None
assert ret is not None, "There is no script for %d version" % self.version
return ret
# deprecated?

View File

@ -173,7 +173,7 @@ class TestVersionedRepository(fixture.Pathed):
self.assertRaises(Exception, repos.changeset, 'postgres', -1)
# Downgrade
cs=check_changeset((10, 0),10)
cs = check_changeset((10, 0),10)
self.assertEquals(cs.keys().pop(0), 10) # 10 -> 9
self.assertEquals(cs.keys().pop(), 1) # 1 -> 0
self.assertEquals(cs.start, 10)

View File

@ -55,15 +55,19 @@ class TestSchemaDiff(fixture.DB):
diff = schemadiff.getDiffOfModelAgainstDatabase(self.meta, self.engine, excludeTables=['migrate_version'])
decls, upgradeCommands, downgradeCommands = genmodel.ModelGenerator(diff).toUpgradeDowngradePython()
self.assertEqualsIgnoreWhitespace(decls, '''
meta = MetaData(migrate_engine)
meta = MetaData()
tmp_schemadiff = Table('tmp_schemadiff',meta,
Column('id',Integer(),primary_key=True,nullable=False),
Column('name',UnicodeText(length=None)),
Column('data',UnicodeText(length=None)),
)
''')
self.assertEqualsIgnoreWhitespace(upgradeCommands, '''tmp_schemadiff.create()''')
self.assertEqualsIgnoreWhitespace(downgradeCommands, '''tmp_schemadiff.drop()''')
self.assertEqualsIgnoreWhitespace(upgradeCommands,
'''meta.bind(migrate_engine)
tmp_schemadiff.create()''')
self.assertEqualsIgnoreWhitespace(downgradeCommands,
'''meta.bind(migrate_engine)
tmp_schemadiff.drop()''')
# Create table in database, now model should match database.
self._applyLatestModel()

View File

@ -90,15 +90,15 @@ class TestPyScript(fixture.Pathed, fixture.DB):
from migrate import *
from sqlalchemy import *
metadata = MetaData(migrate_engine)
metadata = MetaData()
UserGroup = Table('Link', metadata,
Column('link1ID', Integer),
Column('link2ID', Integer),
UniqueConstraint('link1ID', 'link2ID'))
def upgrade():
metadata.create_all()
def upgrade(migrate_engine):
metadata.create_all(migrate_engine)
"""
f.write(content)
f.close()

View File

@ -533,25 +533,28 @@ class TestShellDatabase(Shell, fixture.DB):
# We're happy with db changes, make first db upgrade script to go from version 0 -> 1.
output, exitcode = self.output_and_exitcode('python %s make_update_script_for_model' % script_path) # intentionally omit a parameter
self.assertEquals('Not enough arguments' in output, True)
output, exitcode = self.output_and_exitcode('python %s make_update_script_for_model --oldmodel=oldtestmodel.meta' % script_path)
assert """from sqlalchemy import *
output, exitcode = self.output_and_exitcode('python %s make_update_script_for_model --oldmodel=oldtestmodel:meta' % script_path)
self.assertEqualsIgnoreWhitespace(output,
"""from sqlalchemy import *
from migrate import *
meta = MetaData(migrate_engine)
meta = MetaData()
tmp_account_rundiffs = Table('tmp_account_rundiffs', meta,
Column('id', Integer(), primary_key=True, nullable=False),
Column('login', String(length=40, convert_unicode=False, assert_unicode=None)),
Column('passwd', String(length=40, convert_unicode=False, assert_unicode=None)),
)
def upgrade():
# Upgrade operations go here. Don't create your own engine; use the engine
# named 'migrate_engine' imported from migrate.
def upgrade(migrate_engine):
# Upgrade operations go here. Don't create your own engine; bind migrate_engine
# to your metadata
meta.bind(migrate_engine)
tmp_account_rundiffs.create()
def downgrade():
def downgrade(migrate_engine):
# Operations to reverse the above upgrade go here.
tmp_account_rundiffs.drop()""" in output, output
meta.bind(migrate_engine)
tmp_account_rundiffs.drop()""")
# Save the upgrade script.
self.assertSuccess(self.cmd('script', '--repository=%s' % repos_path, 'Desc'))