spanner-manager
Unified CLI for Cloud Spanner schema migration, schema diffing, and Go ORM code generation.
Replaces three separate tools (wrench, hammer, yo) with a single binary, shared connection management, and a common SQL parser powered by memefish.
Features
- Database lifecycle -- create, drop, reset, truncate, and export DDL
- Schema diffing -- AST-based diff between any two sources (files or live databases), producing ready-to-apply ALTER statements
- Versioned migrations -- sequential, version-tracked DDL/DML migration files with dirty-state protection
- Code generation -- type-safe Go structs with CRUD mutations and primary-key lookups from a live database or DDL file
- Instance management -- create and delete Spanner instances
- Emulator support -- first-class
SPANNER_EMULATOR_HOST integration for local development
Installation
go install github.com/zchee/spanner-manager@latest
Quick Start
1. Authenticate for real Cloud Spanner
For production or staging projects, use Application Default Credentials or a service account key:
# Option 1: local development with gcloud
gcloud auth application-default login
# Option 2: explicit service account key
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
If you are using the Spanner emulator, skip authentication and set SPANNER_EMULATOR_HOST instead.
2. Export common connection settings
You can pass flags on every command, but exporting environment variables is less repetitive:
export SPANNER_PROJECT_ID=my-project
export SPANNER_INSTANCE_ID=my-instance
export SPANNER_DATABASE_ID=my-database
Then run commands without repeating -p, -i, and -d each time:
spanner-manager db load
spanner-manager migrate version
spanner-manager generate -o ./models
3. Discover commands and flags
spanner-manager --help
spanner-manager schema --help
spanner-manager schema apply --help
Configuration
All connection parameters can be set via flags or environment variables. Flags take precedence.
| Flag |
Short |
Env Var |
Description |
--project |
-p |
SPANNER_PROJECT_ID, GOOGLE_CLOUD_PROJECT |
GCP project ID |
--instance |
-i |
SPANNER_INSTANCE_ID |
Spanner instance ID |
--database |
-d |
SPANNER_DATABASE_ID |
Spanner database ID |
--credentials |
|
GOOGLE_APPLICATION_CREDENTIALS |
Path to service account JSON key |
--emulator-host |
|
SPANNER_EMULATOR_HOST |
Emulator address (e.g. localhost:9010) |
--timeout |
|
|
Operation timeout (default: 1h) |
When --emulator-host is set, the tool automatically uses insecure gRPC credentials and skips authentication.
Connection Requirements by Command
Not every command needs a live Spanner connection. The common cases are:
| Command |
Project |
Instance |
Database |
Notes |
instance create, instance delete |
required |
required |
not used |
Creates or deletes an instance |
db create, db drop, db reset, db load, db truncate |
required |
required |
required |
db create and db reset can also read a local --schema file |
migrate up, migrate version, migrate set |
required |
required |
required |
Reads migration files from migrations/ under --directory |
schema apply |
required |
required |
required |
Target database comes from global flags |
schema export FILE, schema diff FILE1 FILE2 |
not needed |
not needed |
not needed |
Works entirely on local DDL files |
schema export spanner://..., schema diff spanner://... FILE |
required inside URI |
required inside URI |
required inside URI |
Global --credentials and --emulator-host still apply |
generate --from-ddl schema.sql -o ./models |
not needed |
not needed |
not needed |
Generates code without contacting Spanner |
generate -o ./models |
required |
required |
required |
Reads schema from INFORMATION_SCHEMA |
migrate create NAME |
not needed |
not needed |
not needed |
Creates the next file under migrations/ |
Usage
End-to-End Example on Managed Cloud Spanner
This is the fastest way to understand the intended workflow:
# 1. Create an instance once
spanner-manager instance create --config regional-us-central1 --nodes 1
# 2. Create the database with an initial schema
spanner-manager db create --schema schema.sql
# 3. Export the current DDL for review or version control
spanner-manager db load --output current.sql
# 4. Compare the current schema with a desired schema file
spanner-manager schema diff current.sql desired.sql
# 5. Apply the desired schema to the live database
spanner-manager schema apply desired.sql
# 6. Create and apply versioned migrations for incremental changes
spanner-manager migrate create add_users_table
spanner-manager migrate up
# 7. Generate Go models from the live database
spanner-manager generate -o ./models
If you prefer to inspect the live database directly during diffing, use a spanner:// URI:
spanner-manager schema diff \
spanner://projects/my-project/instances/my-instance/databases/my-database \
desired.sql
db -- Database Management
# Create a database
spanner-manager db create -p my-project -i my-instance -d my-db
# Create with initial schema
spanner-manager db create -p my-project -i my-instance -d my-db --schema schema.sql
# Drop a database (irreversible)
spanner-manager db drop -p my-project -i my-instance -d my-db --force
# Drop and recreate (useful for dev reset)
spanner-manager db reset -p my-project -i my-instance -d my-db --schema schema.sql --force
# Truncate all tables (preserves schema and SchemaMigrations table)
# Respects interleave order: child tables are deleted before parents
spanner-manager db truncate -p my-project -i my-instance -d my-db --force
# Export current DDL to stdout
spanner-manager db load -p my-project -i my-instance -d my-db
# Export current DDL to file
spanner-manager db load -p my-project -i my-instance -d my-db --output schema.sql
Use db create or db reset when you already have a canonical DDL file. Use db load when you want to snapshot the live schema before a diff or commit it back to version control.
schema -- Schema Diff and Apply
Schema sources can be a local DDL file path or a spanner:// URI:
spanner://projects/{PROJECT}/instances/{INSTANCE}/databases/{DATABASE}
# Export DDL from a source
spanner-manager schema export schema.sql
spanner-manager schema export spanner://projects/my-project/instances/my-instance/databases/my-db
# Diff two schemas -- outputs ALTER DDL to migrate from SOURCE1 to SOURCE2
spanner-manager schema diff old.sql new.sql
spanner-manager schema diff spanner://projects/P/instances/I/databases/D desired.sql
# Apply: diff current database against desired DDL, then execute the changes
spanner-manager schema apply desired.sql -p my-project -i my-instance -d my-db
Recommended review flow before applying changes:
# Inspect the exact ALTER statements first
spanner-manager schema diff \
spanner://projects/my-project/instances/my-instance/databases/my-db \
desired.sql
# Apply only after the diff looks correct
spanner-manager schema apply desired.sql -p my-project -i my-instance -d my-db
Cloud Spanner DDL support matrix
This section is the maintenance contract for schema diff coverage. It is
intentionally conservative: "parsed" means the pinned memefish version can build
an AST node, while "diffed" means diff.ParseDatabase records enough semantic
state for diff.Diff to generate migration DDL.
Current parser reference:
go.mod pins github.com/cloudspannerecosystem/memefish
v0.6.3-0.20260429070454-64f857b2c61e.
sqlutil.ParseDDLs delegates to memefish.ParseDDLs.
diff.ParseDatabase currently records only the statement families handled
in its switch. Unknown ast.DDL nodes are a correctness gap until production
fail-loud handling rejects them explicitly.
Status legend
| Status |
Meaning |
| Supported |
Parsed into diff.Database and diff generation exists. |
| Partial |
Parsed or stored, but some alter/drop/equality paths are incomplete. |
| Parse-only gap |
memefish parses the DDL, but diff.ParseDatabase does not model it. |
| Unsupported |
Not intentionally supported by this diff engine. |
| Live validation gap |
Local AST output exists, but Cloud Spanner acceptance still needs disposable database validation. |
Support matrix
| DDL family |
memefish parse |
ParseDatabase model |
Diff output |
Notes and guardrails |
Schemas: CREATE SCHEMA, DROP SCHEMA |
Yes |
Supported |
Partial |
Creates are emitted before tables and drops after removed objects. Schema option changes are not modeled. |
Database options: ALTER DATABASE ... SET OPTIONS |
Yes |
Partial |
Partial |
Database options are stored and diffed, but only as whole option-set changes. Validate option-specific semantics before broadening. |
Tables: CREATE TABLE, DROP TABLE |
Yes |
Supported |
Supported |
Drop/create is destructive. Default policy must remain conservative: do not use table recreation as a fallback for uncertain changes. |
| Table columns: add/drop/default/generated/options/type/nullability |
Yes |
Supported |
Partial |
Add/drop and selected alter paths exist. Data-preserving ALTER is preferred; unknown conversions must fail loudly or require an explicit destructive mode. |
| Primary keys and interleave clauses |
Yes |
Supported |
Partial |
Primary-key changes and parent interleave changes are high risk. Do not silently recreate tables for these without explicit destructive-policy documentation. SET ON DELETE and SET INTERLEAVE need focused validation. |
| Table constraints: named FK/check constraints |
Yes |
Supported |
Partial |
Constraint add/drop exists. Named constraints should be tracked by table plus constraint name, not raw SQL formatting, to avoid skipped recreates. |
| Row deletion policies |
Yes |
Supported |
Supported |
Create, replace, and drop row deletion policy diffs are modeled. Keep behavior covered because it changes data retention. |
| Secondary indexes |
Yes |
Supported |
Supported |
Create/drop and storing-column ALTER paths exist. Changed index definitions may require drop/recreate; this is destructive to index state but not table data. |
| Search indexes |
Yes |
Supported |
Partial |
Create/drop is modeled. Changed definitions generally drop and recreate the search index. Live validation is needed for newer search-index options. |
| Vector indexes |
Yes |
Supported |
Partial |
Create/drop is modeled. Changed definitions generally drop and recreate the vector index. Live validation is needed for vector option semantics. |
| Change streams |
Yes |
Supported |
Partial |
Create/drop and selected ALTER paths are modeled. Table-scoped stream rewrites must preserve grants and avoid invalid ordering. |
| Sequences |
Yes |
Supported |
Partial |
Create/drop exists. Changed sequence options must use ALTER SEQUENCE where safe; drop/recreate loses sequence state and must not be the default fallback. |
| Views |
Yes |
Supported |
Partial |
Create/drop and replace exist. Unchanged views should produce no statement; changed views should use CREATE OR REPLACE VIEW only when definitions differ semantically. |
| Roles |
Yes |
Supported |
Supported |
Create/drop is modeled. Role drops must account for grants. |
| Grants and revokes |
Yes |
Partial |
Partial |
Grants are modeled; missing grants emit REVOKE. REVOKE input statements themselves are not modeled as desired-state objects. |