add db migration mechanism
This commit is contained in:
		
							parent
							
								
									4415ed90e8
								
							
						
					
					
						commit
						8b6ae897a5
					
				@ -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);
 | 
					 | 
				
			||||||
@ -40,6 +40,7 @@ const (
 | 
				
			|||||||
	SETTING_TYPE_INTEGER = "integer"
 | 
						SETTING_TYPE_INTEGER = "integer"
 | 
				
			||||||
	SETTING_TYPE_STRING  = "alphanumeric"
 | 
						SETTING_TYPE_STRING  = "alphanumeric"
 | 
				
			||||||
	SETTING_TYPE_DATE    = "datetime"
 | 
						SETTING_TYPE_DATE    = "datetime"
 | 
				
			||||||
 | 
						SETTING_TYPE_BOOL    = "boolean"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Setting struct {
 | 
					type Setting struct {
 | 
				
			||||||
@ -88,6 +89,8 @@ func (s *Setting) Validate(value string) bool {
 | 
				
			|||||||
		return s.validateAlphanum(value)
 | 
							return s.validateAlphanum(value)
 | 
				
			||||||
	case SETTING_TYPE_DATE:
 | 
						case SETTING_TYPE_DATE:
 | 
				
			||||||
		return s.validateDatetime(value)
 | 
							return s.validateDatetime(value)
 | 
				
			||||||
 | 
						case SETTING_TYPE_BOOL:
 | 
				
			||||||
 | 
							return s.validateBool(value)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return false
 | 
						return false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -152,3 +155,11 @@ func (s *Setting) validateDatetime(value string) bool {
 | 
				
			|||||||
func (s *Setting) validateAlphanum(value string) bool {
 | 
					func (s *Setting) validateAlphanum(value string) bool {
 | 
				
			||||||
	return len(value) >= 0
 | 
						return len(value) >= 0
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *Setting) validateBool(value string) bool {
 | 
				
			||||||
 | 
						_, err := strconv.ParseBool(value)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								migrations/000001_create_users_table.down.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								migrations/000001_create_users_table.down.sql
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
				
			|||||||
 | 
					DROP TABLE users;
 | 
				
			||||||
 | 
					DROP TABLE sessions;
 | 
				
			||||||
							
								
								
									
										16
									
								
								migrations/000001_create_users_table.up.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								migrations/000001_create_users_table.up.sql
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
							
								
								
									
										3
									
								
								migrations/000002_create_websites_table.down.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								migrations/000002_create_websites_table.down.sql
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					DROP TABLE IF EXISTS guestbook_comments;
 | 
				
			||||||
 | 
					DROP TABLE IF EXISTS guestbooks;
 | 
				
			||||||
 | 
					DROP TABLE IF EXISTS websites;
 | 
				
			||||||
@ -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 (
 | 
					CREATE TABLE websites (
 | 
				
			||||||
    Id integer primary key autoincrement,
 | 
					    Id integer primary key autoincrement,
 | 
				
			||||||
    ShortId integer UNIQUE NOT NULL,
 | 
					    ShortId integer UNIQUE NOT NULL,
 | 
				
			||||||
@ -61,9 +50,3 @@ CREATE TABLE guestbook_comments (
 | 
				
			|||||||
        ON DELETE RESTRICT
 | 
					        ON DELETE RESTRICT
 | 
				
			||||||
        ON UPDATE RESTRICT
 | 
					        ON UPDATE RESTRICT
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
					 | 
				
			||||||
CREATE TABLE sessions (
 | 
					 | 
				
			||||||
    token CHAR(43) primary key,
 | 
					 | 
				
			||||||
    data BLOB NOT NULL,
 | 
					 | 
				
			||||||
    expiry TEXT NOT NULL
 | 
					 | 
				
			||||||
);
 | 
					 | 
				
			||||||
							
								
								
									
										6
									
								
								migrations/000003_create_settings_tables.down.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								migrations/000003_create_settings_tables.down.sql
									
									
									
									
									
										Normal file
									
								
							@ -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;
 | 
				
			||||||
@ -68,9 +68,3 @@ CREATE TABLE guestbook_settings (
 | 
				
			|||||||
        ON UPDATE RESTRICT
 | 
					        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');
 | 
					 | 
				
			||||||
							
								
								
									
										20
									
								
								migrations/000004_insert_settings_info.down.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								migrations/000004_insert_settings_info.down.sql
									
									
									
									
									
										Normal file
									
								
							@ -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';
 | 
				
			||||||
							
								
								
									
										40
									
								
								migrations/000004_insert_settings_info.up.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								migrations/000004_insert_settings_info.up.sql
									
									
									
									
									
										Normal file
									
								
							@ -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';
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user