Реализовать lab3 в общем Helm-релизе с Traefik, TLS и HPA
This commit is contained in:
@@ -22,6 +22,9 @@ import (
|
||||
//go:embed migrations/001_initial.sql
|
||||
var initialMigration string
|
||||
|
||||
//go:embed migrations/002_idempotency.sql
|
||||
var idempotencyMigration string
|
||||
|
||||
const migrationLockID = 202602
|
||||
|
||||
// Options bounds database resource use.
|
||||
@@ -79,14 +82,24 @@ func (s *Store) Start(ctx context.Context) error {
|
||||
if err := tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version=1)").Scan(&exists); err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
if _, err := tx.Exec(ctx, initialMigration); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, "INSERT INTO schema_migrations(version) VALUES(1)"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version=2)").Scan(&exists); err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(ctx, initialMigration); err != nil {
|
||||
if _, err := tx.Exec(ctx, idempotencyMigration); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := tx.Exec(ctx, "INSERT INTO schema_migrations(version) VALUES(1)")
|
||||
_, err := tx.Exec(ctx, "INSERT INTO schema_migrations(version) VALUES(2)")
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -125,3 +138,17 @@ func normalize(err error) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// CheckSchema verifies readiness without allowing API replicas to run DDL.
|
||||
func (s *Store) CheckSchema(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
var version int
|
||||
if err := s.pool.QueryRow(ctx, "SELECT COALESCE(max(version),0) FROM schema_migrations").Scan(&version); err != nil {
|
||||
return err
|
||||
}
|
||||
if version != 2 {
|
||||
return fmt.Errorf("database schema version %d; run server migrate", version)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user