summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriElectric <unknown>2009-07-01 04:01:13 +0200
committeriElectric <unknown>2009-07-01 04:01:13 +0200
commite765caaef44be49cbe17a9c14c93367001afe8ea (patch)
treea63a2b9cff96b6b1927899bf0fefe2718366472c
parent7e60b6b58a828d3003cd886838f4725527709a88 (diff)
downloadsqlalchemy-migrate-e765caaef44be49cbe17a9c14c93367001afe8ea.tar.gz
add populate_default kwarg to column.create, fixes issue #50
-rw-r--r--docs/changelog.rst1
-rw-r--r--docs/changeset.rst4
-rw-r--r--migrate/changeset/schema.py9
-rw-r--r--test/changeset/test_changeset.py14
-rw-r--r--test_db.cfg14
5 files changed, 26 insertions, 16 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst
index f09e5c9..9792808 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -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.
diff --git a/docs/changeset.rst b/docs/changeset.rst
index 8a2b620..2612e7c 100644
--- a/docs/changeset.rst
+++ b/docs/changeset.rst
@@ -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
diff --git a/migrate/changeset/schema.py b/migrate/changeset/schema.py
index ab839b1..9af0f7d 100644
--- a/migrate/changeset/schema.py
+++ b/migrate/changeset/schema.py
@@ -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):
diff --git a/test/changeset/test_changeset.py b/test/changeset/test_changeset.py
index 5360a2b..9c2b66a 100644
--- a/test/changeset/test_changeset.py
+++ b/test/changeset/test_changeset.py
@@ -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
diff --git a/test_db.cfg b/test_db.cfg
deleted file mode 100644
index ceca16c..0000000
--- a/test_db.cfg
+++ /dev/null
@@ -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