Compare commits
No commits in common. "main" and "v1.0.2023" have entirely different histories.
|
|
@ -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,16 +74,18 @@ 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
|
||||||
if def.PrimaryKey && def.AutoIncrement {
|
dbType := def.Type
|
||||||
// SQLite requires INTEGER type for AUTOINCREMENT
|
if def.AutoIncrement && def.PrimaryKey {
|
||||||
parts = append(parts, "INTEGER PRIMARY KEY AUTOINCREMENT")
|
if dbType == "INT" || dbType == "INTEGER" {
|
||||||
return strings.Join(parts, " ")
|
dbType = "INTEGER"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
parts = append(parts, dbType)
|
||||||
|
|
||||||
// Regular column definition
|
if def.PrimaryKey && def.AutoIncrement {
|
||||||
parts = append(parts, def.Type)
|
parts = append(parts, "PRIMARY KEY AUTOINCREMENT")
|
||||||
|
} else {
|
||||||
if !def.Null {
|
if !def.Null {
|
||||||
parts = append(parts, "NOT NULL")
|
parts = append(parts, "NOT NULL")
|
||||||
}
|
}
|
||||||
|
|
@ -98,6 +98,7 @@ func (m *Migration) buildColumnDefinition(name string, def *database.ColumnDefin
|
||||||
defaultValue := formatDefaultValue(def.Default)
|
defaultValue := formatDefaultValue(def.Default)
|
||||||
parts = append(parts, fmt.Sprintf("DEFAULT %s", defaultValue))
|
parts = append(parts, fmt.Sprintf("DEFAULT %s", defaultValue))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return strings.Join(parts, " ")
|
return strings.Join(parts, " ")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue