]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [feature] Added create_type constructor argument
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 29 Nov 2011 03:28:28 +0000 (22:28 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 29 Nov 2011 03:28:28 +0000 (22:28 -0500)
to pg.ENUM.  When False, no CREATE/DROP or
checking for the type will be performed as part
of a table create/drop event; only the
create()/drop)() methods called directly
will do this.  Helps with Alembic "offline"
scripts.

CHANGES
doc/build/core/types.rst
doc/build/dialects/postgresql.rst
lib/sqlalchemy/dialects/postgresql/base.py
lib/sqlalchemy/types.py
test/dialect/test_postgresql.py

diff --git a/CHANGES b/CHANGES
index f3793cfc3354901ec8af95a646f5e43071a8954a..04e45d97d54c9f1dc7038cbbfc58a97096ac4877 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -138,6 +138,14 @@ CHANGES
      "checkfirst" turned on it only needs to 
      check for the ENUM once.  [ticket:2311]
 
+   - [feature] Added create_type constructor argument 
+     to pg.ENUM.  When False, no CREATE/DROP or 
+     checking for the type will be performed as part
+     of a table create/drop event; only the 
+     create()/drop)() methods called directly
+     will do this.  Helps with Alembic "offline"
+     scripts.
+
 - mysql
   - [bug] Unicode adjustments allow latest pymysql 
     (post 0.4) to pass 100% on Python 2.
