Progress on database schema migration for account -> org refactor
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package migrator
|
||||
|
||||
type MigrationCondition interface {
|
||||
Sql(dialect Dialect) (string, []interface{})
|
||||
}
|
||||
|
||||
type IfTableExistsCondition struct {
|
||||
TableName string
|
||||
}
|
||||
|
||||
func (c *IfTableExistsCondition) Sql(dialect Dialect) (string, []interface{}) {
|
||||
return dialect.TableCheckSql(c.TableName)
|
||||
}
|
||||
@@ -20,8 +20,12 @@ type Dialect interface {
|
||||
CreateIndexSql(tableName string, index *Index) string
|
||||
CreateTableSql(table *Table) string
|
||||
AddColumnSql(tableName string, Col *Column) string
|
||||
CopyTableData(sourceTable string, targetTable string, sourceCols string, targetCols string) string
|
||||
DropTable(tableName string) string
|
||||
DropIndexSql(tableName string, index *Index) string
|
||||
|
||||
TableCheckSql(tableName string) (string, []interface{})
|
||||
RenameTable(oldName string, newName string) string
|
||||
}
|
||||
|
||||
func NewDialect(name string) Dialect {
|
||||
@@ -113,3 +117,25 @@ func (db *BaseDialect) CreateIndexSql(tableName string, index *Index) string {
|
||||
quote(idxName), quote(tableName),
|
||||
quote(strings.Join(index.Cols, quote(","))))
|
||||
}
|
||||
|
||||
func (db *BaseDialect) CopyTableData(sourceTable string, targetTable string, sourceCols string, targetCols string) string {
|
||||
quote := db.dialect.Quote
|
||||
return fmt.Sprintf("INSERT INTO %s (%s) SELECT %s FROM %s", quote(targetTable), targetCols, sourceCols, quote(sourceTable))
|
||||
}
|
||||
|
||||
func (db *BaseDialect) DropTable(tableName string) string {
|
||||
quote := db.dialect.Quote
|
||||
return fmt.Sprintf("DROP TABLE IF EXISTS %s", quote(tableName))
|
||||
}
|
||||
|
||||
func (db *BaseDialect) RenameTable(oldName string, newName string) string {
|
||||
quote := db.dialect.Quote
|
||||
return fmt.Sprintf("ALTER TABLE %s RENAME TO %s", quote(oldName), quote(newName))
|
||||
}
|
||||
|
||||
func (db *BaseDialect) DropIndexSql(tableName string, index *Index) string {
|
||||
quote := db.dialect.Quote
|
||||
var name string
|
||||
name = index.XName(tableName)
|
||||
return fmt.Sprintf("DROP INDEX %v ON %s", quote(name), quote(tableName))
|
||||
}
|
||||
|
||||
+90
-6
@@ -6,7 +6,8 @@ import (
|
||||
)
|
||||
|
||||
type MigrationBase struct {
|
||||
id string
|
||||
id string
|
||||
Condition MigrationCondition
|
||||
}
|
||||
|
||||
func (m *MigrationBase) Id() string {
|
||||
@@ -17,6 +18,10 @@ func (m *MigrationBase) SetId(id string) {
|
||||
m.id = id
|
||||
}
|
||||
|
||||
func (m *MigrationBase) GetCondition() MigrationCondition {
|
||||
return m.Condition
|
||||
}
|
||||
|
||||
type RawSqlMigration struct {
|
||||
MigrationBase
|
||||
|
||||
@@ -98,6 +103,39 @@ func (m *AddIndexMigration) Sql(dialect Dialect) string {
|
||||
return dialect.CreateIndexSql(m.tableName, &m.index)
|
||||
}
|
||||
|
||||
type DropIndexMigration struct {
|
||||
MigrationBase
|
||||
tableName string
|
||||
index Index
|
||||
}
|
||||
|
||||
func (m *DropIndexMigration) Name(name string) *DropIndexMigration {
|
||||
m.index.Name = name
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *DropIndexMigration) Table(tableName string) *DropIndexMigration {
|
||||
m.tableName = tableName
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *DropIndexMigration) Unique() *DropIndexMigration {
|
||||
m.index.Type = UniqueIndex
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *DropIndexMigration) Columns(columns ...string) *DropIndexMigration {
|
||||
m.index.Cols = columns
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *DropIndexMigration) Sql(dialect Dialect) string {
|
||||
if m.index.Name == "" {
|
||||
m.index.Name = fmt.Sprintf("%s", strings.Join(m.index.Cols, "_"))
|
||||
}
|
||||
return dialect.DropIndexSql(m.tableName, &m.index)
|
||||
}
|
||||
|
||||
type AddTableMigration struct {
|
||||
MigrationBase
|
||||
table Table
|
||||
@@ -130,20 +168,66 @@ func (m *AddTableMigration) WithColumn(col *Column) *AddTableMigration {
|
||||
return m
|
||||
}
|
||||
|
||||
type RenameColumnMigration struct {
|
||||
type DropTableMigration struct {
|
||||
MigrationBase
|
||||
tableName string
|
||||
oldName string
|
||||
newName string
|
||||
}
|
||||
|
||||
func (m *RenameColumnMigration) Table(tableName string) *RenameColumnMigration {
|
||||
func (m *DropTableMigration) Table(tableName string) *DropTableMigration {
|
||||
m.tableName = tableName
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *RenameColumnMigration) Rename(oldName string, newName string) *RenameColumnMigration {
|
||||
func (m *DropTableMigration) Sql(d Dialect) string {
|
||||
return d.DropTable(m.tableName)
|
||||
}
|
||||
|
||||
type RenameTableMigration struct {
|
||||
MigrationBase
|
||||
oldName string
|
||||
newName string
|
||||
}
|
||||
|
||||
func (m *RenameTableMigration) IfTableExists(tableName string) *RenameTableMigration {
|
||||
m.Condition = &IfTableExistsCondition{TableName: tableName}
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *RenameTableMigration) Rename(oldName string, newName string) *RenameTableMigration {
|
||||
m.oldName = oldName
|
||||
m.newName = newName
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *RenameTableMigration) Sql(d Dialect) string {
|
||||
return d.RenameTable(m.oldName, m.newName)
|
||||
}
|
||||
|
||||
type CopyTableDataMigration struct {
|
||||
MigrationBase
|
||||
sourceTable string
|
||||
targetTable string
|
||||
sourceCols string
|
||||
targetCols string
|
||||
}
|
||||
|
||||
func (m *CopyTableDataMigration) Source(tableName string, cols string) *CopyTableDataMigration {
|
||||
m.sourceTable = tableName
|
||||
m.sourceCols = cols
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *CopyTableDataMigration) Target(tableName string, cols string) *CopyTableDataMigration {
|
||||
m.targetTable = tableName
|
||||
m.targetCols = cols
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *CopyTableDataMigration) IfTableExists(tableName string) *CopyTableDataMigration {
|
||||
m.Condition = &IfTableExistsCondition{TableName: tableName}
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *CopyTableDataMigration) Sql(d Dialect) string {
|
||||
return d.CopyTableData(m.sourceTable, m.targetTable, m.sourceCols, m.targetCols)
|
||||
}
|
||||
@@ -118,6 +118,17 @@ func (mg *Migrator) exec(m Migration) error {
|
||||
}
|
||||
|
||||
err := mg.inTransaction(func(sess *xorm.Session) error {
|
||||
|
||||
condition := m.GetCondition()
|
||||
if condition != nil {
|
||||
sql, args := condition.Sql(mg.dialect)
|
||||
results, err := sess.Query(sql, args...)
|
||||
if err != nil || len(results) == 0 {
|
||||
log.Info("Migrator: skipping migration id: %v, condition not fulfilled", m.Id())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
_, err := sess.Exec(m.Sql(mg.dialect))
|
||||
if err != nil {
|
||||
log.Error(3, "Migrator: exec FAILED migration id: %v, err: %v", m.Id(), err)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package migrator
|
||||
|
||||
import "strconv"
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Postgres struct {
|
||||
BaseDialect
|
||||
@@ -84,3 +87,9 @@ func (db *Postgres) TableCheckSql(tableName string) (string, []interface{}) {
|
||||
sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
|
||||
return sql, args
|
||||
}
|
||||
|
||||
func (db *Postgres) DropIndexSql(tableName string, index *Index) string {
|
||||
quote := db.Quote
|
||||
idxName := index.XName(tableName)
|
||||
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package migrator
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Sqlite3 struct {
|
||||
BaseDialect
|
||||
}
|
||||
@@ -57,3 +59,10 @@ func (db *Sqlite3) TableCheckSql(tableName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName}
|
||||
return "SELECT name FROM sqlite_master WHERE type='table' and name = ?", args
|
||||
}
|
||||
|
||||
func (db *Sqlite3) DropIndexSql(tableName string, index *Index) string {
|
||||
quote := db.Quote
|
||||
//var unique string
|
||||
idxName := index.XName(tableName)
|
||||
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package migrator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
POSTGRES = "postgres"
|
||||
SQLITE = "sqlite3"
|
||||
@@ -10,6 +15,7 @@ type Migration interface {
|
||||
Sql(dialect Dialect) string
|
||||
Id() string
|
||||
SetId(string)
|
||||
GetCondition() MigrationCondition
|
||||
}
|
||||
|
||||
type SQLType string
|
||||
@@ -37,6 +43,17 @@ type Index struct {
|
||||
Cols []string
|
||||
}
|
||||
|
||||
func (index *Index) XName(tableName string) string {
|
||||
if !strings.HasPrefix(index.Name, "UQE_") &&
|
||||
!strings.HasPrefix(index.Name, "IDX_") {
|
||||
if index.Type == UniqueIndex {
|
||||
return fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
|
||||
}
|
||||
return fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
|
||||
}
|
||||
return index.Name
|
||||
}
|
||||
|
||||
var (
|
||||
DB_Bit = "BIT"
|
||||
DB_TinyInt = "TINYINT"
|
||||
|
||||
Reference in New Issue
Block a user