Compare commits
No commits in common. "main" and "v1.0.2024" have entirely different histories.
|
|
@ -52,16 +52,14 @@ func (m *Migration) CreateTable(ctx context.Context, table string, columns map[s
|
|||
var primaryKeys []string
|
||||
for name, def := range columns {
|
||||
colDef := m.buildColumnDefinition(name, def)
|
||||
// Only add to primaryKeys if it's not an auto-increment column
|
||||
// (auto-increment columns already have PRIMARY KEY in their definition)
|
||||
if def.PrimaryKey && !def.AutoIncrement {
|
||||
if def.PrimaryKey {
|
||||
primaryKeys = append(primaryKeys, database.QuoteIdentifier(name))
|
||||
}
|
||||
colDefs = append(colDefs, " "+colDef)
|
||||
}
|
||||
|
||||
// Add composite primary key constraint if needed (for non-auto-increment keys)
|
||||
if len(primaryKeys) > 1 {
|
||||
// Add primary key constraint if needed
|
||||
if len(primaryKeys) > 0 {
|
||||
colDefs = append(colDefs, fmt.Sprintf(" PRIMARY KEY (%s)", strings.Join(primaryKeys, ", ")))
|
||||
}
|
||||
|
||||
|
|
@ -76,27 +74,30 @@ func (m *Migration) buildColumnDefinition(name string, def *database.ColumnDefin
|
|||
var parts []string
|
||||
parts = append(parts, database.QuoteIdentifier(name))
|
||||
|
||||
// Handle SQLite-specific types for auto-increment primary key
|
||||
// Handle SQLite-specific types
|
||||
dbType := def.Type
|
||||
if def.AutoIncrement && def.PrimaryKey {
|
||||
if dbType == "INT" || dbType == "INTEGER" {
|
||||
dbType = "INTEGER"
|
||||
}
|
||||
}
|
||||
parts = append(parts, dbType)
|
||||
|
||||
if def.PrimaryKey && def.AutoIncrement {
|
||||
// SQLite requires INTEGER type for AUTOINCREMENT
|
||||
parts = append(parts, "INTEGER PRIMARY KEY AUTOINCREMENT")
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
parts = append(parts, "PRIMARY KEY AUTOINCREMENT")
|
||||
} else {
|
||||
if !def.Null {
|
||||
parts = append(parts, "NOT NULL")
|
||||
}
|
||||
|
||||
// Regular column definition
|
||||
parts = append(parts, def.Type)
|
||||
if def.Unique && !def.PrimaryKey {
|
||||
parts = append(parts, "UNIQUE")
|
||||
}
|
||||
|
||||
if !def.Null {
|
||||
parts = append(parts, "NOT NULL")
|
||||
}
|
||||
|
||||
if def.Unique && !def.PrimaryKey {
|
||||
parts = append(parts, "UNIQUE")
|
||||
}
|
||||
|
||||
if def.Default != nil {
|
||||
defaultValue := formatDefaultValue(def.Default)
|
||||
parts = append(parts, fmt.Sprintf("DEFAULT %s", defaultValue))
|
||||
if def.Default != nil {
|
||||
defaultValue := formatDefaultValue(def.Default)
|
||||
parts = append(parts, fmt.Sprintf("DEFAULT %s", defaultValue))
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(parts, " ")
|
||||
|
|
|
|||
Loading…
Reference in New Issue