From e66d78bd3542fb8344837c90a7ae406f2b6c07a5 Mon Sep 17 00:00:00 2001 From: Timo Smit Date: Tue, 1 Jan 2019 19:49:38 +0100 Subject: [PATCH] Added database config table and schema detection (refs #73) * only used to store schema version for the moment. --- database/new/mysql.sql | 8 ++++++++ database/new/sqlite.sql | 8 ++++++++ database/upgrade/1.1.0/mysql.sql | 9 +++++++++ database/upgrade/1.1.0/sqlite.sql | 9 +++++++++ luamods/wolfadmin/db/mysql.lua | 26 ++++++++++++++++++++++++-- luamods/wolfadmin/db/sqlite3.lua | 26 ++++++++++++++++++++++++-- 6 files changed, 82 insertions(+), 4 deletions(-) diff --git a/database/new/mysql.sql b/database/new/mysql.sql index 16f98cf..2c9dc4e 100644 --- a/database/new/mysql.sql +++ b/database/new/mysql.sql @@ -1,3 +1,8 @@ +CREATE TABLE IF NOT EXISTS `config` ( + `id` varchar(64) NOT NULL PRIMARY KEY, + `value` longtext NOT NULL +); + CREATE TABLE IF NOT EXISTS `level` ( `id` int(11) NOT NULL, `name` varchar(64) DEFAULT NULL, @@ -107,6 +112,9 @@ CREATE TABLE IF NOT EXISTS `record` ( CONSTRAINT `record_player` FOREIGN KEY (`player_id`) REFERENCES `player` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +-- insert database version in config +INSERT INTO `config` (`id`, `value`) VALUES ('schema_version', '1.2.0'); + -- add levels INSERT INTO `level` (`id`, `name`) VALUES (0, 'Guest'); INSERT INTO `level` (`id`, `name`) VALUES (1, 'Regular'); diff --git a/database/new/sqlite.sql b/database/new/sqlite.sql index f91d005..f7d34eb 100644 --- a/database/new/sqlite.sql +++ b/database/new/sqlite.sql @@ -1,3 +1,8 @@ +CREATE TABLE IF NOT EXISTS `config` ( + `id` TEXT NOT NULL PRIMARY KEY, + `value` TEXT NOT NULL +); + CREATE TABLE IF NOT EXISTS `level` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `name` TEXT NOT NULL @@ -103,6 +108,9 @@ CREATE TABLE IF NOT EXISTS `record` ( CREATE INDEX IF NOT EXISTS `record_player_idx` ON `record` (`player_id`); +-- insert database version in config +INSERT INTO `config` (`id`, `value`) VALUES ('schema_version', '1.2.0'); + -- add levels BEGIN; INSERT INTO `level` (`id`, `name`) VALUES (0, 'Guest'); diff --git a/database/upgrade/1.1.0/mysql.sql b/database/upgrade/1.1.0/mysql.sql index 6585772..7476462 100644 --- a/database/upgrade/1.1.0/mysql.sql +++ b/database/upgrade/1.1.0/mysql.sql @@ -1,3 +1,9 @@ +-- create config table +CREATE TABLE IF NOT EXISTS `config` ( + `id` varchar(64) NOT NULL PRIMARY KEY, + `value` longtext NOT NULL +); + -- rename warns to history ALTER TABLE `warn` DROP FOREIGN KEY `warn_player`, @@ -54,6 +60,9 @@ CREATE TABLE IF NOT EXISTS `player_permission` ( CONSTRAINT `player_permission_level` FOREIGN KEY (`player_id`) REFERENCES `player` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +-- insert database version in config +INSERT INTO `config` (`id`, `value`) VALUES ('schema_version', '1.2.0'); + -- populate acl -- add levels INSERT INTO `level` (`id`, `name`) VALUES (0, 'Guest'); diff --git a/database/upgrade/1.1.0/sqlite.sql b/database/upgrade/1.1.0/sqlite.sql index 8ed20dc..dd896f7 100644 --- a/database/upgrade/1.1.0/sqlite.sql +++ b/database/upgrade/1.1.0/sqlite.sql @@ -1,3 +1,9 @@ +-- create config table +CREATE TABLE IF NOT EXISTS `config` ( + `id` TEXT NOT NULL PRIMARY KEY, + `value` TEXT NOT NULL +); + -- rename warns to history CREATE TABLE IF NOT EXISTS `history` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, @@ -41,6 +47,9 @@ CREATE TABLE `player_permission` ( CONSTRAINT `player_permission_player` FOREIGN KEY (`player_id`) REFERENCES `player` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ); +-- insert database version in config +INSERT INTO `config` (`id`, `value`) VALUES ('schema_version', '1.2.0'); + -- populate acl -- add levels BEGIN; diff --git a/luamods/wolfadmin/db/mysql.lua b/luamods/wolfadmin/db/mysql.lua index 515140d..aac1020 100644 --- a/luamods/wolfadmin/db/mysql.lua +++ b/luamods/wolfadmin/db/mysql.lua @@ -30,6 +30,16 @@ local env = assert(luasql.mysql()) local con local cur +-- config +function mysql.isSchemaExistent() + cur = assert(con:execute("SELECT * FROM `information_schema`.`tables` WHERE `table_schema`='"..util.escape(settings.get("db_database")).."' AND `table_name`='config' LIMIT 1")) + + local tbl = cur:fetch({}, "a") + cur:close() + + return tbl and true or false +end + -- players function mysql.addPlayer(guid, ip, lastSeen, seen) cur = assert(con:execute("INSERT INTO `player` (`guid`, `ip`, `level_id`, `lastseen`, `seen`) VALUES ('"..util.escape(guid).."', '"..util.escape(ip).."', 0, "..tonumber(lastSeen)..", "..tonumber(seen)..")")) @@ -505,12 +515,24 @@ function mysql.start() if not con then error("could not connect to database") + elseif not mysql.isSchemaExistent() then + mysql.close() + error("schema does not exist") end end function mysql.close(doSave) - con:close() - env:close() + if con:close() then + con = nil + + if env:close() then + env = nil + + return true + end + end + + return false end return mysql diff --git a/luamods/wolfadmin/db/sqlite3.lua b/luamods/wolfadmin/db/sqlite3.lua index 3d01a04..4f477c7 100644 --- a/luamods/wolfadmin/db/sqlite3.lua +++ b/luamods/wolfadmin/db/sqlite3.lua @@ -30,6 +30,16 @@ local env = assert(luasql.sqlite3()) local con local cur +-- config +function sqlite3.isSchemaExistent() + cur = assert(con:execute("SELECT `name` FROM `sqlite_master` WHERE type='table' AND name='config'")) + + local tbl = cur:fetch({}, "a") + cur:close() + + return tbl and true or false +end + -- players function sqlite3.addPlayer(guid, ip, lastSeen, seen) cur = assert(con:execute("INSERT INTO `player` (`guid`, `ip`, `level_id`, `lastseen`, `seen`) VALUES ('"..util.escape(guid).."', '"..util.escape(ip).."', 0, "..tonumber(lastSeen)..", "..tonumber(seen)..")")) @@ -513,6 +523,9 @@ function sqlite3.start() if not con then error("could not connect to database") + elseif not sqlite3.isSchemaExistent() then + sqlite3.close() + error("schema does not exist") end -- enable foreign key enforcement @@ -521,8 +534,17 @@ function sqlite3.start() end function sqlite3.close(doSave) - con:close() - env:close() + if con:close() then + con = nil + + if env:close() then + env = nil + + return true + end + end + + return false end return sqlite3