Skip to content
This repository
Browse code

Implement missing bits for automatic sequence resetting.

Refs #3206
  • Loading branch information...
commit a1838a0c856ad8357275b7bf5a82cec271e9ef21 1 parent 5d4a6fd
Mark Story authored September 18, 2012
8  lib/Cake/Model/Datasource/Database/Postgres.php
@@ -301,9 +301,11 @@ public function getSequence($table, $field = 'id') {
301 301
  * @return boolean success.
302 302
  */
303 303
 	public function resetSequence($table, $column) {
304  
-		$sequence = $this->value($this->getSequence($table, $column));
305  
-		$table = $this->fullTableName($table);
306  
-		$this->execute("SELECT setval($sequence, (SELECT MAX(id) FROM $table))");
  304
+		$tableName = $this->fullTableName($table, false, false);
  305
+		$fullTable = $this->fullTableName($table);
  306
+
  307
+		$sequence = $this->value($this->getSequence($tableName, $column));
  308
+		$this->execute("SELECT setval($sequence, (SELECT MAX(id) FROM $fullTable))");
307 309
 		return true;
308 310
 	}
309 311
 
39  lib/Cake/TestSuite/Fixture/CakeTestFixture.php
@@ -59,6 +59,28 @@ class CakeTestFixture {
59 59
 	public $created = array();
60 60
 
61 61
 /**
  62
+ * Fields / Schema for the fixture.
  63
+ * This array should match the output of Model::schema()
  64
+ *
  65
+ * @var array
  66
+ */
  67
+	public $fields = array();
  68
+
  69
+/**
  70
+ * Fixture records to be inserted.
  71
+ *
  72
+ * @var array
  73
+ */
  74
+	public $records = array();
  75
+
  76
+/**
  77
+ * The primary key for the table this fixture represents.
  78
+ *
  79
+ * @var string
  80
+ */
  81
+	public $primaryKey = null;
  82
+
  83
+/**
62 84
  * Instantiate the fixture.
63 85
  *
64 86
  * @throws CakeException on invalid datasource usage.
@@ -110,6 +132,7 @@ public function init() {
110 132
 				$this->fields = $model->schema(true);
111 133
 				$this->fields[$model->primaryKey]['key'] = 'primary';
112 134
 				$this->table = $db->fullTableName($model, false, false);
  135
+				$this->primaryKey = $model->primaryKey;
113 136
 				ClassRegistry::config(array('ds' => 'test'));
114 137
 				ClassRegistry::flush();
115 138
 			} elseif (isset($import['table'])) {
@@ -121,6 +144,7 @@ public function init() {
121 144
 				$model->table = $import['table'];
122 145
 				$model->tablePrefix = $db->config['prefix'];
123 146
 				$this->fields = $model->schema(true);
  147
+				$this->primaryKey = $model->primaryKey;
124 148
 				ClassRegistry::flush();
125 149
 			}
126 150
 
@@ -159,7 +183,7 @@ public function init() {
159 183
 /**
160 184
  * Run before all tests execute, should return SQL statement to create table for this fixture could be executed successfully.
161 185
  *
162  
- * @param object	$db	An instance of the database object used to create the fixture table
  186
+ * @param DboSource $db An instance of the database object used to create the fixture table
163 187
  * @return boolean True on success, false on failure
164 188
  */
165 189
 	public function create($db) {
@@ -210,7 +234,7 @@ public function create($db) {
210 234
 /**
211 235
  * Run after all tests executed, should return SQL statement to drop table for this fixture.
212 236
  *
213  
- * @param object	$db	An instance of the database object used to create the fixture table
  237
+ * @param DboSource $db An instance of the database object used to create the fixture table
214 238
  * @return boolean True on success, false on failure
215 239
  */
216 240
 	public function drop($db) {
@@ -232,7 +256,7 @@ public function drop($db) {
232 256
  * Run before each tests is executed, should return a set of SQL statements to insert records for the table
233 257
  * of this fixture could be executed successfully.
234 258
  *
235  
- * @param object $db An instance of the database into which the records will be inserted
  259
+ * @param DboSource $db An instance of the database into which the records will be inserted
236 260
  * @return boolean on success or if there are no records to insert, or false on failure
237 261
  */
238 262
 	public function insert($db) {
@@ -252,6 +276,9 @@ public function insert($db) {
252 276
 				$nested = $db->useNestedTransactions;
253 277
 				$db->useNestedTransactions = false;
254 278
 				$result = $db->insertMulti($this->table, $fields, $values);
  279
+				if ($this->primaryKey && in_array($this->fields[$this->primaryKey]['type'], array('integer', 'biginteger'))) {
  280
+					$db->resetSequence($this->table, $this->primaryKey);
  281
+				}
255 282
 				$db->useNestedTransactions = $nested;
256 283
 				return $result;
257 284
 			}
@@ -260,10 +287,10 @@ public function insert($db) {
260 287
 	}
261 288
 
262 289
 /**
263  
- * Truncates the current fixture. Can be overwritten by classes extending CakeFixture to trigger other events before / after
264  
- * truncate.
  290
+ * Truncates the current fixture. Can be overwritten by classes extending 
  291
+ * CakeFixture to trigger other events before / after truncate.
265 292
  *
266  
- * @param object $db A reference to a db instance
  293
+ * @param DboSource $db A reference to a db instance
267 294
  * @return boolean
268 295
  */
269 296
 	public function truncate($db) {

0 notes on commit a1838a0

Please sign in to comment.
Something went wrong with that request. Please try again.