summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Vrátil <dvratil@kde.org>2016-09-08 10:46:05 (GMT)
committerDaniel Vrátil <dvratil@kde.org>2016-09-08 10:46:05 (GMT)
commit0d123dd796966c50c35556b83bd7ebf2ec702712 (patch)
tree66ec0897c608e44cc2a325c9cc66ab36deb479ec
parent39aff54e8bb6a26116ffffeab37700646a944fc9 (diff)
PSQL: make foreign key DEFERRABLE INITIALLY DEFERRED
-rw-r--r--src/server/storage/datastore.cpp8
-rw-r--r--src/server/storage/dbinitializer_p.cpp3
2 files changed, 10 insertions, 1 deletions
diff --git a/src/server/storage/datastore.cpp b/src/server/storage/datastore.cpp
index ef3a962..37beca1 100644
--- a/src/server/storage/datastore.cpp
+++ b/src/server/storage/datastore.cpp
@@ -1336,6 +1336,14 @@ bool DataStore::beginTransaction()
TRANSACTION_MUTEX_UNLOCK;
return false;
}
+
+ if (DbType::type(m_database) == DbType::PostgreSQL) {
+ // Make constraints check deferred in PostgreSQL. Allows for
+ // INSERT INTO mimetypetable (name) VALUES ('foo') RETURNING id;
+ // INSERT INTO collectionmimetyperelation (collection_id, mimetype_id) VALUES (x, y)
+ // where "y" refers to the newly inserted mimetype
+ m_database.exec(QStringLiteral("SET CONSTRAINTS ALL DEFERRED"));
+ }
}
++m_transactionLevel;
diff --git a/src/server/storage/dbinitializer_p.cpp b/src/server/storage/dbinitializer_p.cpp
index b5a2d2a..57b945c 100644
--- a/src/server/storage/dbinitializer_p.cpp
+++ b/src/server/storage/dbinitializer_p.cpp
@@ -300,7 +300,8 @@ QString DbInitializerPostgreSql::buildAddForeignKeyConstraintStatement(const Tab
const QString constraintName = table.name + column.name + QLatin1Literal("_") + column.refTable + column.refColumn + QLatin1Literal("_fk");
return QLatin1Literal("ALTER TABLE ") + table.name + QLatin1Literal(" ADD CONSTRAINT ") + constraintName + QLatin1Literal(" FOREIGN KEY (") + column.name
+ QLatin1Literal(") REFERENCES ") + column.refTable + QLatin1Literal("Table(") + column.refColumn
- + QLatin1Literal(") ") + buildReferentialAction(column.onUpdate, column.onDelete);
+ + QLatin1Literal(") ") + buildReferentialAction(column.onUpdate, column.onDelete)
+ + QLatin1Literal(" DEFERRABLE INITIALLY DEFERRED");
}
QString DbInitializerPostgreSql::buildRemoveForeignKeyConstraintStatement(const DbIntrospector::ForeignKey &fk, const TableDescription &table) const