Skip to content

Commit

Permalink
Implement missing bits for automatic sequence resetting.
Browse files Browse the repository at this point in the history
Refs #3206
  • Loading branch information
markstory committed Sep 18, 2012
1 parent 5d4a6fd commit a1838a0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
8 changes: 5 additions & 3 deletions lib/Cake/Model/Datasource/Database/Postgres.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -301,9 +301,11 @@ public function getSequence($table, $field = 'id') {
* @return boolean success. * @return boolean success.
*/ */
public function resetSequence($table, $column) { public function resetSequence($table, $column) {
$sequence = $this->value($this->getSequence($table, $column)); $tableName = $this->fullTableName($table, false, false);
$table = $this->fullTableName($table); $fullTable = $this->fullTableName($table);
$this->execute("SELECT setval($sequence, (SELECT MAX(id) FROM $table))");
$sequence = $this->value($this->getSequence($tableName, $column));
$this->execute("SELECT setval($sequence, (SELECT MAX(id) FROM $fullTable))");
return true; return true;
} }


Expand Down
39 changes: 33 additions & 6 deletions lib/Cake/TestSuite/Fixture/CakeTestFixture.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ class CakeTestFixture {
*/ */
public $created = array(); 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. * Instantiate the fixture.
* *
Expand Down Expand Up @@ -110,6 +132,7 @@ public function init() {
$this->fields = $model->schema(true); $this->fields = $model->schema(true);
$this->fields[$model->primaryKey]['key'] = 'primary'; $this->fields[$model->primaryKey]['key'] = 'primary';
$this->table = $db->fullTableName($model, false, false); $this->table = $db->fullTableName($model, false, false);
$this->primaryKey = $model->primaryKey;
ClassRegistry::config(array('ds' => 'test')); ClassRegistry::config(array('ds' => 'test'));
ClassRegistry::flush(); ClassRegistry::flush();
} elseif (isset($import['table'])) { } elseif (isset($import['table'])) {
Expand All @@ -121,6 +144,7 @@ public function init() {
$model->table = $import['table']; $model->table = $import['table'];
$model->tablePrefix = $db->config['prefix']; $model->tablePrefix = $db->config['prefix'];
$this->fields = $model->schema(true); $this->fields = $model->schema(true);
$this->primaryKey = $model->primaryKey;
ClassRegistry::flush(); ClassRegistry::flush();
} }


Expand Down Expand Up @@ -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. * 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 * @return boolean True on success, false on failure
*/ */
public function create($db) { public function create($db) {
Expand Down Expand Up @@ -210,7 +234,7 @@ public function create($db) {
/** /**
* Run after all tests executed, should return SQL statement to drop table for this fixture. * 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 * @return boolean True on success, false on failure
*/ */
public function drop($db) { public function drop($db) {
Expand All @@ -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 * 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. * 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 * @return boolean on success or if there are no records to insert, or false on failure
*/ */
public function insert($db) { public function insert($db) {
Expand All @@ -252,6 +276,9 @@ public function insert($db) {
$nested = $db->useNestedTransactions; $nested = $db->useNestedTransactions;
$db->useNestedTransactions = false; $db->useNestedTransactions = false;
$result = $db->insertMulti($this->table, $fields, $values); $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; $db->useNestedTransactions = $nested;
return $result; return $result;
} }
Expand All @@ -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 * Truncates the current fixture. Can be overwritten by classes extending
* truncate. * 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 * @return boolean
*/ */
public function truncate($db) { public function truncate($db) {
Expand Down

0 comments on commit a1838a0

Please sign in to comment.