Added database config table and schema detection (refs #73)

* only used to store schema version for the moment.
This commit is contained in:
Timo Smit 2019-01-01 19:49:38 +01:00
parent b8688d7fca
commit e66d78bd35
6 changed files with 82 additions and 4 deletions

View file

@ -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` ( CREATE TABLE IF NOT EXISTS `level` (
`id` int(11) NOT NULL, `id` int(11) NOT NULL,
`name` varchar(64) DEFAULT 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 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; ) 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 -- add levels
INSERT INTO `level` (`id`, `name`) VALUES (0, 'Guest'); INSERT INTO `level` (`id`, `name`) VALUES (0, 'Guest');
INSERT INTO `level` (`id`, `name`) VALUES (1, 'Regular'); INSERT INTO `level` (`id`, `name`) VALUES (1, 'Regular');

View file

@ -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` ( CREATE TABLE IF NOT EXISTS `level` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL `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`); 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 -- add levels
BEGIN; BEGIN;
INSERT INTO `level` (`id`, `name`) VALUES (0, 'Guest'); INSERT INTO `level` (`id`, `name`) VALUES (0, 'Guest');

View file

@ -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 -- rename warns to history
ALTER TABLE `warn` ALTER TABLE `warn`
DROP FOREIGN KEY `warn_player`, 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 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; ) 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 -- populate acl
-- add levels -- add levels
INSERT INTO `level` (`id`, `name`) VALUES (0, 'Guest'); INSERT INTO `level` (`id`, `name`) VALUES (0, 'Guest');

View file

@ -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 -- rename warns to history
CREATE TABLE IF NOT EXISTS `history` ( CREATE TABLE IF NOT EXISTS `history` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `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 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 -- populate acl
-- add levels -- add levels
BEGIN; BEGIN;

View file

@ -30,6 +30,16 @@ local env = assert(luasql.mysql())
local con local con
local cur 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 -- players
function mysql.addPlayer(guid, ip, lastSeen, seen) 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)..")")) 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 if not con then
error("could not connect to database") error("could not connect to database")
elseif not mysql.isSchemaExistent() then
mysql.close()
error("schema does not exist")
end end
end end
function mysql.close(doSave) function mysql.close(doSave)
con:close() if con:close() then
env:close() con = nil
if env:close() then
env = nil
return true
end
end
return false
end end
return mysql return mysql

View file

@ -30,6 +30,16 @@ local env = assert(luasql.sqlite3())
local con local con
local cur 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 -- players
function sqlite3.addPlayer(guid, ip, lastSeen, seen) 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)..")")) 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 if not con then
error("could not connect to database") error("could not connect to database")
elseif not sqlite3.isSchemaExistent() then
sqlite3.close()
error("schema does not exist")
end end
-- enable foreign key enforcement -- enable foreign key enforcement
@ -521,8 +534,17 @@ function sqlite3.start()
end end
function sqlite3.close(doSave) function sqlite3.close(doSave)
con:close() if con:close() then
env:close() con = nil
if env:close() then
env = nil
return true
end
end
return false
end end
return sqlite3 return sqlite3