]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- The index name generated when you say "index=True"
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 22 Jun 2008 17:44:35 +0000 (17:44 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 22 Jun 2008 17:44:35 +0000 (17:44 +0000)
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]

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 37741670c875d8415739c90870a5a92e7ac62f73..f5e50fbc0e581abb4598f5a7c02a2d94e99a3273 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -38,6 +38,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 38dba17a5a18fb7d61a89357a5da6e2d3427948a..be6e58b2d7b1b6cfcd69ddf23ba7335b913f40ce 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 ab5a968716b7a8f54a9d27b2e943de06ec61b299..8b370650f6d034b52dd0830c614d489dc44eff5d 100644 (file)
@@ -1030,7 +1030,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(index, self._validate_identifier(index.name, False))
             ))
         self.execute()
 
index d91d9f08f886dddeaabef41f3e59be288dfe74db..3e718655ad0b227b9521bcadd2e6b870f9fdfb60 100644 (file)
@@ -2064,7 +2064,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(index, self._validate_identifier(index.name, False)),
                      self.preparer.format_table(index.table)))
         self.execute()
 
index 212daa06da3d99a449af89180c6fb71a166684a8..0ab6dd8b4dabcf83425df7bdd45ed22821169519 100644 (file)
@@ -747,7 +747,7 @@ class PGSchemaGenerator(compiler.SchemaGenerator):
         if index.unique:
             self.append("UNIQUE ")
         self.append("INDEX %s ON %s (%s)" \
-                    % (preparer.format_index(index),
+                    % (preparer.quote(index, self._validate_identifier(index.name, True)),
                        preparer.format_table(index.table),
                        string.join([preparer.format_column(c) for c in index.columns], ', ')))
         whereclause = index.kwargs.get('postgres_where', None)
index 2551e90c53a4eb11141d0b529300a45419ec562e..912cb47626ee8c17234396b86164a728d0d53b1d 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(index, self._validate_identifier(index.name, False))
             ))
         self.execute()
 
index 439710cee545f246ec0926bebb6a80f86300fef0..408d24c27895b722b45e31040344b572eb63236a 100644 (file)
@@ -741,6 +741,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)
@@ -900,7 +913,7 @@ class SchemaGenerator(DDLBase):
         if index.unique:
             self.append("UNIQUE ")
         self.append("INDEX %s ON %s (%s)" \
-                    % (preparer.format_index(index),
+                    % (preparer.quote(index, self._validate_identifier(index.name, True)),
                        preparer.format_table(index.table),
                        string.join([preparer.quote(c, c.name) for c in index.columns], ', ')))
         self.execute()
@@ -929,7 +942,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,9 +1063,6 @@ class IdentifierPreparer(object):
     def format_constraint(self, constraint):
         return self.quote(constraint, constraint.name)
 
-    def format_index(self, index):
-        return self.quote(index, index.name)
-
     def format_table(self, table, use_schema=True, name=None):
         """Prepare a quoted table and schema name."""
 
index 2908e07da929ef10da54ed59a20567c99cf120af..a40c5644df26e68a261111fe73f9ce8e0400c310 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(exceptions.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):