Skip to content
This repository
Browse code

Implement resetSequence()

This is an optional method in DboSource that allows datasources to
resetSequence values in tables.  This is useful for datasources like
Postgres, and makes using fixtures much easier.

Fixes #3026
  • Loading branch information...
commit 55dcb9c3b3913edac06a9befaeef89f627d190df 1 parent cdfead0
Mark Story authored September 17, 2012
16  lib/Cake/Model/Datasource/Database/Postgres.php
@@ -292,6 +292,22 @@ public function getSequence($table, $field = 'id') {
292 292
 	}
293 293
 
294 294
 /**
  295
+ * Reset a sequence based on the MAX() value of $column.  Useful
  296
+ * for resetting sequences after using insertMulti().
  297
+ *
  298
+ * @param string $table The name of the table to update.
  299
+ * @param string $column The column to use when reseting the sequence value, the
  300
+ *   sequence name will be fetched using Postgres::getSequence();
  301
+ * @return boolean success.
  302
+ */
  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))");
  307
+		return true;
  308
+	}
  309
+
  310
+/**
295 311
  * Deletes all the records in a table and drops all associated auto-increment sequences
296 312
  *
297 313
  * @param string|Model $table A string or model class representing the table to be truncated
14  lib/Cake/Model/Datasource/DboSource.php
@@ -2921,6 +2921,20 @@ public function insertMulti($table, $fields, $values) {
2921 2921
 	}
2922 2922
 
2923 2923
 /**
  2924
+ * Reset a sequence based on the MAX() value of $column.  Useful
  2925
+ * for resetting sequences after using insertMulti().
  2926
+ *
  2927
+ * This method should be implmented by datasources that require sequences to be used.
  2928
+ *
  2929
+ * @param string $table The name of the table to update.
  2930
+ * @param string $column The column to use when reseting the sequence value.
  2931
+ * @return boolean success.
  2932
+ */
  2933
+	public function resetSequence($table, $column) {
  2934
+
  2935
+	}
  2936
+
  2937
+/**
2924 2938
  * Returns an array of the indexes in given datasource name.
2925 2939
  *
2926 2940
  * @param string $model Name of model to inspect
22  lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php
@@ -950,4 +950,26 @@ public function testNestedTransaction() {
950 950
 		$this->assertNotEmpty($model->read(null, 1));
951 951
 	}
952 952
 
  953
+	public function testResetSequence() {
  954
+		$model = new Article();
  955
+
  956
+		$table = $this->Dbo->fullTableName($model, false);
  957
+		$fields = array(
  958
+			'id', 'user_id', 'title', 'body', 'published',
  959
+		);
  960
+		$values = array(
  961
+			array(1, 1, 'test', 'first post', false),
  962
+			array(2, 1, 'test 2', 'second post post', false),
  963
+		);
  964
+		$this->Dbo->insertMulti($table, $fields, $values);
  965
+		$sequence = $this->Dbo->getSequence($table);
  966
+		$result = $this->Dbo->rawQuery("SELECT nextval('$sequence')");
  967
+		$original = $result->fetch(PDO::FETCH_ASSOC);
  968
+
  969
+		$this->assertTrue($this->Dbo->resetSequence($table, 'id'));
  970
+		$result = $this->Dbo->rawQuery("SELECT currval('$sequence')");
  971
+		$new = $result->fetch(PDO::FETCH_ASSOC);
  972
+		$this->assertTrue($new['currval'] > $original['nextval'], 'Sequence did not update');
  973
+	}
  974
+
953 975
 }

0 notes on commit 55dcb9c

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