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
2379c5b7
Commit
2379c5b7
authored
Jan 20, 2015
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added loglevel to migrator, added dashboard table & index migrations
parent
581efa85
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
62 additions
and
17 deletions
+62
-17
pkg/services/sqlstore/migrations/migrations.go
+41
-11
pkg/services/sqlstore/migrations/migrations_test.go
+2
-2
pkg/services/sqlstore/migrations/migrator.go
+18
-3
pkg/services/sqlstore/sqlstore.go
+1
-1
No files found.
pkg/services/sqlstore/migrations/migrations.go
View file @
2379c5b7
...
...
@@ -3,8 +3,13 @@ package migrations
import
"time"
func
AddMigrations
(
mg
*
Migrator
)
{
addMigrationLogMigrations
(
mg
)
addUserMigrations
(
mg
)
addAccountMigrations
(
mg
)
addDashboardMigration
(
mg
)
}
//------- migration_log table -------------------
func
addMigrationLogMigrations
(
mg
*
Migrator
)
{
mg
.
AddMigration
(
"create migration_log table"
,
new
(
AddTableMigration
)
.
Name
(
"migration_log"
)
.
WithColumns
(
&
Column
{
Name
:
"id"
,
Type
:
DB_BigInt
,
IsPrimaryKey
:
true
,
IsAutoIncrement
:
true
},
...
...
@@ -14,8 +19,9 @@ func AddMigrations(mg *Migrator) {
&
Column
{
Name
:
"error"
,
Type
:
DB_Text
},
&
Column
{
Name
:
"timestamp"
,
Type
:
DB_DateTime
},
))
}
//------- user table -------------------
func
addUserMigrations
(
mg
*
Migrator
)
{
mg
.
AddMigration
(
"create user table"
,
new
(
AddTableMigration
)
.
Name
(
"user"
)
.
WithColumns
(
&
Column
{
Name
:
"id"
,
Type
:
DB_BigInt
,
IsPrimaryKey
:
true
,
IsAutoIncrement
:
true
},
...
...
@@ -30,17 +36,19 @@ func AddMigrations(mg *Migrator) {
&
Column
{
Name
:
"created"
,
Type
:
DB_DateTime
,
Nullable
:
false
},
&
Column
{
Name
:
"updated"
,
Type
:
DB_DateTime
,
Nullable
:
false
},
))
//------- user table indexes ------------------
mg
.
AddMigration
(
"add unique index UIX_user.login"
,
new
(
AddIndexMigration
)
.
Name
(
"UIX_user_login"
)
.
Table
(
"user"
)
.
Columns
(
"login"
))
mg
.
AddMigration
(
"add unique index UIX_user.email"
,
new
(
AddIndexMigration
)
.
Name
(
"UIX_user_email"
)
.
Table
(
"user"
)
.
Columns
(
"email"
))
}
//------- account table -------------------
func
addAccountMigrations
(
mg
*
Migrator
)
{
mg
.
AddMigration
(
"create account table"
,
new
(
AddTableMigration
)
.
Name
(
"account"
)
.
WithColumns
(
&
Column
{
Name
:
"id"
,
Type
:
DB_BigInt
,
IsPrimaryKey
:
true
,
IsAutoIncrement
:
true
},
&
Column
{
Name
:
"name"
,
Type
:
DB_NVarchar
,
Length
:
255
},
&
Column
{
Name
:
"name"
,
Type
:
DB_NVarchar
,
Length
:
255
,
Nullable
:
false
},
&
Column
{
Name
:
"created"
,
Type
:
DB_DateTime
,
Nullable
:
false
},
&
Column
{
Name
:
"updated"
,
Type
:
DB_DateTime
,
Nullable
:
false
},
))
...
...
@@ -61,14 +69,36 @@ func AddMigrations(mg *Migrator) {
mg
.
AddMigration
(
"add unique index UIX_account_user"
,
new
(
AddIndexMigration
)
.
Name
(
"UIX_account_user"
)
.
Table
(
"account_user"
)
.
Columns
(
"account_id"
,
"user_id"
))
}
type
Dashboard
struct
{
Id
int64
Slug
string
`xorm:"index(IX_AccountIdSlug)"`
AccountId
int64
`xorm:"index(IX_AccountIdSlug)"`
Created
time
.
Time
Updated
time
.
Time
Title
string
Data
map
[
string
]
interface
{}
}
type
MigrationLog
struct
{
Id
int64
MigrationId
string
Sql
string
Success
bool
Error
string
Timestamp
time
.
Time
func
addDashboardMigration
(
mg
*
Migrator
)
{
mg
.
AddMigration
(
"create dashboard table"
,
new
(
AddTableMigration
)
.
Name
(
"dashboard"
)
.
WithColumns
(
&
Column
{
Name
:
"id"
,
Type
:
DB_BigInt
,
IsPrimaryKey
:
true
,
IsAutoIncrement
:
true
},
&
Column
{
Name
:
"slug"
,
Type
:
DB_NVarchar
,
Length
:
255
,
Nullable
:
false
},
&
Column
{
Name
:
"title"
,
Type
:
DB_NVarchar
,
Length
:
255
,
Nullable
:
false
},
&
Column
{
Name
:
"data"
,
Type
:
DB_Text
,
Nullable
:
false
},
&
Column
{
Name
:
"account_id"
,
Type
:
DB_BigInt
,
Nullable
:
false
},
&
Column
{
Name
:
"created"
,
Type
:
DB_DateTime
,
Nullable
:
false
},
&
Column
{
Name
:
"updated"
,
Type
:
DB_DateTime
,
Nullable
:
false
},
))
//------- indexes ------------------
mg
.
AddMigration
(
"add unique index UIX_dashboard.account_id"
,
new
(
AddIndexMigration
)
.
Name
(
"UIX_dashboard_account_id"
)
.
Table
(
"dashboard"
)
.
Columns
(
"account_id"
))
mg
.
AddMigration
(
"add unique index UIX_dashboard_account_id_slug"
,
new
(
AddIndexMigration
)
.
Name
(
"UIX_dashboard_account_id_slug"
)
.
Table
(
"dashboard"
)
.
Columns
(
"account_id"
,
"slug"
))
}
pkg/services/sqlstore/migrations/migrations_test.go
View file @
2379c5b7
...
...
@@ -35,7 +35,7 @@ func TestMigrations(t *testing.T) {
log
.
NewLogger
(
0
,
"console"
,
`{"level": 0}`
)
testDBs
:=
[][]
string
{
//
[]string{"mysql", "grafana:password@tcp(localhost:3306)/grafana_tests?charset=utf8"},
[]
string
{
"mysql"
,
"grafana:password@tcp(localhost:3306)/grafana_tests?charset=utf8"
},
[]
string
{
"sqlite3"
,
":memory:"
},
}
...
...
@@ -50,6 +50,7 @@ func TestMigrations(t *testing.T) {
}
mg
:=
NewMigrator
(
x
)
mg
.
LogLevel
=
log
.
DEBUG
AddMigrations
(
mg
)
err
=
mg
.
Start
()
...
...
@@ -58,7 +59,6 @@ func TestMigrations(t *testing.T) {
tables
,
err
:=
x
.
DBMetas
()
So
(
err
,
ShouldBeNil
)
//So(len(tables), ShouldEqual, 2)
fmt
.
Printf
(
"
\n
DB Schema after migration: table count: %v
\n
"
,
len
(
tables
))
for
_
,
table
:=
range
tables
{
...
...
pkg/services/sqlstore/migrations/migrator.go
View file @
2379c5b7
...
...
@@ -18,6 +18,15 @@ type Migrator struct {
migrations
[]
Migration
}
type
MigrationLog
struct
{
Id
int64
MigrationId
string
Sql
string
Success
bool
Error
string
Timestamp
time
.
Time
}
func
NewMigrator
(
engine
*
xorm
.
Engine
)
*
Migrator
{
mg
:=
&
Migrator
{}
mg
.
x
=
engine
...
...
@@ -79,7 +88,9 @@ func (mg *Migrator) Start() error {
for
_
,
m
:=
range
mg
.
migrations
{
_
,
exists
:=
logMap
[
m
.
Id
()]
if
exists
{
log
.
Debug
(
"Migrator:: Skipping migration: %v, Already executed"
,
m
.
Id
())
if
mg
.
LogLevel
<=
log
.
DEBUG
{
log
.
Debug
(
"Migrator:: Skipping migration: %v, Already executed"
,
m
.
Id
())
}
continue
}
...
...
@@ -91,7 +102,9 @@ func (mg *Migrator) Start() error {
Timestamp
:
time
.
Now
(),
}
log
.
Debug
(
"Migrator: Executing SQL:
\n
%v
\n
"
,
sql
)
if
mg
.
LogLevel
<=
log
.
DEBUG
{
log
.
Debug
(
"Migrator: Executing SQL:
\n
%v
\n
"
,
sql
)
}
if
err
:=
mg
.
exec
(
m
);
err
!=
nil
{
record
.
Error
=
err
.
Error
()
...
...
@@ -107,7 +120,9 @@ func (mg *Migrator) Start() error {
}
func
(
mg
*
Migrator
)
exec
(
m
Migration
)
error
{
log
.
Info
(
"Migrator::exec migration id: %v"
,
m
.
Id
())
if
mg
.
LogLevel
<=
log
.
INFO
{
log
.
Info
(
"Migrator::exec migration id: %v"
,
m
.
Id
())
}
err
:=
mg
.
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
_
,
err
:=
sess
.
Exec
(
m
.
Sql
(
mg
.
dialect
))
...
...
pkg/services/sqlstore/sqlstore.go
View file @
2379c5b7
...
...
@@ -35,7 +35,7 @@ var (
func
init
()
{
tables
=
make
([]
interface
{},
0
)
tables
=
append
(
tables
,
new
(
m
.
Da
shboard
),
new
(
m
.
Da
taSource
),
new
(
DashboardTag
),
tables
=
append
(
tables
,
new
(
m
.
DataSource
),
new
(
DashboardTag
),
new
(
m
.
Token
))
}
...
...
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