index 4e55b25d730acac728dbdfe43268a79af0dc69c3..b7b4e3488c75a0abd38f256656c6b8ec221bae33 100644 (file)
@@ -58,7 +58,7 @@ Standard Types`_ and the other sections of this chapter.
 
 .. autoclass:: Enum
   :show-inheritance:
-  :members:
+  :members: __init__, create, drop
 
 .. autoclass:: Float
   :show-inheritance:
index 6e45992eb7dc1dace7bd05537a56972c13dd7979..b34c0768487909912a81f4d266915beb04cb8c6f 100644 (file)
@@ -44,7 +44,7 @@ construction arguments, are as follows:
     :show-inheritance:
 
 .. autoclass:: ENUM
-    :members: __init__
+    :members: __init__, create, drop
     :show-inheritance:
 
 .. autoclass:: INET
index e3beeab79c5bbbf338f4c0ed35211322a150ee02..673b86add23bec5b3593c058ce00495394591c88 100644 (file)
@@ -432,8 +432,74 @@ class ARRAY(sqltypes.MutableType, sqltypes.Concatenable, sqltypes.TypeEngine):
 PGArray = ARRAY
 
 class ENUM(sqltypes.Enum):
+    """Postgresql ENUM type.
+    
+    This is a subclass of :class:`.types.Enum` which includes
+    support for PG's ``CREATE TYPE``.
+    
+    :class:`~.postgresql.ENUM` is used automatically when 
+    using the :class:`.types.Enum` type on PG assuming
+    the ``native_enum`` is left as ``True``.   However, the 
+    :class:`~.postgresql.ENUM` class can also be instantiated
+    directly in order to access some additional Postgresql-specific
+    options, namely finer control over whether or not 
+    ``CREATE TYPE`` should be emitted.
+    
+    Note that both :class:`.types.Enum` as well as 
+    :class:`~.postgresql.ENUM` feature create/drop
+    methods; the base :class:`.types.Enum` type ultimately
+    delegates to the :meth:`~.postgresql.ENUM.create` and
+    :meth:`~.postgresql.ENUM.drop` methods present here.
+    
+    """
+
+    def __init__(self, *enums, **kw):
+        """Construct an :class:`~.postgresql.ENUM`.
+        
+        Arguments are the same as that of
+        :class:`.types.Enum`, but also including
+        the following parameters.
+        
+        :param create_type: Defaults to True.  
+         Indicates that ``CREATE TYPE`` should be 
+         emitted, after optionally checking for the 
+         presence of the type, when the parent 
+         table is being created; and additionally
+         that ``DROP TYPE`` is called when the table
+         is dropped.    When ``False``, no check
+         will be performed and no ``CREATE TYPE``
+         or ``DROP TYPE`` is emitted, unless
+         :meth:`~.postgresql.ENUM.create`
+         or :meth:`~.postgresql.ENUM.drop`
+         are called directly.
+         Setting to ``False`` is helpful
+         when invoking a creation scheme to a SQL file
+         without access to the actual database - 
+         the :meth:`~.postgresql.ENUM.create` and
+         :meth:`~.postgresql.ENUM.drop` methods can
+         be used to emit SQL to a target bind.
+         (new in 0.7.4)
+         
+        """
+        self.create_type = kw.pop("create_type", True)
+        super(ENUM, self).__init__(*enums, **kw)
 
     def create(self, bind=None, checkfirst=True):
+        """Emit ``CREATE TYPE`` for this 
+        :class:`~.postgresql.ENUM`.
+        
+        If the underlying dialect does not support
+        Postgresql CREATE TYPE, no action is taken.
+        
+        :param bind: a connectable :class:`.Engine`,
+         :class:`.Connection`, or similar object to emit
+         SQL.
+        :param checkfirst: if ``True``, a query against 
+         the PG catalog will be first performed to see
+         if the type does not exist already before
+         creating.
+         
+        """
         if not bind.dialect.supports_native_enum:
             return
 
@@ -442,6 +508,20 @@ class ENUM(sqltypes.Enum):
             bind.execute(CreateEnumType(self))
 
     def drop(self, bind=None, checkfirst=True):
+        """Emit ``DROP TYPE`` for this 
+        :class:`~.postgresql.ENUM`.
+        
+        If the underlying dialect does not support
+        Postgresql DROP TYPE, no action is taken.
+        
+        :param bind: a connectable :class:`.Engine`,
+         :class:`.Connection`, or similar object to emit
+         SQL.
+        :param checkfirst: if ``True``, a query against 
+         the PG catalog will be first performed to see
+         if the type actually exists before dropping.
+         
+        """
         if not bind.dialect.supports_native_enum:
             return
 
@@ -458,7 +538,8 @@ class ENUM(sqltypes.Enum):
         sequence without relying upon "checkfirst".
 
         """
-
+        if not self.create_type:
+            return True
         if '_ddl_runner' in kw:
             ddl_runner = kw['_ddl_runner']
             if '_pg_enums' in ddl_runner.memo:
index 79799da8f33b5078c1ff96696c7eea882ed8ff35..136de147ee843fbcd656bfe3775ef698d94d642c 100644 (file)
@@ -1784,6 +1784,12 @@ class Enum(String, SchemaType):
 
     By default, uses the backend's native ENUM type if available, 
     else uses VARCHAR + a CHECK constraint.
+    
+    See also:
+    
+        :class:`~.postgresql.ENUM` - PostgreSQL-specific type,
+        which has additional functionality.
+        
     """
 
     __visit_name__ = 'enum'
index b4b7e818cd765b4f0315aee301520b7d13521f96..7279508ba9ed54358b39a27ae3138343be388129 100644 (file)
@@ -450,6 +450,24 @@ class EnumTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL):
         finally:
             metadata.drop_all(testing.db)
 
+    @testing.provide_metadata
+    def test_disable_create(self):
+        metadata = self.metadata
+
+        e1 = postgresql.ENUM('one', 'two', 'three', 
+                            name="myenum",
+                            create_type=False)
+
+        t1 = Table('e1', metadata, 
+            Column('c1', e1)
+        )
+        # table can be created separately
+        # without conflict
+        e1.create(bind=testing.db)
+        t1.create(testing.db)
+        t1.drop(testing.db)
+        e1.drop(bind=testing.db)
+
     @testing.provide_metadata
     def test_generate_multiple(self):
         """Test that the same enum twice only generates once