From 5a57a15f5e154b2e2dc312b1e92600449c6ca1f9 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 6 Mar 2016 17:05:47 -0500 Subject: enforce foreign keys --- src/guff/database-updater.cr | 2 +- src/guff/database.cr | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/guff/database-updater.cr b/src/guff/database-updater.cr index daa3ebd..c1d4188 100644 --- a/src/guff/database-updater.cr +++ b/src/guff/database-updater.cr @@ -129,7 +129,7 @@ module Guff def run # get version from database - db_version = @db.table_exists?("metadata") ? get_version : 0_i32 + db_version = @db.table_exists?("metadata") ? get_version : 0 app_version = MIGRATIONS.size - 1 if db_version < app_version diff --git a/src/guff/database.cr b/src/guff/database.cr index 5b971b6..6882712 100644 --- a/src/guff/database.cr +++ b/src/guff/database.cr @@ -11,8 +11,28 @@ module Guff WHERE type = 'table' AND name = ? ", + + pragma_foreign_keys: " + PRAGMA foreign_keys = true + ", } + def initialize(path) + super(path) + query(SQL[:pragma_foreign_keys]) + end + + def initialize(path, &block) + super(path) + query(SQL[:pragma_foreign_keys]) + + begin + block.call(self) + ensure + close unless closed? + end + end + def table_exists?(table : String) : Bool one(SQL[:table_exists], [table]) == table end -- cgit v1.2.3