Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement missing bits for automatic sequence resetting.

Refs #3206
  • Loading branch information...
commit a1838a0c856ad8357275b7bf5a82cec271e9ef21 1 parent 5d4a6fd
Mark Story markstory authored
8 lib/Cake/Model/Datasource/Database/Postgres.php
View
@@ -301,9 +301,11 @@ public function getSequence($table, $field = 'id') {
* @return boolean success.
*/
public function resetSequence($table, $column) {
- $sequence = $this->value($this->getSequence($table, $column));
- $table = $this->fullTableName($table);
- $this->execute("SELECT setval($sequence, (SELECT MAX(id) FROM $table))");
+ $tableName = $this->fullTableName($table, false, false);
+ $fullTable = $this->fullTableName($table);
+
+ $sequence = $this->value($this->getSequence($tableName, $column));
+ $this->execute("SELECT setval($sequence, (SELECT MAX(id) FROM $fullTable))");
return true;
}
39 lib/Cake/TestSuite/Fixture/CakeTestFixture.php
View
@@ -59,6 +59,28 @@ class CakeTestFixture {
public $created = array();
/**
+ * Fields / Schema for the fixture.
+ * This array should match the output of Model::schema()
+ *
+ * @var array
+ */
+ public $fields = array();
+
+/**
+ * Fixture records to be inserted.
+ *
+ * @var array
+ */
+ public $records = array();
+
+/**
+ * The primary key for the table this fixture represents.
+ *
+ * @var string
+ */
+ public $primaryKey = null;
+
+/**
* Instantiate the fixture.
*
* @throws CakeException on invalid datasource usage.
@@ -110,6 +132,7 @@ public function init() {
$this->fields = $model->schema(true);
$this->fields[$model->primaryKey]['key'] = 'primary';
$this->table = $db->fullTableName($model, false, false);
+ $this->primaryKey = $model->primaryKey;
ClassRegistry::config(array('ds' => 'test'));
ClassRegistry::flush();
} elseif (isset($import['table'])) {
@@ -121,6 +144,7 @@ public function init() {
$model->table = $import['table'];
$model->tablePrefix = $db->config['prefix'];
$this->fields = $model->schema(true);
+ $this->primaryKey = $model->primaryKey;
ClassRegistry::flush();
}
@@ -159,7 +183,7 @@ public function init() {
/**
* Run before all tests execute, should return SQL statement to create table for this fixture could be executed successfully.
*
- * @param object $db An instance of the database object used to create the fixture table
+ * @param DboSource $db An instance of the database object used to create the fixture table
* @return boolean True on success, false on failure
*/
public function create($db) {
@@ -210,7 +234,7 @@ public function create($db) {
/**
* Run after all tests executed, should return SQL statement to drop table for this fixture.
*
- * @param object $db An instance of the database object used to create the fixture table
+ * @param DboSource $db An instance of the database object used to create the fixture table
* @return boolean True on success, false on failure
*/
public function drop($db) {
@@ -232,7 +256,7 @@ public function drop($db) {
* Run before each tests is executed, should return a set of SQL statements to insert records for the table
* of this fixture could be executed successfully.
*
- * @param object $db An instance of the database into which the records will be inserted
+ * @param DboSource $db An instance of the database into which the records will be inserted
* @return boolean on success or if there are no records to insert, or false on failure
*/
public function insert($db) {
@@ -252,6 +276,9 @@ public function insert($db) {
$nested = $db->useNestedTransactions;
$db->useNestedTransactions = false;
$result = $db->insertMulti($this->table, $fields, $values);
+ if ($this->primaryKey && in_array($this->fields[$this->primaryKey]['type'], array('integer', 'biginteger'))) {
+ $db->resetSequence($this->table, $this->primaryKey);
+ }
$db->useNestedTransactions = $nested;
return $result;
}
@@ -260,10 +287,10 @@ public function insert($db) {
}
/**
- * Truncates the current fixture. Can be overwritten by classes extending CakeFixture to trigger other events before / after
- * truncate.
+ * Truncates the current fixture. Can be overwritten by classes extending
+ * CakeFixture to trigger other events before / after truncate.
*
- * @param object $db A reference to a db instance
+ * @param DboSource $db A reference to a db instance
* @return boolean
*/
public function truncate($db) {
Please sign in to comment.
Something went wrong with that request. Please try again.