More work on SQL migrations

This commit is contained in:
Torkel Ödegaard
2015-02-24 17:59:21 +01:00
parent 02a89c752b
commit ed68a4bb9a
9 changed files with 422 additions and 314 deletions
+17 -7
View File
@@ -20,7 +20,7 @@ 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
CopyTableData(sourceTable string, targetTable string, sourceCols []string, targetCols []string) string
DropTable(tableName string) string
DropIndexSql(tableName string, index *Index) string
@@ -105,22 +105,32 @@ func (db *BaseDialect) AddColumnSql(tableName string, col *Column) string {
func (db *BaseDialect) CreateIndexSql(tableName string, index *Index) string {
quote := db.dialect.Quote
var unique string
var idxName string
if index.Type == UniqueIndex {
unique = " UNIQUE"
idxName = fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
} else {
idxName = fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
}
idxName := index.XName(tableName)
return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v);", unique,
quote(idxName), quote(tableName),
quote(strings.Join(index.Cols, quote(","))))
}
func (db *BaseDialect) CopyTableData(sourceTable string, targetTable string, sourceCols string, targetCols string) string {
func (db *BaseDialect) QuoteColList(cols []string) string {
var sourceColsSql = ""
for _, col := range cols {
sourceColsSql += db.dialect.Quote(col)
sourceColsSql += "\n, "
}
return strings.TrimSuffix(sourceColsSql, "\n, ")
}
func (db *BaseDialect) CopyTableData(sourceTable string, targetTable string, sourceCols []string, targetCols []string) string {
sourceColsSql := db.QuoteColList(sourceCols)
targetColsSql := db.QuoteColList(targetCols)
quote := db.dialect.Quote
return fmt.Sprintf("INSERT INTO %s (%s) SELECT %s FROM %s", quote(targetTable), targetCols, sourceCols, quote(sourceTable))
return fmt.Sprintf("INSERT INTO %s (%s) SELECT %s FROM %s", quote(targetTable), targetColsSql, sourceColsSql, quote(sourceTable))
}
func (db *BaseDialect) DropTable(tableName string) string {
+34 -27
View File
@@ -73,12 +73,11 @@ func (m *AddColumnMigration) Sql(dialect Dialect) string {
type AddIndexMigration struct {
MigrationBase
tableName string
index Index
index *Index
}
func (m *AddIndexMigration) Name(name string) *AddIndexMigration {
m.index.Name = name
return m
func NewAddIndexMigration(table Table, index *Index) *AddIndexMigration {
return &AddIndexMigration{tableName: table.Name, index: index}
}
func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration {
@@ -92,26 +91,23 @@ func (m *AddIndexMigration) Unique() *AddIndexMigration {
}
func (m *AddIndexMigration) Columns(columns ...string) *AddIndexMigration {
m.index = &Index{}
m.index.Cols = columns
return m
}
func (m *AddIndexMigration) Sql(dialect Dialect) string {
if m.index.Name == "" {
m.index.Name = fmt.Sprintf("%s", strings.Join(m.index.Cols, "_"))
}
return dialect.CreateIndexSql(m.tableName, &m.index)
return dialect.CreateIndexSql(m.tableName, m.index)
}
type DropIndexMigration struct {
MigrationBase
tableName string
index Index
index *Index
}
func (m *DropIndexMigration) Name(name string) *DropIndexMigration {
m.index.Name = name
return m
func NewDropIndexMigration(table Table, index *Index) *DropIndexMigration {
return &DropIndexMigration{tableName: table.Name, index: index}
}
func (m *DropIndexMigration) Table(tableName string) *DropIndexMigration {
@@ -125,6 +121,7 @@ func (m *DropIndexMigration) Unique() *DropIndexMigration {
}
func (m *DropIndexMigration) Columns(columns ...string) *DropIndexMigration {
m.index = &Index{}
m.index.Cols = columns
return m
}
@@ -133,7 +130,7 @@ 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)
return dialect.DropIndexSql(m.tableName, m.index)
}
type AddTableMigration struct {
@@ -141,10 +138,19 @@ type AddTableMigration struct {
table Table
}
func NewAddTableMigration(table Table) *AddTableMigration {
return &AddTableMigration{table: table}
}
func (m *AddTableMigration) Sql(d Dialect) string {
return d.CreateTableSql(&m.table)
}
func (m *AddTableMigration) Table(table Table) *AddTableMigration {
m.table = table
return m
}
func (m *AddTableMigration) Name(name string) *AddTableMigration {
m.table.Name = name
return m
@@ -173,9 +179,8 @@ type DropTableMigration struct {
tableName string
}
func (m *DropTableMigration) Table(tableName string) *DropTableMigration {
m.tableName = tableName
return m
func NewDropTableMigration(tableName string) *DropTableMigration {
return &DropTableMigration{tableName: tableName}
}
func (m *DropTableMigration) Sql(d Dialect) string {
@@ -188,6 +193,10 @@ type RenameTableMigration struct {
newName string
}
func NewRenameTableMigration(oldName string, newName string) *RenameTableMigration {
return &RenameTableMigration{oldName: oldName, newName: newName}
}
func (m *RenameTableMigration) IfTableExists(tableName string) *RenameTableMigration {
m.Condition = &IfTableExistsCondition{TableName: tableName}
return m
@@ -207,19 +216,17 @@ type CopyTableDataMigration struct {
MigrationBase
sourceTable string
targetTable string
sourceCols string
targetCols string
sourceCols []string
targetCols []string
colMap map[string]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
func NewCopyTableDataMigration(targetTable string, sourceTable string, colMap map[string]string) *CopyTableDataMigration {
m := &CopyTableDataMigration{sourceTable: sourceTable, targetTable: targetTable}
for key, value := range colMap {
m.targetCols = append(m.targetCols, key)
m.sourceCols = append(m.sourceCols, value)
}
return m
}
+5
View File
@@ -30,6 +30,7 @@ type Table struct {
Name string
Columns []*Column
PrimaryKeys []string
Indices []*Index
}
const (
@@ -44,6 +45,10 @@ type Index struct {
}
func (index *Index) XName(tableName string) string {
if index.Name == "" {
index.Name = fmt.Sprintf("%s", strings.Join(index.Cols, "_"))
}
if !strings.HasPrefix(index.Name, "UQE_") &&
!strings.HasPrefix(index.Name, "IDX_") {
if index.Type == UniqueIndex {