From cef7a1aae4205ebf4fbd8958f1f870ff69759edf Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Wed, 16 Sep 2015 17:37:11 -0700 Subject: PLT-92 Adding server side versioning to the binary --- store/sql_store.go | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'store/sql_store.go') diff --git a/store/sql_store.go b/store/sql_store.go index c0b3c2021..6ba73a0e5 100644 --- a/store/sql_store.go +++ b/store/sql_store.go @@ -56,6 +56,8 @@ func NewSqlStore() Store { utils.Cfg.SqlSettings.Trace) } + //version := sqlStore.GetCurrentSchemaVersion() + // Temporary upgrade code, remove after 0.8.0 release if sqlStore.DoesColumnExist("Sessions", "AltId") { sqlStore.GetMaster().Exec("DROP TABLE IF EXISTS Sessions") @@ -131,6 +133,11 @@ func setupConnection(con_type string, driver string, dataSource string, maxIdle return dbmap } +func (ss SqlStore) GetCurrentSchemaVersion() string { + version, _ := ss.GetMaster().SelectStr("SELECT PropVal FROM MattermostSystem WHERE PropName='SchemaVersion'") + return version +} + func (ss SqlStore) DoesColumnExist(tableName string, columnName string) bool { if utils.Cfg.SqlSettings.DriverName == "postgres" { count, err := ss.GetMaster().SelectInt( -- cgit v1.2.3-1-g7c22 From abda39b6523f2563d4663036f13ad1c24dac161e Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Wed, 16 Sep 2015 19:59:57 -0700 Subject: Adding database schema version --- store/sql_store.go | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'store/sql_store.go') diff --git a/store/sql_store.go b/store/sql_store.go index 6ba73a0e5..1ae722f16 100644 --- a/store/sql_store.go +++ b/store/sql_store.go @@ -39,6 +39,7 @@ type SqlStore struct { audit AuditStore session SessionStore oauth OAuthStore + system SystemStore } func NewSqlStore() Store { @@ -56,7 +57,24 @@ func NewSqlStore() Store { utils.Cfg.SqlSettings.Trace) } - //version := sqlStore.GetCurrentSchemaVersion() + schemaVersion := sqlStore.GetCurrentSchemaVersion() + + // If the version is already set then we are potentially in an 'upgrade needed' state + if schemaVersion != "" { + // Check to see if it's the most current database schema version + if !model.IsCurrentVersion(schemaVersion) { + // If we are upgrading from the previous version then print a warning and continue + if model.IsLastVersion(schemaVersion) { + l4g.Warn("The database schema version of " + schemaVersion + " appears to be out of date") + l4g.Warn("Attempting to upgrade the database schema version to " + model.GetFullVersion()) + } else { + // If this is an 'upgrade needed' state but the user is attempting to skip a version then halt the world + l4g.Critical("The database schema version of " + schemaVersion + " cannot be upgraded. You must not skip a version.") + time.Sleep(time.Second) + panic("The database schema version of " + schemaVersion + " cannot be upgraded. You must not skip a version.") + } + } + } // Temporary upgrade code, remove after 0.8.0 release if sqlStore.DoesColumnExist("Sessions", "AltId") { @@ -70,6 +88,7 @@ func NewSqlStore() Store { sqlStore.audit = NewSqlAuditStore(sqlStore) sqlStore.session = NewSqlSessionStore(sqlStore) sqlStore.oauth = NewSqlOAuthStore(sqlStore) + sqlStore.system = NewSqlSystemStore(sqlStore) sqlStore.master.CreateTablesIfNotExists() @@ -80,6 +99,7 @@ func NewSqlStore() Store { sqlStore.audit.(*SqlAuditStore).UpgradeSchemaIfNeeded() sqlStore.session.(*SqlSessionStore).UpgradeSchemaIfNeeded() sqlStore.oauth.(*SqlOAuthStore).UpgradeSchemaIfNeeded() + sqlStore.system.(*SqlSystemStore).UpgradeSchemaIfNeeded() sqlStore.team.(*SqlTeamStore).CreateIndexesIfNotExists() sqlStore.channel.(*SqlChannelStore).CreateIndexesIfNotExists() @@ -88,6 +108,17 @@ func NewSqlStore() Store { sqlStore.audit.(*SqlAuditStore).CreateIndexesIfNotExists() sqlStore.session.(*SqlSessionStore).CreateIndexesIfNotExists() sqlStore.oauth.(*SqlOAuthStore).CreateIndexesIfNotExists() + sqlStore.system.(*SqlSystemStore).CreateIndexesIfNotExists() + + if model.IsLastVersion(schemaVersion) { + sqlStore.system.Update(&model.System{Name: "Version", Value: model.GetFullVersion()}) + l4g.Warn("The database schema has been upgraded to version " + model.GetFullVersion()) + } + + if schemaVersion == "" { + sqlStore.system.Save(&model.System{Name: "Version", Value: model.GetFullVersion()}) + l4g.Info("The database schema has been set to version " + model.GetFullVersion()) + } return sqlStore } @@ -134,7 +165,7 @@ func setupConnection(con_type string, driver string, dataSource string, maxIdle } func (ss SqlStore) GetCurrentSchemaVersion() string { - version, _ := ss.GetMaster().SelectStr("SELECT PropVal FROM MattermostSystem WHERE PropName='SchemaVersion'") + version, _ := ss.GetMaster().SelectStr("SELECT Value FROM Systems WHERE Name='Version'") return version } @@ -383,6 +414,10 @@ func (ss SqlStore) OAuth() OAuthStore { return ss.oauth } +func (ss SqlStore) System() SystemStore { + return ss.system +} + type mattermConverter struct{} func (me mattermConverter) ToDb(val interface{}) (interface{}, error) { -- cgit v1.2.3-1-g7c22 From 51c3445e694e68fdecd809f17fbaa64751e5931c Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Wed, 16 Sep 2015 21:16:07 -0700 Subject: Fixing postgres issue and bumping version number --- store/sql_store.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'store/sql_store.go') diff --git a/store/sql_store.go b/store/sql_store.go index 1ae722f16..2e679b81a 100644 --- a/store/sql_store.go +++ b/store/sql_store.go @@ -77,8 +77,10 @@ func NewSqlStore() Store { } // Temporary upgrade code, remove after 0.8.0 release - if sqlStore.DoesColumnExist("Sessions", "AltId") { - sqlStore.GetMaster().Exec("DROP TABLE IF EXISTS Sessions") + if sqlStore.DoesTableExist("Sessions") { + if sqlStore.DoesColumnExist("Sessions", "AltId") { + sqlStore.GetMaster().Exec("DROP TABLE IF EXISTS Sessions") + } } sqlStore.team = NewSqlTeamStore(sqlStore) @@ -169,6 +171,51 @@ func (ss SqlStore) GetCurrentSchemaVersion() string { return version } +func (ss SqlStore) DoesTableExist(tableName string) bool { + if utils.Cfg.SqlSettings.DriverName == "postgres" { + count, err := ss.GetMaster().SelectInt( + `SELECT count(relname) FROM pg_class WHERE relname=$1`, + strings.ToLower(tableName), + ) + + if err != nil { + l4g.Critical("Failed to check if table exists %v", err) + time.Sleep(time.Second) + panic("Failed to check if table exists " + err.Error()) + } + + return count > 0 + + } else if utils.Cfg.SqlSettings.DriverName == "mysql" { + + count, err := ss.GetMaster().SelectInt( + `SELECT + COUNT(0) AS table_exists + FROM + information_schema.TABLES + WHERE + TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = ? + `, + tableName, + ) + + if err != nil { + l4g.Critical("Failed to check if table exists %v", err) + time.Sleep(time.Second) + panic("Failed to check if table exists " + err.Error()) + } + + return count > 0 + + } else { + l4g.Critical("Failed to check if column exists because of missing driver") + time.Sleep(time.Second) + panic("Failed to check if column exists because of missing driver") + } + +} + func (ss SqlStore) DoesColumnExist(tableName string, columnName string) bool { if utils.Cfg.SqlSettings.DriverName == "postgres" { count, err := ss.GetMaster().SelectInt( -- cgit v1.2.3-1-g7c22 From af4deb2369ee6251d7f49db1d67808b773dd9db7 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Thu, 17 Sep 2015 13:01:40 -0700 Subject: Making changes to versioning --- store/sql_store.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'store/sql_store.go') diff --git a/store/sql_store.go b/store/sql_store.go index 2e679b81a..3c41cb1fc 100644 --- a/store/sql_store.go +++ b/store/sql_store.go @@ -64,9 +64,9 @@ func NewSqlStore() Store { // Check to see if it's the most current database schema version if !model.IsCurrentVersion(schemaVersion) { // If we are upgrading from the previous version then print a warning and continue - if model.IsLastVersion(schemaVersion) { + if model.IsPreviousVersion(schemaVersion) { l4g.Warn("The database schema version of " + schemaVersion + " appears to be out of date") - l4g.Warn("Attempting to upgrade the database schema version to " + model.GetFullVersion()) + l4g.Warn("Attempting to upgrade the database schema version to " + model.CurrentVersion) } else { // If this is an 'upgrade needed' state but the user is attempting to skip a version then halt the world l4g.Critical("The database schema version of " + schemaVersion + " cannot be upgraded. You must not skip a version.") @@ -112,14 +112,14 @@ func NewSqlStore() Store { sqlStore.oauth.(*SqlOAuthStore).CreateIndexesIfNotExists() sqlStore.system.(*SqlSystemStore).CreateIndexesIfNotExists() - if model.IsLastVersion(schemaVersion) { - sqlStore.system.Update(&model.System{Name: "Version", Value: model.GetFullVersion()}) - l4g.Warn("The database schema has been upgraded to version " + model.GetFullVersion()) + if model.IsPreviousVersion(schemaVersion) { + sqlStore.system.Update(&model.System{Name: "Version", Value: model.CurrentVersion}) + l4g.Warn("The database schema has been upgraded to version " + model.CurrentVersion) } if schemaVersion == "" { - sqlStore.system.Save(&model.System{Name: "Version", Value: model.GetFullVersion()}) - l4g.Info("The database schema has been set to version " + model.GetFullVersion()) + sqlStore.system.Save(&model.System{Name: "Version", Value: model.CurrentVersion}) + l4g.Info("The database schema has been set to version " + model.CurrentVersion) } return sqlStore -- cgit v1.2.3-1-g7c22