From af69fc6cc90d3ce18fb061c6cff302886a2cae68 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Mon, 25 Sep 2023 08:38:19 +0300 Subject: [PATCH 37/37] fcdb: Create sqlite database, if it doesn't exist See osdn #48672 Signed-off-by: Marko Lindqvist --- doc/README.fcdb | 4 ---- lua/database.lua | 17 +++++++++++++++-- server/scripting/script_fcdb.c | 3 ++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/doc/README.fcdb b/doc/README.fcdb index abc5d3a921..9c7d4459a6 100644 --- a/doc/README.fcdb +++ b/doc/README.fcdb @@ -57,10 +57,6 @@ setup.) Now start the server with freeciv-server --Database fc_auth.conf --auth --Newusers -The first time you do this, you need to create the database file and its -tables with the following server command: - /fcdb lua sqlite_createdb() - Now you can create some users by connecting with the client; due to the --Newusers flag, when you connect with the client with a previously unknown username, the server will prompt for a password and save the new account to diff --git a/lua/database.lua b/lua/database.lua index 7ae9e5325a..8bd1362544 100644 --- a/lua/database.lua +++ b/lua/database.lua @@ -83,10 +83,23 @@ local function sqlite_connect() local sql = ls_sqlite3.sqlite3() - -- Load the database parameters. local database = get_option("database") - dbh = assert(sql:connect(database)) + -- Check database existence + local dfile = io.open(database, "r") + if (dfile) then + -- Close the file + dfile:close() + + -- Load the database parameters. + dbh = assert(sql:connect(database)) + else + -- Open the connection before trying to create db through it. + dbh = assert(sql:connect(database)) + + -- Create a fresh database + sqlite_createdb() + end end -- Set up tables for an SQLite database. diff --git a/server/scripting/script_fcdb.c b/server/scripting/script_fcdb.c index a308759a48..66f415f672 100644 --- a/server/scripting/script_fcdb.c +++ b/server/scripting/script_fcdb.c @@ -56,6 +56,7 @@ /* server */ #include "console.h" +#include "srv_main.h" #include "stdinhand.h" /* server/scripting */ @@ -299,7 +300,7 @@ bool script_fcdb_init(const char *fcdb_luafile) return FALSE; } - if (!script_fcdb_call("database_init")) { + if (srvarg.fcdb_enabled && !script_fcdb_call("database_init")) { log_error("Error connecting to the database"); script_fcdb_free(); return FALSE; -- 2.40.1