]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- use "key in dict" rather than KeyError if the usual case
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 18 Sep 2010 15:40:25 +0000 (11:40 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 18 Sep 2010 15:40:25 +0000 (11:40 -0400)
is that the key is not present.
- don't need to uniquify Index schemes, just don't copy Indexes
that were known to be generated from the index=True flag
- user facing changes go in CHANGES
- Table.c allows string lookup

CHANGES
lib/sqlalchemy/schema.py
test/engine/test_metadata.py

diff --git a/CHANGES b/CHANGES
index abcd25ef0424725f575c50f2161f4a68b11f7b8c..1cf7a39f62602801e29276505a2153390fb4a3de 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -84,6 +84,14 @@ CHANGES
     of the query only, instead of repeatedly.   May make
     some more adjustments to this.
 
+- sql
+   - Table.tometadata() now copies Index objects associated
+     with the Table as well.
+
+   - Table.tometadata() issues a warning if the given Table 
+     is already present in the target MetaData - the existing
+     Table object is returned.
+     
 - engine
    
    - Fixed a regression in 0.6.4 whereby the change that
index 99e8c2a8c3ee25dcb6efe9ab87612ad03872c605..4b7916e98f9d3964b48993a26495316bc050926d 100644 (file)
@@ -467,43 +467,34 @@ class Table(SchemaItem, expression.TableClause):
         
         """
 
-        try:
-            if schema is RETAIN_SCHEMA:
-                schema = self.schema
-            key = _get_table_key(self.name, schema)
-            result = metadata.tables[key]
-            util.warn('tometadata will raise an exception '
-                      'when a table already exists in the '
-                      'target metadata in SQLAlchemy 0.7')
-            return result
-        except KeyError:
-            args = []
-            for c in self.columns:
-                args.append(c.copy(schema=schema))
-            for c in self.constraints:
-                args.append(c.copy(schema=schema))
-            table = Table(
-                self.name, metadata, schema=schema,
-                *args, **self.kwargs
-                )
-            copied_already = set()
-            for i in table.indexes:
-                entry = [i.name,i.unique]
-                entry.extend(sorted(i.kwargs.items()))
-                entry.extend(i.columns.keys())
-                copied_already.add(tuple(entry))
-            for i in self.indexes:
-                cols = i.columns.keys()
-                entry = [i.name,i.unique]
-                entry.extend(sorted(i.kwargs.items()))
-                entry.extend(cols)
-                if tuple(entry) not in copied_already:
-                    kwargs = dict(i.kwargs)
-                    kwargs['unique']=i.unique
-                    Index(i.name,
-                          *[getattr(table.c,col) for col in cols],
-                          **kwargs)
-            return table
+        if schema is RETAIN_SCHEMA:
+            schema = self.schema
+        key = _get_table_key(self.name, schema)
+        if key in metadata.tables:
+            util.warn("Table '%s' already exists within the given "
+                      "MetaData - not copying." % self.description)
+            return metadata.tables[key]
+
+        args = []
+        for c in self.columns:
+            args.append(c.copy(schema=schema))
+        for c in self.constraints:
+            args.append(c.copy(schema=schema))
+        table = Table(
+            self.name, metadata, schema=schema,
+            *args, **self.kwargs
+            )
+        for index in self.indexes:
+            # skip indexes that would be generated
+            # by the 'index' flag on Column
+            if len(index.columns) == 1 and \
+                list(index.columns)[0].index:
+                continue
+            Index(index.name,
+                  unique=index.unique,
+                  *[table.c[col] for col in index.columns.keys()],
+                  **index.kwargs)
+        return table
 
 class Column(SchemaItem, expression.ColumnClause):
     """Represents a column in a database table."""
index 4df7205507623566ddcc08d7c2ce828dfb256f3e..528d56244f33fe853187f390e408f80aed16474c 100644 (file)
@@ -276,19 +276,16 @@ class MetaDataTest(TestBase, ComparesTables):
         table_c = table.tometadata(meta2)
 
         def _get_key(i):
-            entry = [i.name,i.unique]
-            entry.extend(sorted(i.kwargs.items()))
-            entry.extend(i.columns.keys())
-            return entry
-
-        table_indexes = [_get_key(i) for i in table.indexes]
-        table_indexes.sort()
-        table_c_indexes = [_get_key(i) for i in table_c.indexes]
-        table_c_indexes.sort()
-            
-        eq_(table_indexes,table_c_indexes)
-
-    @emits_warning('tometadata.*')
+            return [i.name,i.unique] + \
+                    sorted(i.kwargs.items()) + \
+                    i.columns.keys()
+        
+        eq_(
+            sorted([_get_key(i) for i in table.indexes]),
+            sorted([_get_key(i) for i in table_c.indexes])
+        )
+
+    @emits_warning("Table '.+' already exists within the given MetaData")
     def test_tometadata_already_there(self):
         
         meta1 = MetaData()