Skip to content
Permalink
Browse files

Add primary and foreign keys separately

Primary key will serve as autoincremented rowid while foreign key to
link to a snapshot or snapshot_port. Also, add cascade delete so that
on deleting a snapshot, corresponding information is also removed.
  • Loading branch information
umeshksingla authored and neverpanic committed Jun 25, 2017
1 parent 84620b7 commit ce9b552c751d694b66ff9f02a37855e19481cbbe
Showing with 18 additions and 10 deletions.
  1. +1 −1 src/cregistry/entry.c
  2. +17 −9 src/cregistry/sql.c
@@ -1387,7 +1387,7 @@ int snapshot_store_ports(reg_registry* reg, reg_entry* entry, reg_error* errPtr)
sqlite3_stmt* stmt = NULL;
if(reg_entry_propget(entries[i], key1, &port_name, &error)
&& reg_entry_propget(entries[i], key2, &requested, &error)){
char* query = "INSERT INTO registry.snapshot_ports (id, port_name, requested) "
char* query = "INSERT INTO registry.snapshot_ports (snapshots_id, port_name, requested) "
"VALUES (?, ?, ?)";
// entry->id is snapshot's id
if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
@@ -207,20 +207,24 @@ int create_tables(sqlite3* db, reg_error* errPtr) {
/* snapshot ports table */
/* a complete copy of all the installed ports for a snapshot */
"CREATE TABLE registry.snapshot_ports ("
"id INTEGER"
"id INTEGER PRIMARY KEY"
", snapshots_id INTEGER"
", port_name TEXT COLLATE NOCASE"
", requested INTEGER"
", FOREIGN KEY(id) REFERENCES snapshots(id))",
", FOREIGN KEY(snapshots_id) REFERENCES snapshots(id))"
" ON DELETE CASCADE",
"CREATE INDEX registry.snapshot_port ON snapshot_ports"
"(id, port_name)",

/* snapshot port variants table */
/* all variants (+, -) of the ports in a snapshot */
"CREATE TABLE registry.snapshot_port_variants ("
"id INTEGER"
"id INTEGER PRIMARY KEY"
", snapshot_ports_id INTEGER"
", variant_name TEXT COLLATE NOCASE"
", variant_sign TEXT"
", FOREIGN KEY(id) REFERENCES snapshot_ports(id))",
", FOREIGN KEY(snapshot_ports_id) REFERENCES snapshot_ports(id))"
" ON DELETE CASCADE",
"CREATE INDEX registry.snapshot_port_variant ON snapshot_port_variants(id)",

"COMMIT",
@@ -745,26 +749,30 @@ int update_db(sqlite3* db, reg_error* errPtr) {
/* snapshots table */
"CREATE TABLE registry.snapshots ("
"id INTEGER PRIMARY KEY"
", created_at DATETIME"
", created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL"
", note TEXT"
")",

/* snapshot ports table */
"CREATE TABLE registry.snapshot_ports ("
"id INTEGER"
"id INTEGER PRIMARY KEY"
", snapshots_id INTEGER"
", port_name TEXT COLLATE NOCASE"
", requested INTEGER"
", FOREIGN KEY(id) REFERENCES snapshots(id))",
", FOREIGN KEY(snapshots_id) REFERENCES snapshots(id))"
" ON DELETE CASCADE",

"CREATE INDEX registry.snapshot_port ON snapshot_ports"
"(id, port_name)",

/* snapshot port variants table */
"CREATE TABLE registry.snapshot_port_variants ("
"id INTEGER"
"id INTEGER PRIMARY KEY"
", snapshot_ports_id INTEGER"
", variant_name TEXT COLLATE NOCASE"
", variant_sign TEXT"
", FOREIGN KEY(id) REFERENCES snapshot_ports(id))",
", FOREIGN KEY(snapshot_ports_id) REFERENCES snapshot_ports(id))"
" ON DELETE CASCADE",

"CREATE INDEX registry.snapshot_port_variant ON snapshot_port_variants(id)",

0 comments on commit ce9b552

Please sign in to comment.
You can’t perform that action at this time.