Skip to content

Commit b3b85c2

Browse files
authored
Merge pull request #1625 from daniel-rose/bugfix/pgsql-add-unique-index
always create unique indices by constraint (for pgsql platform)
2 parents 021d306 + eedc184 commit b3b85c2

File tree

8 files changed

+85
-2
lines changed

8 files changed

+85
-2
lines changed

src/Propel/Generator/Platform/PgsqlPlatform.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,8 @@ public function getDropIndexDDL(Index $index)
688688
{
689689
if ($index instanceof Unique) {
690690
$pattern = "
691-
ALTER TABLE %s DROP CONSTRAINT %s;
692-
";
691+
ALTER TABLE %s DROP CONSTRAINT %s;
692+
";
693693

694694
return sprintf($pattern,
695695
$this->quoteIdentifier($index->getTable()->getName()),
@@ -723,4 +723,26 @@ public function getIdentifierPhp($columnValueMutator, $connectionVariableName =
723723
return preg_replace('/^/m', $tab, $script);
724724
}
725725

726+
/**
727+
* @param \Propel\Generator\Model\Index $index
728+
*
729+
* @return string
730+
*/
731+
public function getAddIndexDDL(Index $index)
732+
{
733+
if (!$index->isUnique()) {
734+
return parent::getAddIndexDDL($index);
735+
}
736+
737+
$pattern = "
738+
ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s);
739+
";
740+
741+
return sprintf(
742+
$pattern,
743+
$this->quoteIdentifier($index->getTable()->getName()),
744+
$this->quoteIdentifier($index->getName()),
745+
$this->getColumnListDDL($index->getColumnObjects())
746+
);
747+
}
726748
}

tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,12 @@ public function testGetModifyTableIndicesDDL($tableDiff)
175175
$expected = "
176176
DROP INDEX `bar_fk` ON `foo`;
177177
178+
DROP INDEX `bax_unique` ON `foo`;
179+
178180
CREATE INDEX `baz_fk` ON `foo` (`baz`);
179181
182+
CREATE UNIQUE INDEX `bax_bay_unique` ON `foo` (`bax`, `bay`);
183+
180184
DROP INDEX `bar_baz_fk` ON `foo`;
181185
182186
CREATE INDEX `bar_baz_fk` ON `foo` (`id`, `bar`, `baz`);

tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,12 @@ public function testGetModifyTableIndicesDDL($tableDiff)
184184
$expected = "
185185
DROP INDEX `bar_fk` ON `foo`;
186186
187+
DROP INDEX `bax_unique` ON `foo`;
188+
187189
CREATE INDEX `baz_fk` ON `foo` (`baz`);
188190
191+
CREATE UNIQUE INDEX `bax_bay_unique` ON `foo` (`bax`, `bay`);
192+
189193
DROP INDEX `bar_baz_fk` ON `foo`;
190194
191195
CREATE INDEX `bar_baz_fk` ON `foo` (`id`, `bar`, `baz`);

tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ public function testGetModifyTableIndicesDDL($tableDiff)
164164
$expected = "
165165
DROP INDEX bar_fk;
166166
167+
DROP INDEX bax_unique;
168+
167169
CREATE INDEX baz_fk ON foo (baz);
168170
171+
CREATE UNIQUE INDEX bax_bay_unique ON foo (bax,bay);
172+
169173
DROP INDEX bar_baz_fk;
170174
171175
CREATE INDEX bar_baz_fk ON foo (id,bar,baz);

tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,12 @@ public function testGetModifyTableIndicesDDL($tableDiff)
155155
156156
DROP INDEX "bar_fk";
157157
158+
ALTER TABLE "foo" DROP CONSTRAINT "bax_unique";
159+
158160
CREATE INDEX "baz_fk" ON "foo" ("baz");
159161
162+
ALTER TABLE "foo" ADD CONSTRAINT "bax_bay_unique" UNIQUE ("bax","bay");
163+
160164
DROP INDEX "bar_baz_fk";
161165
162166
CREATE INDEX "bar_baz_fk" ON "foo" ("id","bar","baz");

tests/Propel/Tests/Generator/Platform/PgsqlPlatformTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Propel\Generator\Model\IdMethodParameter;
1818
use Propel\Generator\Model\PropelTypes;
1919
use Propel\Generator\Model\Table;
20+
use Propel\Generator\Model\Unique;
2021
use Propel\Generator\Platform\PgsqlPlatform;
2122

2223
/**
@@ -600,6 +601,21 @@ public function testAddIndexDDL($index)
600601
$this->assertEquals($expected, $this->getPlatform()->getAddIndexDDL($index));
601602
}
602603

604+
/**
605+
* @dataProvider providerForTestGetUniqueIndexDDL
606+
*
607+
* @param \Propel\Generator\Model\Unique $index
608+
*
609+
* @return void
610+
*/
611+
public function testAddUniqueIndexDDL(Unique $index): void
612+
{
613+
$expected = '
614+
ALTER TABLE "foo" ADD CONSTRAINT "babar" UNIQUE ("bar1");
615+
';
616+
$this->assertEquals($expected, $this->getPlatform()->getAddIndexDDL($index));
617+
}
618+
603619
/**
604620
* @dataProvider providerForTestGetIndicesDDL
605621
*/

tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,17 @@ public function providerForTestGetModifyTableIndicesDDL()
199199
<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
200200
<column name="bar" type="INTEGER" />
201201
<column name="baz" type="VARCHAR" size="12" required="true" />
202+
<column name="bax" type="VARCHAR" size="12" required="true" />
202203
<index name="bar_fk">
203204
<index-column name="bar"/>
204205
</index>
205206
<index name="bar_baz_fk">
206207
<index-column name="bar"/>
207208
<index-column name="baz"/>
208209
</index>
210+
<unique name="bax_unique">
211+
<unique-column name="bax"/>
212+
</unique>
209213
</table>
210214
</database>
211215
EOF;
@@ -215,6 +219,8 @@ public function providerForTestGetModifyTableIndicesDDL()
215219
<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
216220
<column name="bar" type="INTEGER" />
217221
<column name="baz" type="VARCHAR" size="12" required="true" />
222+
<column name="bay" type="VARCHAR" size="12" required="true" />
223+
<column name="bax" type="VARCHAR" size="12" required="true" />
218224
<index name="bar_baz_fk">
219225
<index-column name="id"/>
220226
<index-column name="bar"/>
@@ -223,6 +229,10 @@ public function providerForTestGetModifyTableIndicesDDL()
223229
<index name="baz_fk">
224230
<index-column name="baz"/>
225231
</index>
232+
<unique name="bax_bay_unique">
233+
<unique-column name="bax"/>
234+
<unique-column name="bay"/>
235+
</unique>
226236
</table>
227237
</database>
228238
EOF;

tests/Propel/Tests/Generator/Platform/PlatformTestProvider.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,25 @@ public function providerForTestGetIndexDDL()
247247
];
248248
}
249249

250+
/**
251+
* @return array
252+
*/
253+
public function providerForTestGetUniqueIndexDDL(): array
254+
{
255+
$table = new Table('foo');
256+
$table->setIdentifierQuoting(true);
257+
$column1 = new Column('bar1');
258+
$column1->getDomain()->copy(new Domain('FOOTYPE'));
259+
$table->addColumn($column1);
260+
$index = new Unique('babar');
261+
$index->addColumn($column1);
262+
$table->addIndex($index);
263+
264+
return [
265+
[$index]
266+
];
267+
}
268+
250269
public function providerForTestPrimaryKeyDDL()
251270
{
252271
$table = new Table('foo');

0 commit comments

Comments
 (0)