2015-01-20 07:15:48 -06:00
|
|
|
package migrator
|
2015-01-20 02:20:44 -06:00
|
|
|
|
2015-02-24 04:46:34 -06:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-20 08:48:38 -05:00
|
|
|
"github.com/lib/pq"
|
2015-02-24 04:46:34 -06:00
|
|
|
"strconv"
|
2017-03-28 07:34:53 -05:00
|
|
|
"strings"
|
2018-05-10 09:54:21 -05:00
|
|
|
|
2020-01-22 09:56:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
2020-04-01 08:57:21 -05:00
|
|
|
"xorm.io/xorm"
|
2015-02-24 04:46:34 -06:00
|
|
|
)
|
2015-01-20 02:20:44 -06:00
|
|
|
|
|
|
|
type Postgres struct {
|
|
|
|
BaseDialect
|
|
|
|
}
|
|
|
|
|
2018-05-10 09:54:21 -05:00
|
|
|
func NewPostgresDialect(engine *xorm.Engine) *Postgres {
|
2015-01-20 02:20:44 -06:00
|
|
|
d := Postgres{}
|
|
|
|
d.BaseDialect.dialect = &d
|
2018-05-10 09:54:21 -05:00
|
|
|
d.BaseDialect.engine = engine
|
2015-01-20 02:20:44 -06:00
|
|
|
d.BaseDialect.driverName = POSTGRES
|
|
|
|
return &d
|
|
|
|
}
|
|
|
|
|
2015-01-20 07:44:37 -06:00
|
|
|
func (db *Postgres) SupportEngine() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-01-20 02:20:44 -06:00
|
|
|
func (db *Postgres) Quote(name string) string {
|
|
|
|
return "\"" + name + "\""
|
|
|
|
}
|
|
|
|
|
2015-08-10 13:25:01 -05:00
|
|
|
func (b *Postgres) LikeStr() string {
|
|
|
|
return "ILIKE"
|
|
|
|
}
|
|
|
|
|
2015-01-20 02:20:44 -06:00
|
|
|
func (db *Postgres) AutoIncrStr() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-09-23 01:07:14 -05:00
|
|
|
func (db *Postgres) BooleanStr(value bool) string {
|
|
|
|
return strconv.FormatBool(value)
|
|
|
|
}
|
|
|
|
|
2015-12-14 03:19:53 -06:00
|
|
|
func (b *Postgres) Default(col *Column) string {
|
|
|
|
if col.Type == DB_Bool {
|
|
|
|
if col.Default == "0" {
|
|
|
|
return "FALSE"
|
|
|
|
}
|
Outdent code after if block that ends with return (golint)
This commit fixes the following golint warnings:
pkg/bus/bus.go:64:9: if block ends with a return statement, so drop this else and outdent its block
pkg/bus/bus.go:84:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:137:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:177:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:183:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:199:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:208:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/components/dynmap/dynmap.go:236:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:242:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:257:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:263:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:278:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:284:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:299:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:331:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:350:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:356:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:366:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:390:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:396:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:405:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:427:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:433:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:442:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:459:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:465:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:474:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:491:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:497:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:506:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:523:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:529:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:538:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:555:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:561:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:570:12: if block ends with a return statement, so drop this else and outdent its block
pkg/login/ldap.go:55:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/login/ldap_test.go:372:10: if block ends with a return statement, so drop this else and outdent its block
pkg/middleware/middleware_test.go:213:12: if block ends with a return statement, so drop this else and outdent its block
pkg/plugins/dashboard_importer.go:153:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/dashboards_updater.go:39:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/dashboards_updater.go:121:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/plugins.go:210:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/plugins.go:235:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/eval_context.go:111:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:92:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:98:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:122:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:108:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:118:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:121:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifiers/telegram.go:94:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/annotation.go:34:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/annotation.go:99:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/dashboard_test.go:107:13: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/plugin_setting.go:78:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/preferences.go:91:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/user.go:50:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/migrator/migrator.go:106:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/migrator/postgres_dialect.go:48:10: if block ends with a return statement, so drop this else and outdent its block
pkg/tsdb/time_range.go:59:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/tsdb/time_range.go:67:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/tsdb/cloudwatch/metric_find_query.go:225:9: if block ends with a return statement, so drop this else and outdent its block
pkg/util/filepath.go:68:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
2018-04-27 15:42:49 -05:00
|
|
|
return "TRUE"
|
2015-12-14 03:19:53 -06:00
|
|
|
}
|
|
|
|
return col.Default
|
|
|
|
}
|
|
|
|
|
2015-01-20 02:20:44 -06:00
|
|
|
func (db *Postgres) SqlType(c *Column) string {
|
|
|
|
var res string
|
|
|
|
switch t := c.Type; t {
|
|
|
|
case DB_TinyInt:
|
|
|
|
res = DB_SmallInt
|
|
|
|
return res
|
2015-01-20 07:15:48 -06:00
|
|
|
case DB_MediumInt, DB_Int, DB_Integer:
|
2015-01-20 02:20:44 -06:00
|
|
|
if c.IsAutoIncrement {
|
|
|
|
return DB_Serial
|
|
|
|
}
|
|
|
|
return DB_Integer
|
2015-01-20 07:15:48 -06:00
|
|
|
case DB_Serial, DB_BigSerial:
|
2015-01-20 02:20:44 -06:00
|
|
|
c.IsAutoIncrement = true
|
|
|
|
c.Nullable = false
|
|
|
|
res = t
|
2015-01-20 07:15:48 -06:00
|
|
|
case DB_Binary, DB_VarBinary:
|
2015-01-20 02:20:44 -06:00
|
|
|
return DB_Bytea
|
|
|
|
case DB_DateTime:
|
|
|
|
res = DB_TimeStamp
|
|
|
|
case DB_TimeStampz:
|
|
|
|
return "timestamp with time zone"
|
|
|
|
case DB_Float:
|
|
|
|
res = DB_Real
|
2015-01-20 07:15:48 -06:00
|
|
|
case DB_TinyText, DB_MediumText, DB_LongText:
|
2015-01-20 02:20:44 -06:00
|
|
|
res = DB_Text
|
|
|
|
case DB_NVarchar:
|
|
|
|
res = DB_Varchar
|
|
|
|
case DB_Uuid:
|
|
|
|
res = DB_Uuid
|
2015-01-20 07:15:48 -06:00
|
|
|
case DB_Blob, DB_TinyBlob, DB_MediumBlob, DB_LongBlob:
|
2015-01-20 02:20:44 -06:00
|
|
|
return DB_Bytea
|
|
|
|
case DB_Double:
|
|
|
|
return "DOUBLE PRECISION"
|
|
|
|
default:
|
|
|
|
if c.IsAutoIncrement {
|
|
|
|
return DB_Serial
|
|
|
|
}
|
|
|
|
res = t
|
|
|
|
}
|
|
|
|
|
2018-04-27 15:14:36 -05:00
|
|
|
var hasLen1 = (c.Length > 0)
|
|
|
|
var hasLen2 = (c.Length2 > 0)
|
2015-01-20 02:20:44 -06:00
|
|
|
if hasLen2 {
|
|
|
|
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
|
|
|
|
} else if hasLen1 {
|
|
|
|
res += "(" + strconv.Itoa(c.Length) + ")"
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-12-18 14:47:45 -06:00
|
|
|
func (db *Postgres) IndexCheckSql(tableName, indexName string) (string, []interface{}) {
|
|
|
|
args := []interface{}{tableName, indexName}
|
|
|
|
sql := "SELECT 1 FROM " + db.Quote("pg_indexes") + " WHERE" + db.Quote("tablename") + "=? AND " + db.Quote("indexname") + "=?"
|
2015-01-20 02:20:44 -06:00
|
|
|
return sql, args
|
|
|
|
}
|
2015-02-24 04:46:34 -06:00
|
|
|
|
|
|
|
func (db *Postgres) DropIndexSql(tableName string, index *Index) string {
|
|
|
|
quote := db.Quote
|
|
|
|
idxName := index.XName(tableName)
|
2019-04-15 10:49:24 -05:00
|
|
|
return fmt.Sprintf("DROP INDEX %v CASCADE", quote(idxName))
|
2015-02-24 04:46:34 -06:00
|
|
|
}
|
2017-03-28 07:34:53 -05:00
|
|
|
|
|
|
|
func (db *Postgres) UpdateTableSql(tableName string, columns []*Column) string {
|
|
|
|
var statements = []string{}
|
|
|
|
|
|
|
|
for _, col := range columns {
|
2018-05-10 09:54:21 -05:00
|
|
|
statements = append(statements, "ALTER "+db.Quote(col.Name)+" TYPE "+db.SqlType(col))
|
2017-03-28 07:34:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return "ALTER TABLE " + db.Quote(tableName) + " " + strings.Join(statements, ", ") + ";"
|
|
|
|
}
|
2018-05-10 09:54:21 -05:00
|
|
|
|
|
|
|
func (db *Postgres) CleanDB() error {
|
|
|
|
sess := db.engine.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
if _, err := sess.Exec("DROP SCHEMA public CASCADE;"); err != nil {
|
|
|
|
return fmt.Errorf("Failed to drop schema public")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sess.Exec("CREATE SCHEMA public;"); err != nil {
|
|
|
|
return fmt.Errorf("Failed to create schema public")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-27 05:07:43 -05:00
|
|
|
|
2019-06-13 08:36:09 -05:00
|
|
|
func (db *Postgres) isThisError(err error, errcode string) bool {
|
2018-09-27 05:07:43 -05:00
|
|
|
if driverErr, ok := err.(*pq.Error); ok {
|
2019-06-13 08:36:09 -05:00
|
|
|
if string(driverErr.Code) == errcode {
|
2018-09-27 05:07:43 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2019-06-13 08:36:09 -05:00
|
|
|
|
2020-04-20 08:48:38 -05:00
|
|
|
func (db *Postgres) ErrorMessage(err error) string {
|
|
|
|
if driverErr, ok := err.(*pq.Error); ok {
|
|
|
|
return driverErr.Message
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-06-13 08:36:09 -05:00
|
|
|
func (db *Postgres) IsUniqueConstraintViolation(err error) bool {
|
|
|
|
return db.isThisError(err, "23505")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Postgres) IsDeadlock(err error) bool {
|
|
|
|
return db.isThisError(err, "40P01")
|
|
|
|
}
|
2020-01-22 09:56:12 -06:00
|
|
|
|
|
|
|
func (db *Postgres) PostInsertId(table string, sess *xorm.Session) error {
|
|
|
|
if table != "org" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sync primary key sequence of org table
|
|
|
|
if _, err := sess.Exec("SELECT setval('org_id_seq', (SELECT max(id) FROM org));"); err != nil {
|
|
|
|
return errutil.Wrapf(err, "failed to sync primary key for org table")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|