add populate_default kwarg to column.create, fixes issue #50
This commit is contained in:
parent
7e60b6b58a
commit
e765caaef4
@ -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.
|
||||
|
@ -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
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
14
test_db.cfg
14
test_db.cfg
@ -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
|
Loading…
x
Reference in New Issue
Block a user