aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2023-07-20 14:27:26 +0200
committerJoas Schilling <coding@schilljs.com>2023-07-20 14:27:26 +0200
commit0bf738d8cb13d48725f9632ddd642e7cf81f5624 (patch)
tree16c14f65009abf6fdc6943a442b4803f58008303 /lib
parenta4dd35e4429e69ab0b6999aef54c6eb0360f395b (diff)
downloadnextcloud-server-0bf738d8cb13d48725f9632ddd642e7cf81f5624.tar.gz
nextcloud-server-0bf738d8cb13d48725f9632ddd642e7cf81f5624.zip
feat(db): Ensure that index names are unique across the database
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/DB/MigrationService.php55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/private/DB/MigrationService.php b/lib/private/DB/MigrationService.php
index 3afe2f689f9..71d7b51d149 100644
--- a/lib/private/DB/MigrationService.php
+++ b/lib/private/DB/MigrationService.php
@@ -448,6 +448,7 @@ class MigrationService {
if ($toSchema instanceof SchemaWrapper) {
$targetSchema = $toSchema->getWrappedSchema();
+ $this->ensureUniqueNamesConstraints($targetSchema);
if ($this->checkOracle) {
$beforeSchema = $this->connection->createSchema();
$this->ensureOracleConstraints($beforeSchema, $targetSchema, strlen($this->connection->getPrefix()));
@@ -525,6 +526,7 @@ class MigrationService {
if ($toSchema instanceof SchemaWrapper) {
$targetSchema = $toSchema->getWrappedSchema();
+ $this->ensureUniqueNamesConstraints($targetSchema);
if ($this->checkOracle) {
$sourceSchema = $this->connection->createSchema();
$this->ensureOracleConstraints($sourceSchema, $targetSchema, strlen($this->connection->getPrefix()));
@@ -659,6 +661,59 @@ class MigrationService {
}
}
+ /**
+ * Naming constraints:
+ * - Index, sequence and primary key names must be unique within a Postgres Schema
+ *
+ * @param Schema $targetSchema
+ */
+ public function ensureUniqueNamesConstraints(Schema $targetSchema): void {
+ $constraintNames = [];
+
+ $sequences = $targetSchema->getSequences();
+
+ foreach ($targetSchema->getTables() as $table) {
+ foreach ($table->getIndexes() as $thing) {
+ $indexName = strtolower($thing->getName());
+ if ($indexName === 'primary' || $thing->isPrimary()) {
+ continue;
+ }
+
+ if (isset($constraintNames[$thing->getName()])) {
+ throw new \InvalidArgumentException('Index name "' . $thing->getName() . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
+ }
+ $constraintNames[$thing->getName()] = $table->getName();
+ }
+
+ foreach ($table->getForeignKeys() as $thing) {
+ if (isset($constraintNames[$thing->getName()])) {
+ throw new \InvalidArgumentException('Foreign key name "' . $thing->getName() . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
+ }
+ $constraintNames[$thing->getName()] = $table->getName();
+ }
+
+ $primaryKey = $table->getPrimaryKey();
+ if ($primaryKey instanceof Index) {
+ $indexName = strtolower($primaryKey->getName());
+ if ($indexName === 'primary') {
+ continue;
+ }
+
+ if (isset($constraintNames[$indexName])) {
+ throw new \InvalidArgumentException('Primary index name "' . $indexName . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
+ }
+ $constraintNames[$indexName] = $table->getName();
+ }
+ }
+
+ foreach ($sequences as $sequence) {
+ if (isset($constraintNames[$sequence->getName()])) {
+ throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
+ }
+ $constraintNames[$sequence->getName()] = 'sequence';
+ }
+ }
+
private function ensureMigrationsAreLoaded() {
if (empty($this->migrations)) {
$this->migrations = $this->findMigrations();