Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #852 from cakephp/2.3-reset-sequence

Implement resetSequence()

Fixes #3206
  • Loading branch information...
commit 144761c0b2a3173ca5b7dc657c8de68b2c18cbd5 2 parents d999a83 + 55dcb9c
@markstory markstory authored
View
16 lib/Cake/Model/Datasource/Database/Postgres.php
@@ -292,6 +292,22 @@ public function getSequence($table, $field = 'id') {
}
/**
+ * Reset a sequence based on the MAX() value of $column. Useful
+ * for resetting sequences after using insertMulti().
+ *
+ * @param string $table The name of the table to update.
+ * @param string $column The column to use when reseting the sequence value, the
+ * sequence name will be fetched using Postgres::getSequence();
+ * @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))");
+ return true;
+ }
+
+/**
* Deletes all the records in a table and drops all associated auto-increment sequences
*
* @param string|Model $table A string or model class representing the table to be truncated
View
14 lib/Cake/Model/Datasource/DboSource.php
@@ -2921,6 +2921,20 @@ public function insertMulti($table, $fields, $values) {
}
/**
+ * Reset a sequence based on the MAX() value of $column. Useful
+ * for resetting sequences after using insertMulti().
+ *
+ * This method should be implmented by datasources that require sequences to be used.
+ *
+ * @param string $table The name of the table to update.
+ * @param string $column The column to use when reseting the sequence value.
+ * @return boolean success.
+ */
+ public function resetSequence($table, $column) {
+
+ }
+
+/**
* Returns an array of the indexes in given datasource name.
*
* @param string $model Name of model to inspect
View
22 lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php
@@ -950,4 +950,26 @@ public function testNestedTransaction() {
$this->assertNotEmpty($model->read(null, 1));
}
+ public function testResetSequence() {
+ $model = new Article();
+
+ $table = $this->Dbo->fullTableName($model, false);
+ $fields = array(
+ 'id', 'user_id', 'title', 'body', 'published',
+ );
+ $values = array(
+ array(1, 1, 'test', 'first post', false),
+ array(2, 1, 'test 2', 'second post post', false),
+ );
+ $this->Dbo->insertMulti($table, $fields, $values);
+ $sequence = $this->Dbo->getSequence($table);
+ $result = $this->Dbo->rawQuery("SELECT nextval('$sequence')");
+ $original = $result->fetch(PDO::FETCH_ASSOC);
+
+ $this->assertTrue($this->Dbo->resetSequence($table, 'id'));
+ $result = $this->Dbo->rawQuery("SELECT currval('$sequence')");
+ $new = $result->fetch(PDO::FETCH_ASSOC);
+ $this->assertTrue($new['currval'] > $original['nextval'], 'Sequence did not update');
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.