feat(store): add account from_address field + v2 migration
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+25
-5
@@ -42,15 +42,35 @@ func Open(path string) (*Store, error) {
|
||||
return nil, fmt.Errorf("apply schema: %w", err)
|
||||
}
|
||||
s := &Store{db: db}
|
||||
if _, err := s.GetSetting("schema_version"); err != nil {
|
||||
if err := s.SetSetting("schema_version", strconv.Itoa(schemaVersion)); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := s.migrate(); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// migrate brings an existing database up to the current schemaVersion. A brand-
|
||||
// new database (no schema_version yet) already has every column from schemaSQL,
|
||||
// so it is simply stamped at the current version. Each older version runs its
|
||||
// forward step. The version gate makes every step idempotent across reopens.
|
||||
func (s *Store) migrate() error {
|
||||
v, err := s.GetSetting("schema_version")
|
||||
if err != nil {
|
||||
// Fresh database: schemaSQL created all columns already.
|
||||
return s.SetSetting("schema_version", strconv.Itoa(schemaVersion))
|
||||
}
|
||||
ver, _ := strconv.Atoi(v)
|
||||
if ver < 2 {
|
||||
if _, err := s.db.Exec(`ALTER TABLE accounts ADD COLUMN from_address TEXT`); err != nil {
|
||||
return fmt.Errorf("migrate to v2: %w", err)
|
||||
}
|
||||
if err := s.SetSetting("schema_version", "2"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Close() error { return s.db.Close() }
|
||||
|
||||
// DefaultDBPath resolves EMCLI_DB or the per-OS default location.
|
||||
|
||||
Reference in New Issue
Block a user