From 53d7b0e0086dc17dee452ea7c29309ebbdee9ccd Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Thu, 17 Aug 2023 21:31:56 +0700 Subject: [PATCH] Fix retrieve sequence name from default value (#309) --- CHANGELOG.md | 1 + src/Schema.php | 8 ++------ tests/SchemaTest.php | 4 ++-- tests/Support/Fixture/pgsql.sql | 4 ++-- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 094958f1c..adf917ca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Enh #300: Refactor `ArrayExpressionBuilder` (@Tigrov) - Enh #302: Refactor `ColumnSchema` (@Tigrov) - Bug #302: Fix incorrect convert string value for BIT type (@Tigrov) +- Bug #309: Fix retrieving sequence name from default value (@Tigrov) ## 1.1.0 July 24, 2023 diff --git a/src/Schema.php b/src/Schema.php index 4b1b6a809..4861d3de5 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -800,13 +800,9 @@ protected function loadColumnSchema(array $info): ColumnSchemaInterface if ( $defaultValue !== null - && preg_match("/nextval\\('\"?\\w+\"?\.?\"?\\w+\"?'(::regclass)?\\)/", $defaultValue) === 1 + && preg_match("/^nextval\('([^']+)/", $defaultValue, $matches) === 1 ) { - $column->sequenceName(preg_replace( - ['/nextval/', '/::/', '/regclass/', '/\'\)/', '/\(\'/'], - '', - $defaultValue - )); + $column->sequenceName($matches[1]); } elseif ($info['sequence_name'] !== null) { $column->sequenceName($this->resolveTableName($info['sequence_name'])->getFullName()); } diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 293845f8f..93cfea967 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -291,14 +291,14 @@ public function testSequenceName(): void $sequenceName = $tableSchema->getSequenceName(); $command->setSql( <<execute(); $schema->refreshTableSchema('item'); $tableSchema = $schema->getTableSchema('item'); $this->assertNotNull($tableSchema); - $this->assertEquals('item_id_seq_2', $tableSchema->getSequenceName()); + $this->assertEquals('nextval_item_id_seq_2', $tableSchema->getSequenceName()); $command->setSql( <<