From 4a2efd998730caafddbb6c5e8514af412ecd8b19 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Wed, 28 Nov 2018 16:24:12 -0500 Subject: [PATCH] Correct the state of PostgreSQL serial sequence during tests --- tests/cases/Db/PostgreSQL/TestDatabase.php | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/cases/Db/PostgreSQL/TestDatabase.php b/tests/cases/Db/PostgreSQL/TestDatabase.php index 372a8b7..7d3d8c8 100644 --- a/tests/cases/Db/PostgreSQL/TestDatabase.php +++ b/tests/cases/Db/PostgreSQL/TestDatabase.php @@ -14,6 +14,27 @@ class TestDatabase extends \JKingWeb\Arsse\TestCase\Database\Base { protected static $implementation = "PDO PostgreSQL"; protected function nextID(string $table): int { - return (int) static::$drv->query("SELECT cast(last_value as bigint) + 1 from pg_sequences where sequencename = '{$table}_id_seq'")->getValue(); + return ((int) static::$drv->query("SELECT last_value from pg_sequences where sequencename = '{$table}_id_seq'")->getValue()) + 1; + } + + public function setUp() { + parent::setUp(); + $seqList = + "select + replace(substring(column_default, 10), right(column_default, 12), '') as seq, + table_name as table, + column_name as col + from information_schema.columns + where table_name like 'arsse_%' + and column_default like 'nextval(%' + "; + foreach(static::$drv->query($seqList) as $r) { + $num = static::$drv->query("SELECT max({$r['col']}) from {$r['table']}")->getValue(); + if (!$num) { + continue; + } + $num++; + static::$drv->exec("ALTER SEQUENCE {$r['seq']} RESTART WITH $num"); + } } }