From: Michael Tremer Date: Fri, 22 Jan 2021 13:03:53 +0000 (+0000) Subject: libpakfire: db: Add foreign keys to tables X-Git-Tag: 0.9.28~1285^2~821 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=46257c5f148a030c83be6e14f8af57c4c8c26f1a;p=pakfire.git libpakfire: db: Add foreign keys to tables Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/db.c b/src/libpakfire/db.c index 94948002b..96f684505 100644 --- a/src/libpakfire/db.c +++ b/src/libpakfire/db.c @@ -264,7 +264,8 @@ static int pakfire_db_create_schema(struct pakfire_db* db) { "CREATE TABLE IF NOT EXISTS dependencies(" "pkg INTEGER, " "type TEXT, " - "dependency TEXT" + "dependency TEXT, " + "FOREIGN KEY (pkg) REFERENCES packages(id)" ")"); if (r) return r; @@ -289,7 +290,8 @@ static int pakfire_db_create_schema(struct pakfire_db* db) { "'group' TEXT, " "hash1 TEXT, " "mtime INTEGER, " - "capabilities TEXT" + "capabilities TEXT, " + "FOREIGN KEY (pkg) REFERENCES packages(id)" ")"); if (r) return 1; @@ -305,7 +307,8 @@ static int pakfire_db_create_schema(struct pakfire_db* db) { "id INTEGER PRIMARY KEY, " "pkg INTEGER, " "action TEXT, " - "scriptlet TEXT" + "scriptlet TEXT, " + "FOREIGN KEY (pkg) REFERENCES packages(id)" ")"); if (r) return 1; @@ -318,6 +321,14 @@ static int pakfire_db_create_schema(struct pakfire_db* db) { return 0; } +static int pakfire_db_migrate_to_schema_8(struct pakfire_db* db) { + // Add foreign keys + // TODO sqlite doesn't support adding foreign keys to existing tables and so we would + // need to recreate the whole table and rename it afterwards. Annoying. + + return 0; +} + static int pakfire_db_migrate_schema(struct pakfire_db* db) { int r; @@ -337,6 +348,14 @@ static int pakfire_db_migrate_schema(struct pakfire_db* db) { db->schema = CURRENT_SCHEMA; break; + case 7: + r = pakfire_db_migrate_to_schema_8(db); + if (r) + goto ROLLBACK; + + db->schema++; + break; + default: ERROR(db->pakfire, "Cannot migrate database from schema %d\n", db->schema); goto ROLLBACK; @@ -367,6 +386,9 @@ static int pakfire_db_setup(struct pakfire_db* db) { // Setup logging sqlite3_config(SQLITE_CONFIG_LOG, logging_callback, db->pakfire); + // Enable foreign keys + pakfire_db_execute(db, "PRAGMA foreign_keys = ON"); + // Make LIKE case-sensitive pakfire_db_execute(db, "PRAGMA case_sensitive_like = ON");