Skip to content

Commit

Permalink
Added database config table and schema detection (refs #73)
Browse files Browse the repository at this point in the history
* only used to store schema version for the moment.
  • Loading branch information
timosmit committed Jan 1, 2019
1 parent b8688d7 commit e66d78b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
8 changes: 8 additions & 0 deletions 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,
Expand Down Expand Up @@ -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');
Expand Down
8 changes: 8 additions & 0 deletions 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
Expand Down Expand Up @@ -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');
Expand Down
9 changes: 9 additions & 0 deletions 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`,
Expand Down Expand Up @@ -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');
Expand Down
9 changes: 9 additions & 0 deletions 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,
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 24 additions & 2 deletions luamods/wolfadmin/db/mysql.lua
Expand Up @@ -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)..")"))
Expand Down Expand Up @@ -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
26 changes: 24 additions & 2 deletions luamods/wolfadmin/db/sqlite3.lua
Expand Up @@ -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)..")"))
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit e66d78b

Please sign in to comment.