|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flow\PostgreSql\Tests\Integration\QueryBuilder\Database; |
| 6 | + |
| 7 | +use Flow\PostgreSql\Client\Infrastructure\PgSql\PgCatalogProvider; |
| 8 | +use Flow\PostgreSql\Schema\Diff\ConstraintComparator; |
| 9 | +use Flow\PostgreSql\Schema\Diff\GreedySimilarityRenameStrategy; |
| 10 | +use Flow\PostgreSql\Schema\Diff\IndexComparator; |
| 11 | +use Flow\PostgreSql\Schema\Diff\SchemaComparator; |
| 12 | +use Flow\PostgreSql\Schema\Diff\SimilarTextStrategy; |
| 13 | +use Flow\PostgreSql\Schema\Diff\TableComparator; |
| 14 | +use Flow\PostgreSql\Schema\Diff\TableStructureComparator; |
| 15 | +use Flow\PostgreSql\Schema\MaterializedView; |
| 16 | +use Flow\PostgreSql\Schema\Schema; |
| 17 | +use Flow\PostgreSql\Schema\View; |
| 18 | +use Flow\PostgreSql\Tests\Integration\PostgreSqlTestCase; |
| 19 | + |
| 20 | +use function array_filter; |
| 21 | +use function array_values; |
| 22 | +use function Flow\PostgreSql\DSL\col; |
| 23 | +use function Flow\PostgreSql\DSL\column; |
| 24 | +use function Flow\PostgreSql\DSL\column_type_serial; |
| 25 | +use function Flow\PostgreSql\DSL\column_type_varchar; |
| 26 | +use function Flow\PostgreSql\DSL\create; |
| 27 | +use function Flow\PostgreSql\DSL\schema_materialized_view; |
| 28 | +use function Flow\PostgreSql\DSL\schema_view; |
| 29 | +use function Flow\PostgreSql\DSL\select; |
| 30 | +use function Flow\PostgreSql\DSL\table; |
| 31 | + |
| 32 | +final class ViewDiffDatabaseTest extends PostgreSqlTestCase |
| 33 | +{ |
| 34 | + private const MATVIEW_SIMPLE = 'flow_postgres_view_diff_matview'; |
| 35 | + |
| 36 | + private const TABLE_SOURCE = 'flow_postgres_view_diff_source'; |
| 37 | + |
| 38 | + private const VIEW_SIMPLE = 'flow_postgres_view_diff_view'; |
| 39 | + |
| 40 | + protected function setUp(): void |
| 41 | + { |
| 42 | + parent::setUp(); |
| 43 | + |
| 44 | + $this |
| 45 | + ->pgsqlContext() |
| 46 | + ->client() |
| 47 | + ->execute( |
| 48 | + create() |
| 49 | + ->table(self::TABLE_SOURCE) |
| 50 | + ->column(column('id', column_type_serial())) |
| 51 | + ->column(column('name', column_type_varchar(100))) |
| 52 | + ->toSql(), |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + protected function tearDown(): void |
| 57 | + { |
| 58 | + $this->pgsqlContext()->dropViewIfExists(self::VIEW_SIMPLE); |
| 59 | + $this->pgsqlContext()->dropMaterializedViewIfExists(self::MATVIEW_SIMPLE); |
| 60 | + $this->pgsqlContext()->dropTableIfExists(self::TABLE_SOURCE); |
| 61 | + |
| 62 | + parent::tearDown(); |
| 63 | + } |
| 64 | + |
| 65 | + public function test_catalog_materialized_view_matches_single_line_definition(): void |
| 66 | + { |
| 67 | + $select = select(col('id'), col('name'))->from(table(self::TABLE_SOURCE)); |
| 68 | + $this |
| 69 | + ->pgsqlContext() |
| 70 | + ->client() |
| 71 | + ->execute(create()->materializedView(self::MATVIEW_SIMPLE)->as($select)->toSql()); |
| 72 | + |
| 73 | + $catalogSchema = (new PgCatalogProvider($this->pgsqlContext()->client(), ['public']))->get()->get('public'); |
| 74 | + $catalogMatViews = array_values(array_filter( |
| 75 | + $catalogSchema->materializedViews, |
| 76 | + static fn(MaterializedView $mv): bool => $mv->name === self::MATVIEW_SIMPLE, |
| 77 | + )); |
| 78 | + static::assertCount(1, $catalogMatViews); |
| 79 | + |
| 80 | + $constraintComparator = new ConstraintComparator(); |
| 81 | + $comparator = new SchemaComparator( |
| 82 | + new TableComparator( |
| 83 | + new IndexComparator(new GreedySimilarityRenameStrategy(new SimilarTextStrategy())), |
| 84 | + $constraintComparator, |
| 85 | + new GreedySimilarityRenameStrategy(new SimilarTextStrategy()), |
| 86 | + ), |
| 87 | + new IndexComparator(new GreedySimilarityRenameStrategy(new SimilarTextStrategy())), |
| 88 | + $constraintComparator, |
| 89 | + new TableStructureComparator(new GreedySimilarityRenameStrategy(new SimilarTextStrategy())), |
| 90 | + ); |
| 91 | + |
| 92 | + $diff = $comparator->compare( |
| 93 | + new Schema('public', materializedViews: [$catalogMatViews[0]]), |
| 94 | + new Schema('public', materializedViews: [ |
| 95 | + schema_materialized_view(self::MATVIEW_SIMPLE, $select->toSql()), |
| 96 | + ]), |
| 97 | + ); |
| 98 | + |
| 99 | + static::assertSame([], $diff->modifiedMaterializedViews); |
| 100 | + } |
| 101 | + |
| 102 | + public function test_catalog_view_matches_single_line_definition(): void |
| 103 | + { |
| 104 | + $select = select(col('id'), col('name'))->from(table(self::TABLE_SOURCE)); |
| 105 | + $this->pgsqlContext()->client()->execute(create()->view(self::VIEW_SIMPLE)->as($select)->toSql()); |
| 106 | + |
| 107 | + $catalogSchema = (new PgCatalogProvider($this->pgsqlContext()->client(), ['public']))->get()->get('public'); |
| 108 | + $catalogViews = array_values(array_filter( |
| 109 | + $catalogSchema->views, |
| 110 | + static fn(View $v): bool => $v->name === self::VIEW_SIMPLE, |
| 111 | + )); |
| 112 | + static::assertCount(1, $catalogViews); |
| 113 | + |
| 114 | + $constraintComparator = new ConstraintComparator(); |
| 115 | + $comparator = new SchemaComparator( |
| 116 | + new TableComparator( |
| 117 | + new IndexComparator(new GreedySimilarityRenameStrategy(new SimilarTextStrategy())), |
| 118 | + $constraintComparator, |
| 119 | + new GreedySimilarityRenameStrategy(new SimilarTextStrategy()), |
| 120 | + ), |
| 121 | + new IndexComparator(new GreedySimilarityRenameStrategy(new SimilarTextStrategy())), |
| 122 | + $constraintComparator, |
| 123 | + new TableStructureComparator(new GreedySimilarityRenameStrategy(new SimilarTextStrategy())), |
| 124 | + ); |
| 125 | + |
| 126 | + $diff = $comparator->compare( |
| 127 | + new Schema('public', views: [$catalogViews[0]]), |
| 128 | + new Schema('public', views: [ |
| 129 | + schema_view(self::VIEW_SIMPLE, $select->toSql(), isUpdatable: $catalogViews[0]->isUpdatable), |
| 130 | + ]), |
| 131 | + ); |
| 132 | + |
| 133 | + static::assertSame([], $diff->modifiedViews); |
| 134 | + } |
| 135 | +} |
0 commit comments