add populate_default kwarg to column.create, fixes issue #50

This commit is contained in:
iElectric 2009-07-01 04:01:13 +02:00
parent 7e60b6b58a
commit e765caaef4
5 changed files with 26 additions and 16 deletions

View File

@ -1,6 +1,7 @@
0.5.5
-----
- added `populate_default` bool argument to :meth:`Column.create <migrate.changeset.schema.ChangesetColumn.create>` which issues corresponding UPDATE statements to set defaults after column creation
- url parameter can also be an Engine instance (this usage is discouraged though sometimes necessary)
- added support for SQLAlchemy 0.6 (missing oracle and firebird) by Michael Bayer
- alter, create, drop column / rename table / rename index constructs now accept `alter_metadata` parameter. If True, it will modify Column/Table objects according to changes. Otherwise, everything will be untouched.

View File

@ -39,8 +39,8 @@ Given a standard SQLAlchemy table::
:meth:`Create a column <ChangesetColumn.create>`::
col = Column('col1', String)
col.create(table)
col = Column('col1', String, default='foobar')
col.create(table, populate_default=True)
# Column is added to table based on its name
assert col is table.c.col1

View File

@ -485,12 +485,16 @@ class ChangesetColumn(object):
:param primary_key_name: Creates :class:\
`~migrate.changeset.constraint.PrimaryKeyConstraint` on this column.
:param alter_metadata: If True, column will be added to table object.
:param populate_default: If True, created column will be \
populated with defaults
:type table: Table instance
:type index_name: string
:type unique_name: string
:type primary_key_name: string
:type alter_metadata: bool
:type populate_default: bool
"""
self.populate_default = kwargs.pop('populate_default', False)
self.alter_metadata = kwargs.pop('alter_metadata', DEFAULT_ALTER_METADATA)
self.index_name = index_name
self.unique_name = unique_name
@ -503,6 +507,11 @@ class ChangesetColumn(object):
engine = self.table.bind
visitorcallable = get_engine_visitor(engine, 'columngenerator')
engine._run_visitor(visitorcallable, self, *args, **kwargs)
if self.populate_default and self.default is not None:
stmt = table.update().values({self: engine._execute_default(self.default)})
engine.execute(stmt)
return self
def drop(self, table=None, *args, **kwargs):

View File

@ -273,6 +273,20 @@ class TestAddDropColumn(fixture.DB):
self.assertEqual(u'foobar', row['data'])
col.drop()
@fixture.usedb()
def test_populate_default(self):
"""Test populate_default=True"""
def default():
return 'foobar'
col = Column('data', String(244), default=default)
col.create(self.table, populate_default=True)
self.table.insert(values={'id': 10}).execute()
row = self.table.select(autocommit=True).execute().fetchone()
self.assertEqual(u'foobar', row['data'])
col.drop()
# TODO: test sequence
# TODO: test quoting

View File

@ -1,14 +0,0 @@
# test_db.cfg
#
# This file contains a list of connection strings which will be used by
# database tests. Tests will be executed once for each string in this file.
# You should be sure that the database used for the test doesn't contain any
# important data. See README for more information.
#
# The string '__tmp__' is substituted for a temporary file in each connection
# string. This is useful for sqlite tests.
sqlite:///__tmp__
postgres://migrate:UPd2icyw@localhost/migrate_test
mysql://migrate:fTP82sjf@localhost/migrate_test
oracle://migrate:FdnjJK8s@localhost
firebird://migrate:BowV7EEm@localhost//var/db/migrate.gdb