diff --git a/documize/api/request/config.go b/documize/api/request/config.go index 9cc99aad..0d8e730f 100644 --- a/documize/api/request/config.go +++ b/documize/api/request/config.go @@ -1,11 +1,9 @@ package request import ( - "fmt" "strings" "github.com/documize/community/wordsmith/environment" - "github.com/documize/community/wordsmith/log" "github.com/documize/community/wordsmith/utility" ) @@ -29,12 +27,11 @@ func ConfigString(area, path string) (ret string) { sql := `SELECT JSON_EXTRACT(details,'$` + path + `') AS item FROM config WHERE area = '` + area + `';` stmt, err := Db.Preparex(sql) - defer utility.Close(stmt) - if err != nil { - log.Error(fmt.Sprintf("Unable to prepare select for ConfigString: %s", sql), err) + //log.Error(fmt.Sprintf("Unable to prepare select for ConfigString: %s", sql), err) return "" } + defer utility.Close(stmt) var item = make([]uint8, 0) diff --git a/documize/api/request/init.go b/documize/api/request/init.go index b62eef3c..3394645d 100644 --- a/documize/api/request/init.go +++ b/documize/api/request/init.go @@ -66,28 +66,27 @@ func init() { // go into setup mode if required if database.Check(Db, connectionString) { log.Info("database.Check(Db) OK") + migrations, err := database.Migrations(ConfigString("DATABASE", "last_migration")) + if err != nil { + log.Error("Unable to find required database migrations: ", err) + os.Exit(2) + } + if len(migrations) > 0 { + if strings.ToLower(upgrade) != "true" { + log.Error("database migrations are required", + errors.New("the -upgrade flag is not 'true'")) + os.Exit(2) + + } + if err := migrations.Migrate(); err != nil { + log.Error("Unable to run database migration: ", err) + os.Exit(2) + } + } } else { log.Info("database.Check(Db) !OK, going into setup mode") } - migrations, err := database.Migrations(ConfigString("DATABASE", "last_migration")) - if err != nil { - log.Error("Unable to find required database migrations: ", err) - os.Exit(2) - } - if len(migrations) > 0 { - if strings.ToLower(upgrade) != "true" { - log.Error("database migrations are required", - errors.New("the -upgrade flag is not 'true'")) - os.Exit(2) - - } - if err := migrations.Migrate(); err != nil { - log.Error("Unable to run database migration: ", err) - os.Exit(2) - } - } - return false // value not changed }) } diff --git a/documize/database/scripts/create.sql b/documize/database/scripts/create.sql index 4a03ac03..6b3bb280 100644 --- a/documize/database/scripts/create.sql +++ b/documize/database/scripts/create.sql @@ -262,3 +262,17 @@ ALTER TABLE label CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE document CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE page CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; */ + +DROP TABLE IF EXISTS `config`; + +CREATE TABLE IF NOT EXISTS `config` ( + `area` CHAR(16) NOT NULL, + `details` JSON, + UNIQUE INDEX `idx_config_area` (`area` ASC) ) ; + +INSERT INTO `config` VALUES ('DOCUMIZE','{\"plugin\": \"PLUGIN\"}'); +INSERT INTO `config` VALUES ('PLUGIN', +'[{\"Comment\": \"Disable (or not) built-in html import (NOTE: no Plugin name)\",\"Disabled\": false,\"API\": \"Convert\",\"Actions\": [\"htm\",\"html\"]},{\"Comment\": \"Disable (or not) built-in Documize API import used from SDK (NOTE: no Plugin name)\",\"Disabled\": false,\"API\": \"Convert\",\"Actions\": [\"documizeapi\"]}]'); + +INSERT INTO `config` VALUES ('DATABASE','{\"last_migration\": \"migrate-00002.sql\"}'); +/* NOTE the line above must be changed every time a new migration is incorporated into this file */ diff --git a/documize/database/scripts/migrate/migrate-00002.sql b/documize/database/scripts/migrate/migrate-00002.sql index ce30a8a9..25c61f34 100644 --- a/documize/database/scripts/migrate/migrate-00002.sql +++ b/documize/database/scripts/migrate/migrate-00002.sql @@ -4,4 +4,9 @@ CREATE TABLE IF NOT EXISTS `config` ( `area` CHAR(16) NOT NULL, `details` JSON, UNIQUE INDEX `idx_config_area` (`area` ASC) ) ; - \ No newline at end of file + +INSERT INTO `config` VALUES ('DOCUMIZE','{\"plugin\": \"PLUGIN\"}'); +INSERT INTO `config` VALUES ('PLUGIN', +'[{\"Comment\": \"Disable (or not) built-in html import (NOTE: no Plugin name)\",\"Disabled\": false,\"API\": \"Convert\",\"Actions\": [\"htm\",\"html\"]},{\"Comment\": \"Disable (or not) built-in Documize API import used from SDK (NOTE: no Plugin name)\",\"Disabled\": false,\"API\": \"Convert\",\"Actions\": [\"documizeapi\"]}]'); + +INSERT INTO `config` VALUES ('DATABASE','{\"last_migration\": \"migrate-00002.sql\"}'); diff --git a/wordsmith/environment/environment.go b/wordsmith/environment/environment.go index d57805cc..64caf177 100644 --- a/wordsmith/environment/environment.go +++ b/wordsmith/environment/environment.go @@ -59,6 +59,8 @@ func GetString(target *string, name string, required bool, usage string, callbac vars.vv = append(vars.vv, varT{target: target, name: name, required: required, callback: callback, value: value, setter: setter}) } +var showSettings = flag.Bool("showsettings", false, "if true, show settings in the log (WARNING: these settings may include passwords)") + // Parse calls flag.Parse() then checks that the required environment variables are all set. // It should be the first thing called by any main() that uses this library. // If all the required variables are not present, it prints an error and calls os.Exit(2) like flag.Parse(). @@ -94,9 +96,11 @@ func Parse(doFirst string) { } typ = "Required" } - if *(v.target) != "" && vars.vv[vi].setter != goInit { - fmt.Fprintf(os.Stdout, "%s setting from '%s' is: '%s'\n", - typ, vars.vv[vi].setter, *(v.target)) + if *showSettings { + if *(v.target) != "" && vars.vv[vi].setter != goInit { + fmt.Fprintf(os.Stdout, "%s setting from '%s' is: '%s'\n", + typ, vars.vv[vi].setter, *(v.target)) + } } } } diff --git a/wordsmith/utility/defclose.go b/wordsmith/utility/defclose.go index cb0fef10..6aafbf47 100644 --- a/wordsmith/utility/defclose.go +++ b/wordsmith/utility/defclose.go @@ -5,7 +5,7 @@ import "github.com/documize/community/wordsmith/log" // Close is a convenience function to close an io.Closer, usually in a defer. func Close(f io.Closer) { - if f != nil { + if f != nil && f != io.Closer(nil) { log.IfErr(f.Close()) } }