Compare commits
2 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
7e54a96454 | |
|
|
33faa6f722 |
|
|
@ -52,14 +52,16 @@ 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)
|
||||
if def.PrimaryKey {
|
||||
// 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 {
|
||||
primaryKeys = append(primaryKeys, database.QuoteIdentifier(name))
|
||||
}
|
||||
colDefs = append(colDefs, " "+colDef)
|
||||
}
|
||||
|
||||
// Add primary key constraint if needed
|
||||
if len(primaryKeys) > 0 {
|
||||
// Add composite primary key constraint if needed (for non-auto-increment keys)
|
||||
if len(primaryKeys) > 1 {
|
||||
colDefs = append(colDefs, fmt.Sprintf(" PRIMARY KEY (%s)", strings.Join(primaryKeys, ", ")))
|
||||
}
|
||||
|
||||
|
|
@ -74,18 +76,16 @@ func (m *Migration) buildColumnDefinition(name string, def *database.ColumnDefin
|
|||
var parts []string
|
||||
parts = append(parts, database.QuoteIdentifier(name))
|
||||
|
||||
// Handle SQLite-specific types
|
||||
dbType := def.Type
|
||||
if def.AutoIncrement && def.PrimaryKey {
|
||||
if dbType == "INT" || dbType == "INTEGER" {
|
||||
dbType = "INTEGER"
|
||||
}
|
||||
}
|
||||
parts = append(parts, dbType)
|
||||
|
||||
// Handle SQLite-specific types for auto-increment primary key
|
||||
if def.PrimaryKey && def.AutoIncrement {
|
||||
parts = append(parts, "PRIMARY KEY AUTOINCREMENT")
|
||||
} else {
|
||||
// SQLite requires INTEGER type for AUTOINCREMENT
|
||||
parts = append(parts, "INTEGER PRIMARY KEY AUTOINCREMENT")
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
|
||||
// Regular column definition
|
||||
parts = append(parts, def.Type)
|
||||
|
||||
if !def.Null {
|
||||
parts = append(parts, "NOT NULL")
|
||||
}
|
||||
|
|
@ -98,7 +98,6 @@ func (m *Migration) buildColumnDefinition(name string, def *database.ColumnDefin
|
|||
defaultValue := formatDefaultValue(def.Default)
|
||||
parts = append(parts, fmt.Sprintf("DEFAULT %s", defaultValue))
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,3 +50,9 @@ func (d *Driver) New(core *database.Core, node *database.ConfigNode) (database.D
|
|||
func (d *Driver) GetChars() (charLeft string, charRight string) {
|
||||
return quoteChar, quoteChar
|
||||
}
|
||||
|
||||
// GetMigration returns a Migration instance implementing the Migration interface.
|
||||
// Note: sqlitecgo driver does not have migration support yet, returns nil.
|
||||
func (d *Driver) GetMigration() database.Migration {
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ package database
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -46,6 +48,13 @@ func (am *AutoMigrateCore) migrateEntity(ctx context.Context, entity any) error
|
|||
return fmt.Errorf("no columns found for table %s", tableName)
|
||||
}
|
||||
|
||||
// For SQLite, ensure the database file directory exists
|
||||
if am.db.GetConfig().Type == "sqlite" {
|
||||
if err := am.ensureSQLiteDirectory(); err != nil {
|
||||
return fmt.Errorf("failed to prepare SQLite database: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Check if table exists
|
||||
hasTable, err := am.db.HasTable(ctx, tableName)
|
||||
if err != nil {
|
||||
|
|
@ -450,3 +459,41 @@ func IsZeroValue(v any) bool {
|
|||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ensureSQLiteDirectory ensures the SQLite database file directory exists.
|
||||
func (am *AutoMigrateCore) ensureSQLiteDirectory() error {
|
||||
config := am.db.GetConfig()
|
||||
if config.Type != "sqlite" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get the database file path from config.Name or config.Link
|
||||
dbPath := config.Name
|
||||
if dbPath == "" && config.Link != "" {
|
||||
// Parse link format: sqlite::@file(./data/company.db)
|
||||
if strings.Contains(config.Link, "@file(") {
|
||||
start := strings.Index(config.Link, "@file(") + 6
|
||||
end := strings.Index(config.Link[start:], ")")
|
||||
if end > 0 {
|
||||
dbPath = config.Link[start : start+end]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if dbPath == "" {
|
||||
return fmt.Errorf("SQLite database path is empty")
|
||||
}
|
||||
|
||||
// Get directory path
|
||||
dir := filepath.Dir(dbPath)
|
||||
|
||||
// Check if directory exists
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
// Create directory with all parents
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create directory %s: %w", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue