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 {