diff --git a/db/create-settings.sql b/db/create-settings.sql deleted file mode 100644 index 87ee494..0000000 --- a/db/create-settings.sql +++ /dev/null @@ -1,9 +0,0 @@ -INSERT INTO setting_groups (Description) VALUES ("guestbook"); -INSERT INTO setting_groups (Description) VALUES ("user"); - -INSERT INTO setting_data_types (Description) VALUES ("alphanumeric") -INSERT INTO setting_data_types (Description) VALUES ("integer") -INSERT INTO setting_data_types (Description) VALUES ("datetime") - -INSERT INTO settings (Description, Constrained, DataType, SettingGroup) -VALUES ("Local Timezone", 0, 1, 1); diff --git a/internal/models/settings.go b/internal/models/settings.go index 9ae8259..684e312 100644 --- a/internal/models/settings.go +++ b/internal/models/settings.go @@ -40,6 +40,7 @@ const ( SETTING_TYPE_INTEGER = "integer" SETTING_TYPE_STRING = "alphanumeric" SETTING_TYPE_DATE = "datetime" + SETTING_TYPE_BOOL = "boolean" ) type Setting struct { @@ -88,6 +89,8 @@ func (s *Setting) Validate(value string) bool { return s.validateAlphanum(value) case SETTING_TYPE_DATE: return s.validateDatetime(value) + case SETTING_TYPE_BOOL: + return s.validateBool(value) } return false } @@ -152,3 +155,11 @@ func (s *Setting) validateDatetime(value string) bool { func (s *Setting) validateAlphanum(value string) bool { return len(value) >= 0 } + +func (s *Setting) validateBool(value string) bool { + _, err := strconv.ParseBool(value) + if err != nil { + return false + } + return true +} diff --git a/migrations/000001_create_users_table.down.sql b/migrations/000001_create_users_table.down.sql new file mode 100644 index 0000000..10b4ee1 --- /dev/null +++ b/migrations/000001_create_users_table.down.sql @@ -0,0 +1,2 @@ +DROP TABLE users; +DROP TABLE sessions; diff --git a/migrations/000001_create_users_table.up.sql b/migrations/000001_create_users_table.up.sql new file mode 100644 index 0000000..89b0312 --- /dev/null +++ b/migrations/000001_create_users_table.up.sql @@ -0,0 +1,16 @@ +CREATE TABLE users ( + Id integer primary key autoincrement, + ShortId integer UNIQUE NOT NULL, + Username varchar(32) NOT NULL, + Email varchar(256) UNIQUE NOT NULL, + Deleted datetime, + IsBanned boolean NOT NULL DEFAULT FALSE, + HashedPassword char(60) NOT NULL, + Created datetime NOT NULL +); + +CREATE TABLE sessions ( + token CHAR(43) primary key, + data BLOB NOT NULL, + expiry TEXT NOT NULL +); diff --git a/migrations/000002_create_websites_table.down.sql b/migrations/000002_create_websites_table.down.sql new file mode 100644 index 0000000..2b1c5b6 --- /dev/null +++ b/migrations/000002_create_websites_table.down.sql @@ -0,0 +1,3 @@ +DROP TABLE IF EXISTS guestbook_comments; +DROP TABLE IF EXISTS guestbooks; +DROP TABLE IF EXISTS websites; diff --git a/db/create-tables-sqlite.sql b/migrations/000002_create_websites_table.up.sql similarity index 78% rename from db/create-tables-sqlite.sql rename to migrations/000002_create_websites_table.up.sql index cf7dc9a..5fc9374 100644 --- a/db/create-tables-sqlite.sql +++ b/migrations/000002_create_websites_table.up.sql @@ -1,14 +1,3 @@ -CREATE TABLE users ( - Id integer primary key autoincrement, - ShortId integer UNIQUE NOT NULL, - Username varchar(32) NOT NULL, - Email varchar(256) UNIQUE NOT NULL, - Deleted datetime, - IsBanned boolean NOT NULL DEFAULT FALSE, - HashedPassword char(60) NOT NULL, - Created datetime NOT NULL -); - CREATE TABLE websites ( Id integer primary key autoincrement, ShortId integer UNIQUE NOT NULL, @@ -61,9 +50,3 @@ CREATE TABLE guestbook_comments ( ON DELETE RESTRICT ON UPDATE RESTRICT ); - -CREATE TABLE sessions ( - token CHAR(43) primary key, - data BLOB NOT NULL, - expiry TEXT NOT NULL -); diff --git a/migrations/000003_create_settings_tables.down.sql b/migrations/000003_create_settings_tables.down.sql new file mode 100644 index 0000000..b682e63 --- /dev/null +++ b/migrations/000003_create_settings_tables.down.sql @@ -0,0 +1,6 @@ +DROP TABLE IF EXISTS guestbook_settings; +DROP TABLE IF EXISTS user_settings; +DROP TABLE IF EXISTS allowed_setting_values; +DROP TABLE IF EXISTS settings; +DROP TABLE IF EXISTS setting_data_types; +DROP TABLE IF EXISTS setting_groups; diff --git a/db/create-settings-tables.sql b/migrations/000003_create_settings_tables.up.sql similarity index 86% rename from db/create-settings-tables.sql rename to migrations/000003_create_settings_tables.up.sql index 4708887..21aea74 100644 --- a/db/create-settings-tables.sql +++ b/migrations/000003_create_settings_tables.up.sql @@ -68,9 +68,3 @@ CREATE TABLE guestbook_settings ( ON UPDATE RESTRICT ); -INSERT INTO setting_groups (Description) VALUES ('guestbook'); -INSERT INTO setting_groups (Description) VALUES ('user'); - -INSERT INTO setting_data_types (Description) VALUES ('alphanumeric'); -INSERT INTO setting_data_types (Description) VALUES ('integer'); -INSERT INTO setting_data_types (Description) VALUES ('datetime'); diff --git a/migrations/000004_insert_settings_info.down.sql b/migrations/000004_insert_settings_info.down.sql new file mode 100644 index 0000000..8f89491 --- /dev/null +++ b/migrations/000004_insert_settings_info.down.sql @@ -0,0 +1,20 @@ +DELETE FROM settings WHERE Description='remote_enabled'; +DELETE FROM settings WHERE Description='is_visible'; +DELETE FROM settings WHERE Description='reenable_comments'; +DELETE FROM settings WHERE Description='commenting_enabled'; +DELETE FROM settings WHERE Description='local_timezone'; + +DELETE FROM setting_data_types WHERE Description='boolean'; +DELETE FROM setting_data_types WHERE Description='datetime'; +DELETE FROM setting_data_types WHERE Description='integer'; +DELETE FROM setting_data_types WHERE Description='alphanumeric'; + +DELETE FROM setting_groups WHERE Description='user'; +DELETE FROM setting_groups WHERE Description='guestbook'; + +DELETE FROM allowed_setting_values WHERE Caption='commenting_enabled'; +DELETE FROM allowed_setting_values WHERE Caption='commenting_disabled'; +DELETE FROM allowed_setting_values WHERE Caption='guestbook_visible'; +DELETE FROM allowed_setting_values WHERE Caption='guestbook_invisible'; +DELETE FROM allowed_setting_values WHERE Caption='remote_enabled'; +DELETE FROM allowed_setting_values WHERE Caption='remote_disabled'; diff --git a/migrations/000004_insert_settings_info.up.sql b/migrations/000004_insert_settings_info.up.sql new file mode 100644 index 0000000..689e8aa --- /dev/null +++ b/migrations/000004_insert_settings_info.up.sql @@ -0,0 +1,40 @@ +INSERT INTO setting_groups (Description) VALUES ('guestbook'); +INSERT INTO setting_groups (Description) VALUES ('user'); + +INSERT INTO setting_data_types (Description) VALUES ('alphanumeric'); +INSERT INTO setting_data_types (Description) VALUES ('integer'); +INSERT INTO setting_data_types (Description) VALUES ('datetime'); +INSERT INTO setting_data_types (Description) VALUES ('boolean'); + +INSERT INTO settings (Description, Constrained, DataType, SettingGroup) + SELECT 'local_timezone', 0, t.id, g.id FROM setting_data_types as t INNER JOIN setting_groups AS g WHERE g.Description='user' AND t.Description='alphanumeric'; + +INSERT INTO settings (Description, Constrained, DataType, SettingGroup) + SELECT 'commenting_enabled', 1, t.id, g.id FROM setting_data_types as t INNER JOIN setting_groups AS g WHERE g.Description='guestbook' AND t.Description='boolean'; + +INSERT INTO allowed_setting_values (SettingId, ItemValue, Caption) + SELECT settings.id, 'true', 'commenting_enabled' FROM settings WHERE settings.Description='commenting_enabled'; + +INSERT INTO allowed_setting_values (SettingId, ItemValue, Caption) + SELECT settings.id, 'false', 'commenting_disabled' FROM settings WHERE settings.Description='commenting_enabled'; + +INSERT INTO settings (Description, Constrained, DataType, SettingGroup) + SELECT 'reenable_comments', 0, t.id, g.id FROM setting_data_types as t INNER JOIN setting_groups as g WHERE g.Description='guestbook' AND t.Description='alphanumeric'; + +INSERT INTO settings (Description, Constrained, DataType, SettingGroup) + SELECT 'is_visible', 1, t.id, g.id FROM setting_data_types as t INNER JOIN setting_groups as g WHERE g.Description='guestbook' AND t.Description='boolean'; + +INSERT INTO allowed_setting_values (SettingId, ItemValue, Caption) + SELECT settings.id, 'true', 'guestbook_visible' FROM settings WHERE settings.Description='is_visible'; + +INSERT INTO allowed_setting_values (SettingId, ItemValue, Caption) + SELECT settings.id, 'false', 'guestbook_invisible' FROM settings WHERE settings.Description='is_visible'; + +INSERT INTO settings (Description, Constrained, DataType, SettingGroup) + SELECT 'remote_enabled', 1, t.id, g.id FROM setting_data_types as t INNER JOIN setting_groups as g WHERE g.Description='guestbook' AND t.Description='boolean'; + +INSERT INTO allowed_setting_values (SettingId, ItemValue, Caption) + SELECT settings.id, 'true', 'remote_enabled' FROM settings WHERE settings.Description='remote_enabled'; + +INSERT INTO allowed_setting_values (SettingId, ItemValue, Caption) + SELECT settings.id, 'false', 'remote_disabled' FROM settings WHERE settings.Description='remote_enabled';