Skip to content

Commit

Permalink
Fix retrieve sequence name from default value (yiisoft#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Aug 17, 2023
1 parent 5359753 commit 53d7b0e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 2 additions & 6 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,14 @@ public function testSequenceName(): void
$sequenceName = $tableSchema->getSequenceName();
$command->setSql(
<<<SQL
ALTER TABLE "item" ALTER COLUMN "id" SET DEFAULT nextval('item_id_seq_2')
ALTER TABLE "item" ALTER COLUMN "id" SET DEFAULT nextval('nextval_item_id_seq_2')
SQL,
)->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(
<<<SQL
Expand Down
4 changes: 2 additions & 2 deletions tests/Support/Fixture/pgsql.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DROP TABLE IF EXISTS "composite_fk" CASCADE;
DROP TABLE IF EXISTS "order_item" CASCADE;
DROP TABLE IF EXISTS "item" CASCADE;
DROP SEQUENCE IF EXISTS "item_id_seq_2" CASCADE;
DROP SEQUENCE IF EXISTS "nextval_item_id_seq_2" CASCADE;
DROP TABLE IF EXISTS "order_item_with_null_fk" CASCADE;
DROP TABLE IF EXISTS "order" CASCADE;
DROP TABLE IF EXISTS "order_with_null_fk" CASCADE;
Expand Down Expand Up @@ -88,7 +88,7 @@ CREATE TABLE "item" (
name varchar(128) NOT NULL,
category_id integer NOT NULL references "category"(id) on UPDATE CASCADE on DELETE CASCADE
);
CREATE SEQUENCE "item_id_seq_2";
CREATE SEQUENCE "nextval_item_id_seq_2";

CREATE TABLE "order" (
id serial not null primary key,
Expand Down

0 comments on commit 53d7b0e

Please sign in to comment.