feat(database): 添加 SQLite 数据库目录自动创建功能
- 为 SQLite 数据库添加数据库文件目录自动创建逻辑 - 实现 ensureSQLiteDirectory 方法解析数据库路径并创建必要目录 - 支持从 config.Name 或 config.Link 中提取 SQLite 文件路径 - 添加对 SQLite 链接格式 sqlite::@file(path) 的路径解析支持 - 在数据库表迁移前确保 SQLite 数据库文件目录存在 - 新增 sqlitecgo 驱动包实现 SQLite 数据库驱动支持main v1.0.2024
parent
284f9380ed
commit
33faa6f722
|
|
@ -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