From: Mike Bayer Date: Mon, 17 Oct 2016 04:22:38 +0000 (-0400) Subject: Add explicit copy() to Enum X-Git-Tag: rel_1_1_2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ef4f6a53864ce9c57c4879d6b2aa0f81ddbf596;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add explicit copy() to Enum The Boolean and Enum types both place SchemaType second in the inheritance hierarchy. In the case of Enum, this works out that the copy() method is called from the base TypeEngine which fails to transfer _create_events. The test suite doesn't seem to work with the inhertance hierarchy set up like this as the event listeners don't work out, the _on_metadata_create and _on_table_create hooks cause the production of an adapted type which then adds event listeners that cause deque changed while iteration. It's not clear why Enum /Boolean don't have this problem. But in any case it seems like the class mechanics for these types remains fragile and would benefit from yet another refactor someday. Change-Id: Ib641a5d2321b00f58bbe98dd0c5e789374db32b2 Fixes: #3827 --- diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 45d98aa223..11d49d7a7f 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -51,6 +51,19 @@ paths, the "present" column loader will now override the deferred non- load for that entity regardless of row ordering. + .. change:: + :tags: bug, sql, postgresql + :tickets: 3827 + + Fixed regression in :class:`.Enum` type where event handlers were not + transferred in the case of the type object being copied, due to a + conflicting copy() method added as part of [ticket:3250]. This copy + occurs normally in situations when the column is copied, such as + in tometadata() or when using declarative mixins with columns. The + event handler not being present would impact the constraint being + created for a non-native enumerated type, but more critically the + ENUM object on the PostgreSQL backend. + .. changelog:: :version: 1.1.1 :released: October 7, 2016 diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 78547ccf07..118c26070a 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -1330,6 +1330,9 @@ class Enum(String, SchemaType): ) assert e.table is table + def copy(self, **kw): + return SchemaType.copy(self, **kw) + def adapt(self, impltype, **kw): schema = kw.pop('schema', self.schema) metadata = kw.pop('metadata', self.metadata) diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index af291b8427..f2df4da063 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -1,7 +1,6 @@ from sqlalchemy.testing import assert_raises from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import emits_warning - import pickle from sqlalchemy import Integer, String, UniqueConstraint, \ CheckConstraint, ForeignKey, MetaData, Sequence, \ @@ -16,7 +15,7 @@ import sqlalchemy as tsa from sqlalchemy.testing import fixtures from sqlalchemy import testing from sqlalchemy.testing import ComparesTables, AssertsCompiledSQL -from sqlalchemy.testing import eq_, is_, mock +from sqlalchemy.testing import eq_, is_, mock, is_true from contextlib import contextmanager from sqlalchemy import util @@ -1534,26 +1533,39 @@ class PKAutoIncrementTest(fixtures.TestBase): class SchemaTypeTest(fixtures.TestBase): - class MyType(sqltypes.SchemaType, sqltypes.TypeEngine): + class TrackEvents(object): column = None table = None evt_targets = () def _set_table(self, column, table): - super(SchemaTypeTest.MyType, self)._set_table(column, table) + super(SchemaTypeTest.TrackEvents, self)._set_table(column, table) self.column = column self.table = table def _on_table_create(self, target, bind, **kw): - super(SchemaTypeTest.MyType, self)._on_table_create( + super(SchemaTypeTest.TrackEvents, self)._on_table_create( target, bind, **kw) self.evt_targets += (target,) def _on_metadata_create(self, target, bind, **kw): - super(SchemaTypeTest.MyType, self)._on_metadata_create( + super(SchemaTypeTest.TrackEvents, self)._on_metadata_create( target, bind, **kw) self.evt_targets += (target,) + # TODO: Enum and Boolean put TypeEngine first. Changing that here + # causes collection-mutate-while-iterated errors in the event system + # since the hooks here call upon the adapted type. Need to figure out + # why Enum and Boolean don't have this problem. + class MyType(TrackEvents, sqltypes.SchemaType, sqltypes.TypeEngine): + pass + + class WrapEnum(TrackEvents, Enum): + pass + + class WrapBoolean(TrackEvents, Boolean): + pass + class MyTypeWImpl(MyType): def _gen_dialect_impl(self, dialect): @@ -1656,6 +1668,29 @@ class SchemaTypeTest(fixtures.TestBase): eq_(t1.c.y.type.evt_targets, (t1,)) eq_(t2.c.y.type.evt_targets, (t2, t2)) + def test_enum_column_copy_transfers_events(self): + m = MetaData() + + type_ = self.WrapEnum('a', 'b', 'c', name='foo') + y = Column('y', type_) + y_copy = y.copy() + t1 = Table('x', m, y_copy) + + is_true(y_copy.type._create_events) + + m.dispatch.before_create(t1, testing.db) + eq_(t1.c.y.type.evt_targets, (t1, )) + + def test_boolean_column_copy_transfers_events(self): + m = MetaData() + + type_ = self.WrapBoolean() + y = Column('y', type_) + y_copy = y.copy() + t1 = Table('x', m, y_copy) + + is_true(y_copy.type._create_events) + def test_metadata_dispatch_no_new_impl(self): m1 = MetaData() typ = self.MyType(metadata=m1)