EnsureSchema creates the required tables (and schema when provided).
(ctx context.Context)
| 109 | |
| 110 | // EnsureSchema creates the required tables (and schema when provided). |
| 111 | func (s *PostgresStore) EnsureSchema(ctx context.Context) error { |
| 112 | if s == nil || s.db == nil { |
| 113 | return fmt.Errorf("postgres store: not initialized") |
| 114 | } |
| 115 | if schema := strings.TrimSpace(s.cfg.Schema); schema != "" { |
| 116 | query := fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %s", quoteIdentifier(schema)) |
| 117 | if _, err := s.db.ExecContext(ctx, query); err != nil { |
| 118 | return fmt.Errorf("postgres store: create schema: %w", err) |
| 119 | } |
| 120 | } |
| 121 | configTable := s.fullTableName(s.cfg.ConfigTable) |
| 122 | if _, err := s.db.ExecContext(ctx, fmt.Sprintf(` |
| 123 | CREATE TABLE IF NOT EXISTS %s ( |
| 124 | id TEXT PRIMARY KEY, |
| 125 | content TEXT NOT NULL, |
| 126 | created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 127 | updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() |
| 128 | ) |
| 129 | `, configTable)); err != nil { |
| 130 | return fmt.Errorf("postgres store: create config table: %w", err) |
| 131 | } |
| 132 | authTable := s.fullTableName(s.cfg.AuthTable) |
| 133 | if _, err := s.db.ExecContext(ctx, fmt.Sprintf(` |
| 134 | CREATE TABLE IF NOT EXISTS %s ( |
| 135 | id TEXT PRIMARY KEY, |
| 136 | content JSONB NOT NULL, |
| 137 | created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 138 | updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() |
| 139 | ) |
| 140 | `, authTable)); err != nil { |
| 141 | return fmt.Errorf("postgres store: create auth table: %w", err) |
| 142 | } |
| 143 | return nil |
| 144 | } |
| 145 | |
| 146 | // Bootstrap synchronizes configuration and auth records between PostgreSQL and the local workspace. |
| 147 | func (s *PostgresStore) Bootstrap(ctx context.Context, exampleConfigPath string) error { |
no test coverage detected