Skip to content

Instantly share code, notes, and snippets.

@ayf
Created December 8, 2023 17:30
Show Gist options
  • Save ayf/6720cd25fec68a572183b3b6169d8442 to your computer and use it in GitHub Desktop.
Save ayf/6720cd25fec68a572183b3b6169d8442 to your computer and use it in GitHub Desktop.
Spanner Check
package main
import (
"context"
"log"
"prodvana/db"
"prodvana/db/modelsgen"
"strconv"
"cloud.google.com/go/spanner"
)
// Set during build
// find go/db/dbtestutil/migrations/*.sql -type f -printf '%f\n' | sort -n | tail -n1 | sed "s/^0*//" | sed "s/\.sql$//"
var (
minSchemaVersionStr string
)
func main() {
ctx := context.Background()
spannerClient, err := db.NewSpannerClient(ctx)
if err != nil {
log.Fatal(err)
}
minSchemaVersion, err := strconv.Atoi(minSchemaVersionStr)
if err != nil {
log.Fatal(err)
}
if minSchemaVersion == 0 {
log.Fatal("Min schema version not set - is the build script broken?")
}
log.Printf("Min schema version: %d", minSchemaVersion)
schemaMigrations, err := modelsgen.ReadSchemaMigration(ctx, spannerClient.Single(), spanner.AllKeys())
if err != nil {
log.Fatal(err)
}
if len(schemaMigrations) != 1 {
log.Fatalf("Expected 1 SchemaMigration record, found %d", len(schemaMigrations))
}
latestSchemaMigration := schemaMigrations[0]
log.Printf("Latest schema migration: %+v", latestSchemaMigration)
if latestSchemaMigration.Dirty {
log.Fatal("Schema is dirty, migration still in progress")
}
if latestSchemaMigration.Version < int64(minSchemaVersion) {
log.Fatalf("Schema version mismatch: expected at least %d, found %d", minSchemaVersion, latestSchemaMigration.Version)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment