Ticket #702 (closed defect: fixed)

Opened 19 months ago

Last modified 2 months ago

Sequences not working (at least) with MySQL

Reported by: jupaju Owned by: somebody
Priority: critical Milestone: 2.0.0 (OLD)
Component: Other Version: 1.0.7
Severity: abc Keywords:
Cc: Has Test: no
Status: Pending Core Response Has Patch: no

Description (last modified by romanb) (diff)

I'm having problems getting sequences to work with MySQL. After some digging I realized that sequences with MySQL were, how to put it kindly, work in progress. :) I made some fixes here and there and now sequences seem to work at least somehow.

I removed few $this->conn->formatter->getSequenceName() -calls because they were causing table names like table_seq_seq because $record->getTable()->sequenceName already contains complete sequenceName and doesn't need to be modified again.

$ svn diff
Index: lib/Doctrine/Export/Mysql.php
===================================================================
--- lib/Doctrine/Export/Mysql.php       (revision 3431)
+++ lib/Doctrine/Export/Mysql.php       (working copy)
@@ -380,7 +380,7 @@
      */
     public function createSequence($sequenceName, $start = 1, array $options = array())
     {
-        $sequenceName   = $this->conn->quoteIdentifier($this->conn->getSequenceName($sequenceName), true);
+        $sequenceName   = $this->conn->quoteIdentifier($sequenceName, true);
         $seqcolName     = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
 
         $optionsStrings = array();
@@ -393,7 +393,7 @@
             $optionsStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
 
             if (isset($options['collate'])) {
-                $optionsStrings['collate'] .= ' COLLATE ' . $options['collate'];
+                $optionsStrings['charset'] .= ' COLLATE ' . $options['collate'];
             }
         }
 
@@ -402,7 +402,7 @@
         if (isset($options['type'])) {
             $type = $options['type'];
         } else {
-            $type = $this->conn->default_table_type;
+            $type = $this->conn->getAttribute(Doctrine::ATTR_DEFAULT_TABLE_TYPE);
         }
         if ($type) {
             $optionsStrings[] = 'ENGINE = ' . $type;
@@ -411,17 +411,14 @@
 
         try {
             $query  = 'CREATE TABLE ' . $sequenceName
-                    . ' (' . $seqcolName . ' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
-                    . $seqcolName . '))'
-                    . strlen($this->conn->default_table_type) ? ' TYPE = '
-                    . $this->conn->default_table_type : '';
-
+                    . ' (' . $seqcolName . ' BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
+                    . $seqcolName . ')) ' . implode($optionsStrings, ' ');
             $res    = $this->conn->exec($query);
         } catch(Doctrine_Connection_Exception $e) {
             throw new Doctrine_Export_Exception('could not create sequence table');
         }
 
-        if ($start == 1)
+        if ($start == 1 && $res == 1)
             return true;
 
         $query  = 'INSERT INTO ' . $sequenceName
@@ -429,6 +426,9 @@
 
         $res    = $this->conn->exec($query);
 
+        if ($res == 1)
+            return true;
+
         // Handle error
         try {
             $result = $this->conn->exec('DROP TABLE ' . $sequenceName);
@@ -436,7 +436,6 @@
             throw new Doctrine_Export_Exception('could not drop inconsistent sequence table');
         }
 
-
     }
 
     /**
Index: lib/Doctrine/Sequence/Mysql.php
===================================================================
--- lib/Doctrine/Sequence/Mysql.php     (revision 3431)
+++ lib/Doctrine/Sequence/Mysql.php     (working copy)
@@ -42,7 +42,7 @@
      */
     public function nextId($seqName, $onDemand = true)
     {
-        $sequenceName  = $this->conn->quoteIdentifier($this->conn->formatter->getSequenceName($seqName), true);
+        $sequenceName  = $this->conn->quoteIdentifier($seqName, true);
         $seqcolName    = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
         $query         = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)';
         
@@ -103,7 +103,7 @@
      */
     public function currId($seqName)
     {
-        $sequenceName   = $this->conn->quoteIdentifier($this->conn->formatter->getSequenceName($seqName), true);
+        $sequenceName   = $this->conn->quoteIdentifier($seqName, true);
         $seqcolName     = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
         $query          = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName;
 
Index: lib/Doctrine/Table.php
===================================================================
--- lib/Doctrine/Table.php      (revision 3431)
+++ lib/Doctrine/Table.php      (working copy)
@@ -420,13 +420,13 @@
                                 $this->_identifierType = Doctrine::IDENTIFIER_SEQUENCE;
                                 $found = true;
 
-                                if ($value) {
+                                if (is_string($value)) {
                                     $this->_options['sequenceName'] = $value;
                                 } else {
                                     if (($sequence = $this->getAttribute(Doctrine::ATTR_DEFAULT_SEQUENCE)) !== null) {
                                         $this->_options['sequenceName'] = $sequence;
                                     } else {
-                                        $this->_options['sequenceName'] = $this->_conn->getSequenceName($this->_options['tableName']);
+                                        $this->_options['sequenceName'] = $this->_conn->formatter->getSequenceName($this->_options['tableName']);
                                     }
                                 }
                                 break;

Change History

Changed 19 months ago by jupaju

  • description modified (diff)

Changed 18 months ago by jwage

  • milestone set to 1.0

Changed 17 months ago by guilhermeblanco

  • status changed from new to closed
  • resolution set to fixed

(In [3785]) Fix to allow sequences work in MySQL (fixes #702) in 0.9 branch

Changed 17 months ago by guilhermeblanco

(In [3798]) Fixes #702 in 0.10 branch. Still misses trunk

Changed 17 months ago by guilhermeblanco

  • status changed from closed to reopened
  • resolution fixed deleted

Need to fix this in trunk!

Changed 16 months ago by anonymous

  • milestone 1.0 deleted

Milestone 1.0 deleted

Changed 2 months ago by romanb

  • status changed from reopened to closed
  • severity set to abc
  • has_test unset
  • version set to 1.0.7
  • mystatus set to Pending Core Response
  • has_patch unset
  • resolution set to fixed
  • description modified (diff)

This ticket is no longer valid for Doctrine 2.

Note: See TracTickets for help on using tickets.