Skip to content

Commit

Permalink
Merge pull request #5407 from jongotlin/jongotlin-patch-1
Browse files Browse the repository at this point in the history
Create sequences before tables
  • Loading branch information
derrabus authored May 23, 2022
2 parents 007223c + 8ba42c0 commit 4c1926e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Schema/Visitor/CreateSchemaSqlCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public function getQueries()
{
return array_merge(
$this->createNamespaceQueries,
$this->createTableQueries,
$this->createSequenceQueries,
$this->createTableQueries,
$this->createFkConstraintQueries
);
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Functional/Schema/PostgreSQL/SchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Doctrine\DBAL\Tests\Functional\Schema\PostgreSQL;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\IntegerType;

final class SchemaTest extends FunctionalTestCase
{
public function testCreateTableWithSequenceInColumnDefinition(): void
{
$platform = $this->connection->getDatabasePlatform();

if (! $platform instanceof PostgreSQLPlatform) {
self::markTestSkipped('Test is for PostgreSQL.');
}

$this->dropTableIfExists('my_table');

$options = ['default' => 'nextval(\'my_table_id_seq\'::regclass)'];
$table = new Table('my_table', [new Column('id', new IntegerType(), $options)]);
$sequence = new Sequence('my_table_id_seq');

$schema = new Schema([$table], [$sequence]);
foreach ($schema->toSql($platform) as $sql) {
$this->connection->executeStatement($sql);
}

$result = $this->connection->fetchAssociative(
'SELECT column_default FROM information_schema.columns WHERE table_name = ?',
['my_table']
);

$this->assertNotFalse($result);
$this->assertEquals('nextval(\'my_table_id_seq\'::regclass)', $result['column_default']);
}
}
4 changes: 4 additions & 0 deletions tests/Functional/Schema/SchemaManagerFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,8 @@ public function testCommentStringsAreQuoted(): void
self::markTestSkipped('Database does not support column comments.');
}

$this->dropTableIfExists('my_table');

$table = new Table('my_table');
$table->addColumn('id', 'integer', ['comment' => "It's a comment with a quote"]);
$table->setPrimaryKey(['id']);
Expand All @@ -1273,6 +1275,8 @@ public function testCommentNotDuplicated(): void
self::markTestSkipped('Database does not support column comments.');
}

$this->dropTableIfExists('my_table');

$options = [
'type' => Type::getType('integer'),
'default' => 0,
Expand Down
2 changes: 1 addition & 1 deletion tests/Schema/Visitor/SchemaSqlCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testCreateSchema(): void

$sql = $schema->toSql($platformMock);

self::assertEquals(['foo', 'foo', 'bar', 'baz'], $sql);
self::assertEquals(['bar', 'foo', 'foo', 'baz'], $sql);
}

public function testDropSchema(): void
Expand Down

0 comments on commit 4c1926e

Please sign in to comment.