summaryrefslogtreecommitdiffstats
path: root/store/sql_store.go
diff options
context:
space:
mode:
Diffstat (limited to 'store/sql_store.go')
-rw-r--r--store/sql_store.go110
1 files changed, 97 insertions, 13 deletions
diff --git a/store/sql_store.go b/store/sql_store.go
index a994ec57e..de23f4db3 100644
--- a/store/sql_store.go
+++ b/store/sql_store.go
@@ -139,7 +139,7 @@ func NewSqlStore() Store {
sqlStore.preference.(*SqlPreferenceStore).DeleteUnusedFeatures()
- if model.IsPreviousVersionsSupported(schemaVersion) {
+ if model.IsPreviousVersionsSupported(schemaVersion) && !model.IsCurrentVersion(schemaVersion) {
sqlStore.system.Update(&model.System{Name: "Version", Value: model.CurrentVersion})
l4g.Warn(utils.T("store.sql.upgraded.warn"), model.CurrentVersion)
}
@@ -379,6 +379,49 @@ func (ss SqlStore) RenameColumnIfExists(tableName string, oldColumnName string,
return true
}
+func (ss SqlStore) GetMaxLengthOfColumnIfExists(tableName string, columnName string) string {
+ if !ss.DoesColumnExist(tableName, columnName) {
+ return ""
+ }
+
+ var result string
+ var err error
+ if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL {
+ result, err = ss.GetMaster().SelectStr("SELECT CHARACTER_MAXIMUM_LENGTH FROM information_schema.columns WHERE table_name = '" + tableName + "' AND COLUMN_NAME = '" + columnName + "'")
+ } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES {
+ result, err = ss.GetMaster().SelectStr("SELECT character_maximum_length FROM information_schema.columns WHERE table_name = '" + strings.ToLower(tableName) + "' AND column_name = '" + strings.ToLower(columnName) + "'")
+ }
+
+ if err != nil {
+ l4g.Critical(utils.T("store.sql.maxlength_column.critical"), err)
+ time.Sleep(time.Second)
+ panic(fmt.Sprintf(utils.T("store.sql.maxlength_column.critical"), err.Error()))
+ }
+
+ return result
+}
+
+func (ss SqlStore) AlterColumnTypeIfExists(tableName string, columnName string, mySqlColType string, postgresColType string) bool {
+ if !ss.DoesColumnExist(tableName, columnName) {
+ return false
+ }
+
+ var err error
+ if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL {
+ _, err = ss.GetMaster().Exec("ALTER TABLE " + tableName + " MODIFY " + columnName + " " + mySqlColType)
+ } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES {
+ _, err = ss.GetMaster().Exec("ALTER TABLE " + strings.ToLower(tableName) + " ALTER COLUMN " + strings.ToLower(columnName) + " TYPE " + postgresColType)
+ }
+
+ if err != nil {
+ l4g.Critical(utils.T("store.sql.alter_column_type.critical"), err)
+ time.Sleep(time.Second)
+ panic(fmt.Sprintf(utils.T("store.sql.alter_column_type.critical"), err.Error()))
+ }
+
+ return true
+}
+
func (ss SqlStore) CreateIndexIfNotExists(indexName string, tableName string, columnName string) {
ss.createIndexIfNotExists(indexName, tableName, columnName, INDEX_TYPE_DEFAULT)
}
@@ -440,25 +483,66 @@ func (ss SqlStore) createIndexIfNotExists(indexName string, tableName string, co
}
}
+func (ss SqlStore) RemoveIndexIfExists(indexName string, tableName string) {
+
+ if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES {
+ _, err := ss.GetMaster().SelectStr("SELECT $1::regclass", indexName)
+ // It should fail if the index does not exist
+ if err == nil {
+ return
+ }
+
+ _, err = ss.GetMaster().Exec("DROP INDEX " + indexName)
+ if err != nil {
+ l4g.Critical(utils.T("store.sql.remove_index.critical"), err)
+ time.Sleep(time.Second)
+ panic(fmt.Sprintf(utils.T("store.sql.remove_index.critical"), err.Error()))
+ }
+ } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL {
+
+ count, err := ss.GetMaster().SelectInt("SELECT COUNT(0) AS index_exists FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE() and table_name = ? AND index_name = ?", tableName, indexName)
+ if err != nil {
+ l4g.Critical(utils.T("store.sql.check_index.critical"), err)
+ time.Sleep(time.Second)
+ panic(fmt.Sprintf(utils.T("store.sql.check_index.critical"), err.Error()))
+ }
+
+ if count > 0 {
+ return
+ }
+
+ _, err = ss.GetMaster().Exec("DROP INDEX " + indexName + " ON " + tableName)
+ if err != nil {
+ l4g.Critical(utils.T("store.sql.remove_index.critical"), err)
+ time.Sleep(time.Second)
+ panic(fmt.Sprintf(utils.T("store.sql.remove_index.critical"), err.Error()))
+ }
+ } else {
+ l4g.Critical(utils.T("store.sql.create_index_missing_driver.critical"))
+ time.Sleep(time.Second)
+ panic(utils.T("store.sql.create_index_missing_driver.critical"))
+ }
+}
+
func IsUniqueConstraintError(err string, mysql string, postgres string) bool {
unique := strings.Contains(err, "unique constraint") || strings.Contains(err, "Duplicate entry")
field := strings.Contains(err, mysql) || strings.Contains(err, postgres)
return unique && field
}
-func (ss SqlStore) GetColumnDataType(tableName, columnName string) string {
- dataType, err := ss.GetMaster().SelectStr("SELECT data_type FROM INFORMATION_SCHEMA.COLUMNS where table_name = :Tablename AND column_name = :Columnname", map[string]interface{}{
- "Tablename": tableName,
- "Columnname": columnName,
- })
- if err != nil {
- l4g.Critical(utils.T("store.sql.table_column_type.critical"), columnName, tableName, err.Error())
- time.Sleep(time.Second)
- panic(fmt.Sprintf(utils.T("store.sql.table_column_type.critical"), columnName, tableName, err.Error()))
- }
+// func (ss SqlStore) GetColumnDataType(tableName, columnName string) string {
+// dataType, err := ss.GetMaster().SelectStr("SELECT data_type FROM INFORMATION_SCHEMA.COLUMNS where table_name = :Tablename AND column_name = :Columnname", map[string]interface{}{
+// "Tablename": tableName,
+// "Columnname": columnName,
+// })
+// if err != nil {
+// l4g.Critical(utils.T("store.sql.table_column_type.critical"), columnName, tableName, err.Error())
+// time.Sleep(time.Second)
+// panic(fmt.Sprintf(utils.T("store.sql.table_column_type.critical"), columnName, tableName, err.Error()))
+// }
- return dataType
-}
+// return dataType
+// }
func (ss SqlStore) GetMaster() *gorp.DbMap {
return ss.master