From 8bb9126b77f9108301ea9b8a1a96d3025cf4febd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 20 Jan 2015 09:20:44 +0100 Subject: [PATCH] added postgres support for db migrations --- conf/grafana.ini | 2 +- docker/blocks/graphite/fig | 2 +- docker/blocks/postgres_tests/fig | 7 ++ docker/fig.yml | 8 ++ .../sqlstore/migrations/migrations_test.go | 1 + pkg/services/sqlstore/migrations/migrator.go | 2 + .../sqlstore/migrations/mysql_dialect.go | 14 +-- .../sqlstore/migrations/postgres_dialect.go | 86 +++++++++++++++++++ 8 files changed, 107 insertions(+), 15 deletions(-) create mode 100644 docker/blocks/postgres_tests/fig create mode 100644 pkg/services/sqlstore/migrations/postgres_dialect.go diff --git a/conf/grafana.ini b/conf/grafana.ini index 26931572eb3..64a8bb84ded 100644 --- a/conf/grafana.ini +++ b/conf/grafana.ini @@ -84,7 +84,7 @@ mode = console ; Buffer length of channel, keep it as it is if you don't know what it is. buffer_len = 10000 ; Either "Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "Trace" -level = Trace +level = Info ; For "console" mode only [log.console] diff --git a/docker/blocks/graphite/fig b/docker/blocks/graphite/fig index 1f6381e6847..28e7d3c53a2 100644 --- a/docker/blocks/graphite/fig +++ b/docker/blocks/graphite/fig @@ -1,4 +1,4 @@ graphite: - build: blocks/docker_graphite + build: blocks/graphite ports: - "8776:80" diff --git a/docker/blocks/postgres_tests/fig b/docker/blocks/postgres_tests/fig new file mode 100644 index 00000000000..049afe185c8 --- /dev/null +++ b/docker/blocks/postgres_tests/fig @@ -0,0 +1,7 @@ +postgrestest: + image: postgres:latest + environment: + POSTGRES_USER: grafanatest + POSTGRES_PASSWORD: grafanatest + ports: + - "5432:5432" diff --git a/docker/fig.yml b/docker/fig.yml index efaed13c4c9..fa47a0ce914 100644 --- a/docker/fig.yml +++ b/docker/fig.yml @@ -8,3 +8,11 @@ mysqltests: ports: - "3306:3306" +postgrestest: + image: postgres:latest + environment: + POSTGRES_USER: grafanatest + POSTGRES_PASSWORD: grafanatest + ports: + - "5432:5432" + diff --git a/pkg/services/sqlstore/migrations/migrations_test.go b/pkg/services/sqlstore/migrations/migrations_test.go index 406eac8da09..2a182523ee7 100644 --- a/pkg/services/sqlstore/migrations/migrations_test.go +++ b/pkg/services/sqlstore/migrations/migrations_test.go @@ -35,6 +35,7 @@ func TestMigrations(t *testing.T) { log.NewLogger(0, "console", `{"level": 0}`) testDBs := [][]string{ + []string{"postgres", "user=grafanatest password=grafanatest host=localhost port=5432 dbname=grafanatest sslmode=disable"}, []string{"mysql", "grafana:password@tcp(localhost:3306)/grafana_tests?charset=utf8"}, []string{"sqlite3", ":memory:"}, } diff --git a/pkg/services/sqlstore/migrations/migrator.go b/pkg/services/sqlstore/migrations/migrator.go index abe05af8d99..47a88e992ed 100644 --- a/pkg/services/sqlstore/migrations/migrator.go +++ b/pkg/services/sqlstore/migrations/migrator.go @@ -38,6 +38,8 @@ func NewMigrator(engine *xorm.Engine) *Migrator { mg.dialect = NewMysqlDialect() case SQLITE: mg.dialect = NewSqlite3Dialect() + case POSTGRES: + mg.dialect = NewPostgresDialect() } return mg diff --git a/pkg/services/sqlstore/migrations/mysql_dialect.go b/pkg/services/sqlstore/migrations/mysql_dialect.go index 782dc59f3f1..0b26365a16e 100644 --- a/pkg/services/sqlstore/migrations/mysql_dialect.go +++ b/pkg/services/sqlstore/migrations/mysql_dialect.go @@ -1,9 +1,6 @@ package migrations -import ( - "fmt" - "strconv" -) +import "strconv" type Mysql struct { BaseDialect @@ -71,15 +68,6 @@ func (db *Mysql) SqlType(c *Column) string { return res } -func (db *Mysql) ToDBTypeSql(columnType ColumnType, length int) string { - switch columnType { - case DB_TYPE_STRING: - return fmt.Sprintf("NVARCHAR(%d)", length) - } - - panic("Unsupported db type") -} - func (db *Mysql) TableCheckSql(tableName string) (string, []interface{}) { args := []interface{}{"grafana", tableName} sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?" diff --git a/pkg/services/sqlstore/migrations/postgres_dialect.go b/pkg/services/sqlstore/migrations/postgres_dialect.go new file mode 100644 index 00000000000..5b085b457e1 --- /dev/null +++ b/pkg/services/sqlstore/migrations/postgres_dialect.go @@ -0,0 +1,86 @@ +package migrations + +import ( + "strconv" + + "github.com/go-xorm/core" +) + +type Postgres struct { + BaseDialect +} + +func NewPostgresDialect() *Postgres { + d := Postgres{} + d.BaseDialect.dialect = &d + d.BaseDialect.driverName = POSTGRES + return &d +} + +func (db *Postgres) Quote(name string) string { + return "\"" + name + "\"" +} + +func (db *Postgres) QuoteStr() string { + return "\"" +} + +func (db *Postgres) AutoIncrStr() string { + return "" +} + +func (db *Postgres) SqlType(c *Column) string { + var res string + switch t := c.Type; t { + case DB_TinyInt: + res = DB_SmallInt + return res + case DB_MediumInt, core.Int, core.Integer: + if c.IsAutoIncrement { + return DB_Serial + } + return DB_Integer + case DB_Serial, core.BigSerial: + c.IsAutoIncrement = true + c.Nullable = false + res = t + case DB_Binary, core.VarBinary: + return DB_Bytea + case DB_DateTime: + res = DB_TimeStamp + case DB_TimeStampz: + return "timestamp with time zone" + case DB_Float: + res = DB_Real + case DB_TinyText, core.MediumText, core.LongText: + res = DB_Text + case DB_NVarchar: + res = DB_Varchar + case DB_Uuid: + res = DB_Uuid + case DB_Blob, core.TinyBlob, core.MediumBlob, core.LongBlob: + return DB_Bytea + case DB_Double: + return "DOUBLE PRECISION" + default: + if c.IsAutoIncrement { + return DB_Serial + } + res = t + } + + var hasLen1 bool = (c.Length > 0) + var hasLen2 bool = (c.Length2 > 0) + if hasLen2 { + res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")" + } else if hasLen1 { + res += "(" + strconv.Itoa(c.Length) + ")" + } + return res +} + +func (db *Postgres) TableCheckSql(tableName string) (string, []interface{}) { + args := []interface{}{"grafana", tableName} + sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?" + return sql, args +}