NewMySQLStore 创建 MySQL 存储
(config *MySQLStoreConfig)
| 33 | |
| 34 | // NewMySQLStore 创建 MySQL 存储 |
| 35 | func NewMySQLStore(config *MySQLStoreConfig) (*MySQLStore, error) { |
| 36 | if config == nil { |
| 37 | return nil, errors.New("config is required") |
| 38 | } |
| 39 | |
| 40 | if config.DB == nil { |
| 41 | return nil, errors.New("database connection is required") |
| 42 | } |
| 43 | |
| 44 | tableName := config.TableName |
| 45 | if tableName == "" { |
| 46 | tableName = "logic_memories" |
| 47 | } |
| 48 | |
| 49 | store := &MySQLStore{ |
| 50 | db: config.DB, |
| 51 | tableName: tableName, |
| 52 | } |
| 53 | |
| 54 | // 自动创建表 |
| 55 | if config.AutoMigrate { |
| 56 | if err := store.migrate(); err != nil { |
| 57 | return nil, fmt.Errorf("failed to migrate: %w", err) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return store, nil |
| 62 | } |
| 63 | |
| 64 | // migrate 创建表结构 |
| 65 | func (s *MySQLStore) migrate() error { |