| 
									
										
										
										
											2015-01-20 21:15:48 +08:00
										 |  |  | package migrator | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2017-03-28 20:34:53 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2018-05-10 22:54:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-29 20:08:32 +08:00
										 |  |  | 	"github.com/lib/pq" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-22 23:56:12 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util/errutil" | 
					
						
							| 
									
										
										
										
											2020-04-01 21:57:21 +08:00
										 |  |  | 	"xorm.io/xorm" | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type Postgres struct { | 
					
						
							|  |  |  | 	BaseDialect | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 22:54:21 +08:00
										 |  |  | func NewPostgresDialect(engine *xorm.Engine) *Postgres { | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 	d := Postgres{} | 
					
						
							|  |  |  | 	d.BaseDialect.dialect = &d | 
					
						
							| 
									
										
										
										
											2018-05-10 22:54:21 +08:00
										 |  |  | 	d.BaseDialect.engine = engine | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 	d.BaseDialect.driverName = POSTGRES | 
					
						
							|  |  |  | 	return &d | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 21:44:37 +08:00
										 |  |  | func (db *Postgres) SupportEngine() bool { | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | func (db *Postgres) Quote(name string) string { | 
					
						
							|  |  |  | 	return "\"" + name + "\"" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 02:25:01 +08:00
										 |  |  | func (b *Postgres) LikeStr() string { | 
					
						
							|  |  |  | 	return "ILIKE" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | func (db *Postgres) AutoIncrStr() string { | 
					
						
							|  |  |  | 	return "" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 14:07:14 +08:00
										 |  |  | func (db *Postgres) BooleanStr(value bool) string { | 
					
						
							|  |  |  | 	return strconv.FormatBool(value) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-14 17:19:53 +08: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-28 04:42:49 +08:00
										 |  |  | 		return "TRUE" | 
					
						
							| 
									
										
										
										
											2015-12-14 17:19:53 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return col.Default | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08: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 21:15:48 +08:00
										 |  |  | 	case DB_MediumInt, DB_Int, DB_Integer: | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 		if c.IsAutoIncrement { | 
					
						
							|  |  |  | 			return DB_Serial | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return DB_Integer | 
					
						
							| 
									
										
										
										
											2015-01-20 21:15:48 +08:00
										 |  |  | 	case DB_Serial, DB_BigSerial: | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 		c.IsAutoIncrement = true | 
					
						
							|  |  |  | 		c.Nullable = false | 
					
						
							|  |  |  | 		res = t | 
					
						
							| 
									
										
										
										
											2015-01-20 21:15:48 +08:00
										 |  |  | 	case DB_Binary, DB_VarBinary: | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08: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 21:15:48 +08:00
										 |  |  | 	case DB_TinyText, DB_MediumText, DB_LongText: | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 		res = DB_Text | 
					
						
							|  |  |  | 	case DB_NVarchar: | 
					
						
							|  |  |  | 		res = DB_Varchar | 
					
						
							|  |  |  | 	case DB_Uuid: | 
					
						
							|  |  |  | 		res = DB_Uuid | 
					
						
							| 
									
										
										
										
											2015-01-20 21:15:48 +08:00
										 |  |  | 	case DB_Blob, DB_TinyBlob, DB_MediumBlob, DB_LongBlob: | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 		return DB_Bytea | 
					
						
							|  |  |  | 	case DB_Double: | 
					
						
							|  |  |  | 		return "DOUBLE PRECISION" | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		if c.IsAutoIncrement { | 
					
						
							|  |  |  | 			return DB_Serial | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		res = t | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-28 04:14:36 +08:00
										 |  |  | 	var hasLen1 = (c.Length > 0) | 
					
						
							|  |  |  | 	var hasLen2 = (c.Length2 > 0) | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 	if hasLen2 { | 
					
						
							|  |  |  | 		res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")" | 
					
						
							|  |  |  | 	} else if hasLen1 { | 
					
						
							|  |  |  | 		res += "(" + strconv.Itoa(c.Length) + ")" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return res | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-19 04:47:45 +08: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 16:20:44 +08:00
										 |  |  | 	return sql, args | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (db *Postgres) DropIndexSql(tableName string, index *Index) string { | 
					
						
							|  |  |  | 	quote := db.Quote | 
					
						
							|  |  |  | 	idxName := index.XName(tableName) | 
					
						
							| 
									
										
										
										
											2019-04-15 23:49:24 +08:00
										 |  |  | 	return fmt.Sprintf("DROP INDEX %v CASCADE", quote(idxName)) | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-28 20:34:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (db *Postgres) UpdateTableSql(tableName string, columns []*Column) string { | 
					
						
							|  |  |  | 	var statements = []string{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, col := range columns { | 
					
						
							| 
									
										
										
										
											2018-05-10 22:54:21 +08:00
										 |  |  | 		statements = append(statements, "ALTER "+db.Quote(col.Name)+" TYPE "+db.SqlType(col)) | 
					
						
							| 
									
										
										
										
											2017-03-28 20:34:53 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return "ALTER TABLE " + db.Quote(tableName) + " " + strings.Join(statements, ", ") + ";" | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-10 22:54:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (db *Postgres) CleanDB() error { | 
					
						
							|  |  |  | 	sess := db.engine.NewSession() | 
					
						
							|  |  |  | 	defer sess.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if _, err := sess.Exec("DROP SCHEMA public CASCADE;"); err != nil { | 
					
						
							| 
									
										
										
										
											2020-07-09 13:14:35 +08:00
										 |  |  | 		return fmt.Errorf("Failed to drop schema public") | 
					
						
							| 
									
										
										
										
											2018-05-10 22:54:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if _, err := sess.Exec("CREATE SCHEMA public;"); err != nil { | 
					
						
							| 
									
										
										
										
											2020-07-09 13:14:35 +08:00
										 |  |  | 		return fmt.Errorf("Failed to create schema public") | 
					
						
							| 
									
										
										
										
											2018-05-10 22:54:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-27 18:07:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-13 21:36:09 +08:00
										 |  |  | func (db *Postgres) isThisError(err error, errcode string) bool { | 
					
						
							| 
									
										
										
										
											2018-09-27 18:07:43 +08:00
										 |  |  | 	if driverErr, ok := err.(*pq.Error); ok { | 
					
						
							| 
									
										
										
										
											2019-06-13 21:36:09 +08:00
										 |  |  | 		if string(driverErr.Code) == errcode { | 
					
						
							| 
									
										
										
										
											2018-09-27 18:07:43 +08:00
										 |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-13 21:36:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-20 21:48:38 +08:00
										 |  |  | func (db *Postgres) ErrorMessage(err error) string { | 
					
						
							|  |  |  | 	if driverErr, ok := err.(*pq.Error); ok { | 
					
						
							|  |  |  | 		return driverErr.Message | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return "" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-13 21:36:09 +08: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 23:56:12 +08: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 | 
					
						
							|  |  |  | } |