]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
merged r4870 from 0.4 branch, index name truncation, [ticket:820]
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 22 Jun 2008 17:52:13 +0000 (17:52 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 22 Jun 2008 17:52:13 +0000 (17:52 +0000)
CHANGES
lib/sqlalchemy/databases/access.py
lib/sqlalchemy/databases/mssql.py
lib/sqlalchemy/databases/mysql.py
lib/sqlalchemy/databases/postgres.py
lib/sqlalchemy/databases/sybase.py
lib/sqlalchemy/sql/compiler.py
test/sql/constraints.py

diff --git a/CHANGES b/CHANGES
index ce1e8ddbc3d1f31afd927e57790626f37d53044a..d34bf8b810c9cd3426fd09f0b54db03144b9125b 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -5,6 +5,9 @@ CHANGES
 =======
 0.5beta2
 ========
+    - 0.5beta2 includes all bugfixes listed under release 
+      "0.4.7".
+
 - orm
     - In addition to expired attributes, deferred attributes
       also load if their data is present in the result set.
@@ -113,6 +116,13 @@ CHANGES
       looking for the name within the DB's catalog tables.
       [ticket:571]
 
+    - The index name generated when you say "index=True"
+      on a Column is truncated to the length appropriate
+      for the dialect. Additionally, an Index with a too-
+      long name cannot be explicitly dropped with
+      Index.drop(), similar to [ticket:571].
+      [ticket:820]
+      
 - postgres
     - Repaired server_side_cursors to properly detect 
       text() clauses.
index f334522001f5381cd41d7184c2627f77df68a201..55b48e4bcd07c89ed5b2e9bde016270045035299 100644 (file)
@@ -408,7 +408,8 @@ class AccessSchemaGenerator(compiler.SchemaGenerator):
 
 class AccessSchemaDropper(compiler.SchemaDropper):
     def visit_index(self, index):
-        self.append("\nDROP INDEX [%s].[%s]" % (index.table.name, index.name))
+        
+        self.append("\nDROP INDEX [%s].[%s]" % (index.table.name, self._validate_identifier(index.name, False)))
         self.execute()
 
 class AccessDefaultRunner(base.DefaultRunner):
index 1cacd81ef919e518e89fb1961d1aff39d16a407a..4f6db935f404b7871a81871e50a849473ad47e86 100644 (file)
@@ -1031,7 +1031,7 @@ class MSSQLSchemaDropper(compiler.SchemaDropper):
     def visit_index(self, index):
         self.append("\nDROP INDEX %s.%s" % (
             self.preparer.quote_identifier(index.table.name),
-            self.preparer.quote_identifier(index.name)
+            self.preparer.quote(self._validate_identifier(index.name, False), index.quote)
             ))
         self.execute()
 
index 426e97a81d1f92b33947fb9f24a6dc982f41955d..ed01b77d78afd7666495ecb08d7fb97cbbaaa16f 100644 (file)
@@ -2062,7 +2062,7 @@ class MySQLSchemaGenerator(compiler.SchemaGenerator):
 class MySQLSchemaDropper(compiler.SchemaDropper):
     def visit_index(self, index):
         self.append("\nDROP INDEX %s ON %s" %
-                    (self.preparer.format_index(index),
+                    (self.preparer.quote(self._validate_identifier(index.name, False), index.quote),
                      self.preparer.format_table(index.table)))
         self.execute()
 
index 12424208640ef3625543255b49d824f35b66ac77..c30ca8b2873b3c0b5e24de6abf5cff3839008398 100644 (file)
@@ -753,7 +753,7 @@ class PGSchemaGenerator(compiler.SchemaGenerator):
         if index.unique:
             self.append("UNIQUE ")
         self.append("INDEX %s ON %s (%s)" \
-                    % (preparer.format_index(index),
+                    % (preparer.quote(self._validate_identifier(index.name, True), index.quote),
                        preparer.format_table(index.table),
                        string.join([preparer.format_column(c) for c in index.columns], ', ')))
         whereclause = index.kwargs.get('postgres_where', None)
index cc8597068fbd23950c08157ceba931e76cfcef6b..45a98bec59ed25f60af8243acfb77bbe820de578 100644 (file)
@@ -844,7 +844,7 @@ class SybaseSQLSchemaDropper(compiler.SchemaDropper):
     def visit_index(self, index):
         self.append("\nDROP INDEX %s.%s" % (
             self.preparer.quote_identifier(index.table.name),
-            self.preparer.quote_identifier(index.name)
+            self.preparer.quote(self._validate_identifier(index.name, False), index.quote)
             ))
         self.execute()
 
index b57fd3b1853a440899cd2b8a769a50d5b24cece0..28685cabc59411838bd0c08281dcbffaf543a7be 100644 (file)
@@ -742,6 +742,19 @@ class DDLBase(engine.SchemaIterator):
                 findalterables.traverse(c)
         return alterables
 
+    def _validate_identifier(self, ident, truncate):
+        if truncate:
+            if len(ident) > self.dialect.max_identifier_length:
+                counter = getattr(self, 'counter', 0)
+                self.counter = counter + 1
+                return ident[0:self.dialect.max_identifier_length - 6] + "_" + hex(self.counter)[2:]
+            else:
+                return ident
+        else:
+            self.dialect.validate_identifier(ident)
+            return ident
+
+
 class SchemaGenerator(DDLBase):
     def __init__(self, dialect, connection, checkfirst=False, tables=None, **kwargs):
         super(SchemaGenerator, self).__init__(connection, **kwargs)
@@ -901,7 +914,7 @@ class SchemaGenerator(DDLBase):
         if index.unique:
             self.append("UNIQUE ")
         self.append("INDEX %s ON %s (%s)" \
-                    % (preparer.format_index(index),
+                    % (preparer.quote(self._validate_identifier(index.name, True), index.quote),
                        preparer.format_table(index.table),
                        string.join([preparer.quote(c.name, c.quote) for c in index.columns], ', ')))
         self.execute()
@@ -930,7 +943,7 @@ class SchemaDropper(DDLBase):
         return not self.checkfirst or self.dialect.has_table(self.connection, table.name, schema=table.schema)
 
     def visit_index(self, index):
-        self.append("\nDROP INDEX " + self.preparer.format_index(index))
+        self.append("\nDROP INDEX " + self.preparer.quote(index, self._validate_identifier(index.name, False)))
         self.execute()
 
     def drop_foreignkey(self, constraint):
@@ -1050,10 +1063,7 @@ class IdentifierPreparer(object):
 
     def format_constraint(self, constraint):
         return self.quote(constraint.name, constraint.quote)
-
-    def format_index(self, index):
-        return self.quote(index.name, index.quote)
-
+    
     def format_table(self, table, use_schema=True, name=None):
         """Prepare a quoted table and schema name."""
 
index 5ce5cca3417c4c40e13112739e4e41da7a21e220..b719ac93d36d5a8c5f79688f5975af78269432a7 100644 (file)
@@ -201,7 +201,25 @@ class ConstraintTest(TestBase, AssertsExecutionResults):
                                 winner='sweden')
         ss = events.select().execute().fetchall()
 
+    def test_too_long_idx_name(self):
+        dialect = testing.db.dialect.__class__()
+        dialect.max_identifier_length = 20
 
+        schemagen = dialect.schemagenerator(dialect, None)
+        schemagen.execute = lambda : None
+
+        t1 = Table("sometable", MetaData(), Column("foo", Integer))
+        schemagen.visit_index(Index("this_name_is_too_long_for_what_were_doing", t1.c.foo))
+        self.assertEquals(schemagen.buffer.getvalue(), "CREATE INDEX this_name_is_t_1 ON sometable (foo)")
+        schemagen.buffer.truncate(0)
+        schemagen.visit_index(Index("this_other_name_is_too_long_for_what_were_doing", t1.c.foo))
+        self.assertEquals(schemagen.buffer.getvalue(), "CREATE INDEX this_other_nam_2 ON sometable (foo)")
+
+        schemadrop = dialect.schemadropper(dialect, None)
+        schemadrop.execute = lambda: None
+        self.assertRaises(exc.IdentifierError, schemadrop.visit_index, Index("this_name_is_too_long_for_what_were_doing", t1.c.foo))
+
+    
 class ConstraintCompilationTest(TestBase, AssertsExecutionResults):
     class accum(object):
         def __init__(self):