Compare commits

..

No commits in common. "main" and "v1.0.2023" have entirely different histories.

3 changed files with 24 additions and 76 deletions

View File

@ -52,16 +52,14 @@ func (m *Migration) CreateTable(ctx context.Context, table string, columns map[s
var primaryKeys []string var primaryKeys []string
for name, def := range columns { for name, def := range columns {
colDef := m.buildColumnDefinition(name, def) colDef := m.buildColumnDefinition(name, def)
// Only add to primaryKeys if it's not an auto-increment column if def.PrimaryKey {
// (auto-increment columns already have PRIMARY KEY in their definition)
if def.PrimaryKey && !def.AutoIncrement {
primaryKeys = append(primaryKeys, database.QuoteIdentifier(name)) primaryKeys = append(primaryKeys, database.QuoteIdentifier(name))
} }
colDefs = append(colDefs, " "+colDef) colDefs = append(colDefs, " "+colDef)
} }
// Add composite primary key constraint if needed (for non-auto-increment keys) // Add primary key constraint if needed
if len(primaryKeys) > 1 { if len(primaryKeys) > 0 {
colDefs = append(colDefs, fmt.Sprintf(" PRIMARY KEY (%s)", strings.Join(primaryKeys, ", "))) 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 var parts []string
parts = append(parts, database.QuoteIdentifier(name)) 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 { if def.PrimaryKey && def.AutoIncrement {
// SQLite requires INTEGER type for AUTOINCREMENT parts = append(parts, "PRIMARY KEY AUTOINCREMENT")
parts = append(parts, "INTEGER PRIMARY KEY AUTOINCREMENT") } else {
return strings.Join(parts, " ") if !def.Null {
} parts = append(parts, "NOT NULL")
}
// Regular column definition if def.Unique && !def.PrimaryKey {
parts = append(parts, def.Type) parts = append(parts, "UNIQUE")
}
if !def.Null { if def.Default != nil {
parts = append(parts, "NOT NULL") defaultValue := formatDefaultValue(def.Default)
} parts = append(parts, fmt.Sprintf("DEFAULT %s", defaultValue))
}
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))
} }
return strings.Join(parts, " ") return strings.Join(parts, " ")

View File

@ -50,9 +50,3 @@ func (d *Driver) New(core *database.Core, node *database.ConfigNode) (database.D
func (d *Driver) GetChars() (charLeft string, charRight string) { func (d *Driver) GetChars() (charLeft string, charRight string) {
return quoteChar, quoteChar 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
}

View File

@ -9,8 +9,6 @@ package database
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"path/filepath"
"reflect" "reflect"
"strings" "strings"
"time" "time"
@ -48,13 +46,6 @@ func (am *AutoMigrateCore) migrateEntity(ctx context.Context, entity any) error
return fmt.Errorf("no columns found for table %s", tableName) 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 // Check if table exists
hasTable, err := am.db.HasTable(ctx, tableName) hasTable, err := am.db.HasTable(ctx, tableName)
if err != nil { if err != nil {
@ -459,41 +450,3 @@ func IsZeroValue(v any) bool {
} }
return false 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
}