Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
68a77c40
Commit
68a77c40
authored
Jan 18, 2015
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More progress on db schema setup and migrations, tricky stuff
parent
38f237ef
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
146 additions
and
40 deletions
+146
-40
docker/blocks/mysql/fig
+3
-1
docker/blocks/mysql_tests/fig
+9
-0
docker/fig.yml
+7
-7
pkg/api/account.go
+2
-4
pkg/services/sqlstore/migrations/builder.go
+23
-0
pkg/services/sqlstore/migrations/engine.go
+23
-6
pkg/services/sqlstore/migrations/migrations.go
+13
-2
pkg/services/sqlstore/migrations/migrations_test.go
+53
-20
pkg/services/sqlstore/sqlsyntax/dialect.go
+13
-0
No files found.
docker/blocks/mysql/fig
View file @
68a77c40
db
:
mysql
:
image: mysql:latest
image: mysql:latest
environment:
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: grafana
MYSQL_DATABASE: grafana
MYSQL_USER: grafana
MYSQL_USER: grafana
MYSQL_PASSWORD: password
MYSQL_PASSWORD: password
ports:
- "3306:3306"
docker/blocks/mysql_tests/fig
0 → 100644
View file @
68a77c40
mysqltests:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: grafana_tests
MYSQL_USER: grafana
MYSQL_PASSWORD: password
ports:
- "3306:3306"
docker/fig.yml
View file @
68a77c40
openldap
:
mysqltests
:
image
:
cnry/openldap
image
:
mysql:latest
environment
:
environment
:
SLAPD_PASSWORD
:
grafana
MYSQL_ROOT_PASSWORD
:
rootpass
SLAPD_DOMAIN
:
grafana.org
MYSQL_DATABASE
:
grafana_tests
MYSQL_USER
:
grafana
MYSQL_PASSWORD
:
password
ports
:
ports
:
-
"
389:389"
-
"
3306:3306"
pkg/api/account.go
View file @
68a77c40
...
@@ -8,9 +8,8 @@ import (
...
@@ -8,9 +8,8 @@ import (
func
GetAccount
(
c
*
middleware
.
Context
)
{
func
GetAccount
(
c
*
middleware
.
Context
)
{
query
:=
m
.
GetAccountInfoQuery
{
Id
:
c
.
AccountId
}
query
:=
m
.
GetAccountInfoQuery
{
Id
:
c
.
AccountId
}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to fetch collaboratos"
,
err
)
c
.
JsonApiErr
(
500
,
"Failed to fetch collaboratos"
,
err
)
return
return
}
}
...
@@ -31,9 +30,8 @@ func UpdateAccount(c *middleware.Context, cmd m.UpdateAccountCommand) {
...
@@ -31,9 +30,8 @@ func UpdateAccount(c *middleware.Context, cmd m.UpdateAccountCommand) {
func
GetOtherAccounts
(
c
*
middleware
.
Context
)
{
func
GetOtherAccounts
(
c
*
middleware
.
Context
)
{
query
:=
m
.
GetOtherAccountsQuery
{
AccountId
:
c
.
AccountId
}
query
:=
m
.
GetOtherAccountsQuery
{
AccountId
:
c
.
AccountId
}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to get other accounts"
,
err
)
c
.
JsonApiErr
(
500
,
"Failed to get other accounts"
,
err
)
return
return
}
}
...
...
pkg/services/sqlstore/migrations/builder.go
View file @
68a77c40
...
@@ -3,9 +3,27 @@ package migrations
...
@@ -3,9 +3,27 @@ package migrations
type
migration
struct
{
type
migration
struct
{
desc
string
desc
string
sqlite
string
sqlite
string
mysql
string
verifyTable
string
verifyTable
string
}
}
type
columnType
string
const
(
DB_TYPE_STRING
columnType
=
"String"
)
func
(
m
*
migration
)
getSql
(
dbType
string
)
string
{
switch
dbType
{
case
"mysql"
:
return
m
.
mysql
case
"sqlite3"
:
return
m
.
sqlite
}
panic
(
"db type not supported"
)
}
type
migrationBuilder
struct
{
type
migrationBuilder
struct
{
migration
*
migration
migration
*
migration
}
}
...
@@ -15,6 +33,11 @@ func (b *migrationBuilder) sqlite(sql string) *migrationBuilder {
...
@@ -15,6 +33,11 @@ func (b *migrationBuilder) sqlite(sql string) *migrationBuilder {
return
b
return
b
}
}
func
(
b
*
migrationBuilder
)
mysql
(
sql
string
)
*
migrationBuilder
{
b
.
migration
.
mysql
=
sql
return
b
}
func
(
b
*
migrationBuilder
)
verifyTable
(
name
string
)
*
migrationBuilder
{
func
(
b
*
migrationBuilder
)
verifyTable
(
name
string
)
*
migrationBuilder
{
b
.
migration
.
verifyTable
=
name
b
.
migration
.
verifyTable
=
name
return
b
return
b
...
...
pkg/services/sqlstore/migrations/engine.go
View file @
68a77c40
...
@@ -4,8 +4,12 @@ import (
...
@@ -4,8 +4,12 @@ import (
"errors"
"errors"
"fmt"
"fmt"
"github.com/go-xorm/xorm"
"github.com/torkelo/grafana-pro/pkg/services/sqlstore/sqlsyntax"
"github.com/torkelo/grafana-pro/pkg/services/sqlstore/sqlsyntax"
_
"github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
_
"github.com/lib/pq"
_
"github.com/mattn/go-sqlite3"
)
)
var
x
*
xorm
.
Engine
var
x
*
xorm
.
Engine
...
@@ -29,9 +33,18 @@ func getSchemaVersion() (int, error) {
...
@@ -29,9 +33,18 @@ func getSchemaVersion() (int, error) {
return
v
.
Version
,
err
return
v
.
Version
,
err
}
}
func
StartMigration
(
engine
*
xorm
.
Engine
)
error
{
func
setEngineAndDialect
(
engine
*
xorm
.
Engine
)
{
x
=
engine
x
=
engine
dialect
=
new
(
sqlsyntax
.
Sqlite3
)
switch
x
.
DriverName
()
{
case
"mysql"
:
dialect
=
new
(
sqlsyntax
.
Mysql
)
case
"sqlite3"
:
dialect
=
new
(
sqlsyntax
.
Sqlite3
)
}
}
func
StartMigration
(
engine
*
xorm
.
Engine
)
error
{
setEngineAndDialect
(
engine
)
_
,
err
:=
getSchemaVersion
()
_
,
err
:=
getSchemaVersion
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -49,16 +62,21 @@ func StartMigration(engine *xorm.Engine) error {
...
@@ -49,16 +62,21 @@ func StartMigration(engine *xorm.Engine) error {
func
execMigration
(
m
*
migration
)
error
{
func
execMigration
(
m
*
migration
)
error
{
err
:=
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
err
:=
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
_
,
err
:=
sess
.
Exec
(
m
.
sqlite
)
_
,
err
:=
sess
.
Exec
(
m
.
getSql
(
x
.
DriverName
())
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
return
nil
return
nil
})
})
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
// verify
return
verifyMigration
(
m
)
}
func
verifyMigration
(
m
*
migration
)
error
{
if
m
.
verifyTable
!=
""
{
if
m
.
verifyTable
!=
""
{
sqlStr
,
args
:=
dialect
.
TableCheckSql
(
m
.
verifyTable
)
sqlStr
,
args
:=
dialect
.
TableCheckSql
(
m
.
verifyTable
)
results
,
err
:=
x
.
Query
(
sqlStr
,
args
...
)
results
,
err
:=
x
.
Query
(
sqlStr
,
args
...
)
...
@@ -66,7 +84,6 @@ func execMigration(m *migration) error {
...
@@ -66,7 +84,6 @@ func execMigration(m *migration) error {
return
errors
.
New
(
fmt
.
Sprintf
(
"Verify failed: table %v does not exist"
,
m
.
verifyTable
))
return
errors
.
New
(
fmt
.
Sprintf
(
"Verify failed: table %v does not exist"
,
m
.
verifyTable
))
}
}
}
}
return
nil
return
nil
}
}
...
...
pkg/services/sqlstore/migrations/migrations.go
View file @
68a77c40
...
@@ -4,13 +4,24 @@ var migrationList []*migration
...
@@ -4,13 +4,24 @@ var migrationList []*migration
func
init
()
{
func
init
()
{
new
(
migrationBuilder
)
.
new
(
migrationBuilder
)
.
// ------------------------------
desc
(
"Create account table"
)
.
desc
(
"Create account table"
)
.
sqlite
(
`
sqlite
(
`
CREATE TABLE account (
CREATE TABLE account (
id INTEGER PRIMARY KEY
id INTEGER PRIMARY KEY
AUTOINCREMENT
)
)
`
)
.
`
)
.
verifyTable
(
"account"
)
mysql
(
`
CREATE TABLE account (
id BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id)
)
`
)
.
verifyTable
(
"account"
)
.
add
()
// ------------------------------
// desc("Add name column to account table").
// table("account").addColumn("name").colType(DB_TYPE_STRING)
// sqlite("ALTER TABLE account ADD COLUMN name TEXT").
// mysql("ALTER TABLE account ADD COLUMN name NVARCHAR(255)").
}
}
type
SchemaVersion
struct
{
type
SchemaVersion
struct
{
...
...
pkg/services/sqlstore/migrations/migrations_test.go
View file @
68a77c40
package
migrations
package
migrations
// import (
import
(
// "testing"
"fmt"
//
"testing"
// "github.com/go-xorm/xorm"
//
"github.com/go-xorm/xorm"
// . "github.com/smartystreets/goconvey/convey"
// )
.
"github.com/smartystreets/goconvey/convey"
//
)
// func TestMigrationsSqlite(t *testing.T) {
//
func
cleanDB
(
x
*
xorm
.
Engine
)
{
// Convey("Initial SQLite3 migration", t, func() {
tables
,
_
:=
x
.
DBMetas
()
// x, err := xorm.NewEngine("sqlite3", ":memory:")
sess
:=
x
.
NewSession
()
// StartMigration(x)
defer
sess
.
Close
()
//
// tables, err := x.DBMetas()
for
_
,
table
:=
range
tables
{
// So(err, ShouldBeNil)
if
_
,
err
:=
sess
.
Exec
(
"SET FOREIGN_KEY_CHECKS = 0"
);
err
!=
nil
{
//
panic
(
"Failed to disable foreign key checks"
)
// So(len(tables), ShouldEqual, 1)
}
// })
if
_
,
err
:=
sess
.
Exec
(
"DROP TABLE "
+
table
.
Name
);
err
!=
nil
{
// }
panic
(
fmt
.
Sprintf
(
"Failed to delete table: %v, err: %v"
,
table
.
Name
,
err
))
}
if
_
,
err
:=
sess
.
Exec
(
"SET FOREIGN_KEY_CHECKS = 1"
);
err
!=
nil
{
panic
(
"Failed to disable foreign key checks"
)
}
}
}
func
TestMigrationsSqlite
(
t
*
testing
.
T
)
{
testDBs
:=
[][]
string
{
[]
string
{
"sqlite3"
,
":memory:"
},
[]
string
{
"mysql"
,
"grafana:password@tcp(localhost:3306)/grafana_tests?charset=utf8"
},
}
for
_
,
testDB
:=
range
testDBs
{
Convey
(
"Initial "
+
testDB
[
0
]
+
" migration"
,
t
,
func
()
{
x
,
err
:=
xorm
.
NewEngine
(
testDB
[
0
],
testDB
[
1
])
So
(
err
,
ShouldBeNil
)
if
testDB
[
0
]
==
"mysql"
{
cleanDB
(
x
)
}
StartMigration
(
x
)
tables
,
err
:=
x
.
DBMetas
()
So
(
err
,
ShouldBeNil
)
So
(
len
(
tables
),
ShouldEqual
,
2
)
})
}
}
pkg/services/sqlstore/sqlsyntax/dialect.go
View file @
68a77c40
...
@@ -8,11 +8,24 @@ type Dialect interface {
...
@@ -8,11 +8,24 @@ type Dialect interface {
type
Sqlite3
struct
{
type
Sqlite3
struct
{
}
}
type
Mysql
struct
{
}
func
(
db
*
Sqlite3
)
DBType
()
string
{
func
(
db
*
Sqlite3
)
DBType
()
string
{
return
"sqlite3"
return
"sqlite3"
}
}
func
(
db
*
Mysql
)
DBType
()
string
{
return
"mysql"
}
func
(
db
*
Sqlite3
)
TableCheckSql
(
tableName
string
)
(
string
,
[]
interface
{})
{
func
(
db
*
Sqlite3
)
TableCheckSql
(
tableName
string
)
(
string
,
[]
interface
{})
{
args
:=
[]
interface
{}{
tableName
}
args
:=
[]
interface
{}{
tableName
}
return
"SELECT name FROM sqlite_master WHERE type='table' and name = ?"
,
args
return
"SELECT name FROM sqlite_master WHERE type='table' and name = ?"
,
args
}
}
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`=?"
return
sql
,
args
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment