diff options
author | =Corey Hulen <corey@hulen.com> | 2015-09-16 21:16:07 -0700 |
---|---|---|
committer | =Corey Hulen <corey@hulen.com> | 2015-09-16 21:16:07 -0700 |
commit | 51c3445e694e68fdecd809f17fbaa64751e5931c (patch) | |
tree | 3d4a0f0ddceccb8f13dd51b4d2759911b8e700be /store | |
parent | 435211870096f6d31378c1b31c3d0818a7a4e710 (diff) | |
download | chat-51c3445e694e68fdecd809f17fbaa64751e5931c.tar.gz chat-51c3445e694e68fdecd809f17fbaa64751e5931c.tar.bz2 chat-51c3445e694e68fdecd809f17fbaa64751e5931c.zip |
Fixing postgres issue and bumping version number
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_store.go | 51 |
1 files changed, 49 insertions, 2 deletions
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( |