Init creates the schema if needed.
(ctx context.Context)
| 44 | |
| 45 | // Init creates the schema if needed. |
| 46 | func (s Store) Init(ctx context.Context) error { |
| 47 | db, err := s.open() |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | defer db.Close() |
| 52 | _, err = db.ExecContext(ctx, schemaSQL) |
| 53 | if err != nil { |
| 54 | return fmt.Errorf("appstate: init schema: %w", err) |
| 55 | } |
| 56 | if err := migrate(ctx, db); err != nil { |
| 57 | return fmt.Errorf("appstate: migrate schema: %w", err) |
| 58 | } |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // migrate applies idempotent, additive schema changes to databases created by |
| 63 | // older versions. ADD COLUMN on an existing column returns a "duplicate column" |