Skip to content
Permalink
Browse files

Implement missing bits for automatic sequence resetting.

Refs #3206
  • Loading branch information...
markstory committed Sep 18, 2012
1 parent 5d4a6fd commit a1838a0c856ad8357275b7bf5a82cec271e9ef21
Showing with 38 additions and 9 deletions.
  1. +5 −3 lib/Cake/Model/Datasource/Database/Postgres.php
  2. +33 −6 lib/Cake/TestSuite/Fixture/CakeTestFixture.php
@@ -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;
}
@@ -58,6 +58,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.
*
@@ -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) {

0 comments on commit a1838a0

Please sign in to comment.
You can’t perform that action at this time.