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
12f3c8f9
Unverified
Commit
12f3c8f9
authored
Dec 18, 2018
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix only add column if not exists for mysql
parent
a451c201
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
36 additions
and
10 deletions
+36
-10
pkg/services/sqlstore/migrator/conditions.go
+10
-0
pkg/services/sqlstore/migrator/dialect.go
+5
-0
pkg/services/sqlstore/migrator/migrations.go
+3
-1
pkg/services/sqlstore/migrator/migrator.go
+12
-9
pkg/services/sqlstore/migrator/mysql_dialect.go
+6
-0
No files found.
pkg/services/sqlstore/migrator/conditions.go
View file @
12f3c8f9
...
@@ -36,3 +36,13 @@ type IfIndexNotExistsCondition struct {
...
@@ -36,3 +36,13 @@ type IfIndexNotExistsCondition struct {
func
(
c
*
IfIndexNotExistsCondition
)
Sql
(
dialect
Dialect
)
(
string
,
[]
interface
{})
{
func
(
c
*
IfIndexNotExistsCondition
)
Sql
(
dialect
Dialect
)
(
string
,
[]
interface
{})
{
return
dialect
.
IndexCheckSql
(
c
.
TableName
,
c
.
IndexName
)
return
dialect
.
IndexCheckSql
(
c
.
TableName
,
c
.
IndexName
)
}
}
type
IfColumnNotExistsCondition
struct
{
NotExistsMigrationCondition
TableName
string
ColumnName
string
}
func
(
c
*
IfColumnNotExistsCondition
)
Sql
(
dialect
Dialect
)
(
string
,
[]
interface
{})
{
return
dialect
.
ColumnCheckSql
(
c
.
TableName
,
c
.
ColumnName
)
}
pkg/services/sqlstore/migrator/dialect.go
View file @
12f3c8f9
...
@@ -33,6 +33,7 @@ type Dialect interface {
...
@@ -33,6 +33,7 @@ type Dialect interface {
UpdateTableSql
(
tableName
string
,
columns
[]
*
Column
)
string
UpdateTableSql
(
tableName
string
,
columns
[]
*
Column
)
string
IndexCheckSql
(
tableName
,
indexName
string
)
(
string
,
[]
interface
{})
IndexCheckSql
(
tableName
,
indexName
string
)
(
string
,
[]
interface
{})
ColumnCheckSql
(
tableName
,
columnName
string
)
(
string
,
[]
interface
{})
ColString
(
*
Column
)
string
ColString
(
*
Column
)
string
ColStringNoPk
(
*
Column
)
string
ColStringNoPk
(
*
Column
)
string
...
@@ -183,6 +184,10 @@ func (db *BaseDialect) RenameTable(oldName string, newName string) string {
...
@@ -183,6 +184,10 @@ func (db *BaseDialect) RenameTable(oldName string, newName string) string {
return
fmt
.
Sprintf
(
"ALTER TABLE %s RENAME TO %s"
,
quote
(
oldName
),
quote
(
newName
))
return
fmt
.
Sprintf
(
"ALTER TABLE %s RENAME TO %s"
,
quote
(
oldName
),
quote
(
newName
))
}
}
func
(
db
*
BaseDialect
)
ColumnCheckSql
(
tableName
,
columnName
string
)
(
string
,
[]
interface
{})
{
return
""
,
nil
}
func
(
db
*
BaseDialect
)
DropIndexSql
(
tableName
string
,
index
*
Index
)
string
{
func
(
db
*
BaseDialect
)
DropIndexSql
(
tableName
string
,
index
*
Index
)
string
{
quote
:=
db
.
dialect
.
Quote
quote
:=
db
.
dialect
.
Quote
name
:=
index
.
XName
(
tableName
)
name
:=
index
.
XName
(
tableName
)
...
...
pkg/services/sqlstore/migrator/migrations.go
View file @
12f3c8f9
...
@@ -85,7 +85,9 @@ type AddColumnMigration struct {
...
@@ -85,7 +85,9 @@ type AddColumnMigration struct {
}
}
func
NewAddColumnMigration
(
table
Table
,
col
*
Column
)
*
AddColumnMigration
{
func
NewAddColumnMigration
(
table
Table
,
col
*
Column
)
*
AddColumnMigration
{
return
&
AddColumnMigration
{
tableName
:
table
.
Name
,
column
:
col
}
m
:=
&
AddColumnMigration
{
tableName
:
table
.
Name
,
column
:
col
}
m
.
Condition
=
&
IfColumnNotExistsCondition
{
TableName
:
table
.
Name
,
ColumnName
:
col
.
Name
}
return
m
}
}
func
(
m
*
AddColumnMigration
)
Table
(
tableName
string
)
*
AddColumnMigration
{
func
(
m
*
AddColumnMigration
)
Table
(
tableName
string
)
*
AddColumnMigration
{
...
...
pkg/services/sqlstore/migrator/migrator.go
View file @
12f3c8f9
...
@@ -121,16 +121,19 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
...
@@ -121,16 +121,19 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
condition
:=
m
.
GetCondition
()
condition
:=
m
.
GetCondition
()
if
condition
!=
nil
{
if
condition
!=
nil
{
sql
,
args
:=
condition
.
Sql
(
mg
.
Dialect
)
sql
,
args
:=
condition
.
Sql
(
mg
.
Dialect
)
mg
.
Logger
.
Debug
(
"Executing migration condition sql"
,
"id"
,
m
.
Id
(),
"sql"
,
sql
,
"args"
,
args
)
results
,
err
:=
sess
.
SQL
(
sql
,
args
...
)
.
Query
()
if
err
!=
nil
{
mg
.
Logger
.
Error
(
"Executing migration condition failed"
,
"id"
,
m
.
Id
(),
"error"
,
err
)
return
err
}
if
!
condition
.
IsFulfilled
(
results
)
{
if
sql
!=
""
{
mg
.
Logger
.
Warn
(
"Skipping migration: Already executed, but not recorded in migration log"
,
"id"
,
m
.
Id
())
mg
.
Logger
.
Debug
(
"Executing migration condition sql"
,
"id"
,
m
.
Id
(),
"sql"
,
sql
,
"args"
,
args
)
return
nil
results
,
err
:=
sess
.
SQL
(
sql
,
args
...
)
.
Query
()
if
err
!=
nil
{
mg
.
Logger
.
Error
(
"Executing migration condition failed"
,
"id"
,
m
.
Id
(),
"error"
,
err
)
return
err
}
if
!
condition
.
IsFulfilled
(
results
)
{
mg
.
Logger
.
Warn
(
"Skipping migration: Already executed, but not recorded in migration log"
,
"id"
,
m
.
Id
())
return
nil
}
}
}
}
}
...
...
pkg/services/sqlstore/migrator/mysql_dialect.go
View file @
12f3c8f9
...
@@ -108,6 +108,12 @@ func (db *Mysql) IndexCheckSql(tableName, indexName string) (string, []interface
...
@@ -108,6 +108,12 @@ func (db *Mysql) IndexCheckSql(tableName, indexName string) (string, []interface
return
sql
,
args
return
sql
,
args
}
}
func
(
db
*
Mysql
)
ColumnCheckSql
(
tableName
,
columnName
string
)
(
string
,
[]
interface
{})
{
args
:=
[]
interface
{}{
tableName
,
columnName
}
sql
:=
"SELECT 1 FROM "
+
db
.
Quote
(
"INFORMATION_SCHEMA"
)
+
"."
+
db
.
Quote
(
"COLUMNS"
)
+
" WHERE "
+
db
.
Quote
(
"TABLE_SCHEMA"
)
+
" = DATABASE() AND "
+
db
.
Quote
(
"TABLE_NAME"
)
+
"=? AND "
+
db
.
Quote
(
"COLUMN_NAME"
)
+
"=?"
return
sql
,
args
}
func
(
db
*
Mysql
)
CleanDB
()
error
{
func
(
db
*
Mysql
)
CleanDB
()
error
{
tables
,
_
:=
db
.
engine
.
DBMetas
()
tables
,
_
:=
db
.
engine
.
DBMetas
()
sess
:=
db
.
engine
.
NewSession
()
sess
:=
db
.
engine
.
NewSession
()
...
...
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