Share
Embed
Cloud Spanner Schema Migration
Yuki Ito
June 21, 2019
Transcript
61 slides
-
Schema Migration Example Table CREATE TABLE Users ( UserId STRING(36),
Name STRING(1024), ) PRIMARY KEY (UserId); -
Schema Migration After Migration CREATE TABLE Users ( UserId STRING(36),
Name STRING(1024), NickName STRING(1024) NOT NULL, ) PRIMARY KEY (UserId); -
Schema Migration ALTER TABLE Users ADD COLUMN NickName STRING(1024) NOT
NULL; That’s all …? -
Schema Migration Cloud Spanner doesn’t support implicit DEFAULT value We
can’t use `NOT NULL` with `ADD COLUMN` -
Schema Migration Strategy 1. ADD COLUMN without NOT NULL 3.
Fill the new columns of existing rows with non NULL value 4. ALTER COLUMN with NOT NULL 2. Deploy application which doesn’t insert new rows with NULL value anymore. -
Schema Migration ɾWrench (Migration tool made by us) ɾSpinnaker (OSS
CD tool) Using 2 Tools -
Schema Migration Wrench ɾSchema version management ./db/migrations !"" 000001.sql !""
000002.sql !"" 000003.sql #"" 000004.sql -
Schema Migration Wrench ɾSchema version management $ wrench migrate create
db/migrations/000005.sql is created -
Schema Migration Wrench ɾSchema version management ./db/migrations !"" 000001.sql !""
000002.sql !"" 000003.sql !"" 000004.sql #"" 000005.sql -
Schema Migration Wrench CREATE TABLE SchemaMigrations ( Version INT64 NOT
NULL, Dirty BOOL NOT NULL, ) PRIMARY KEY(Version); ɾSchema version management -
Schema Migration Spinnaker ɾApply Kubernetes manifests automatically – Deployment –
Service – Job ɾHook pushing container to registry -
# Create Tag Cloud Build Hook Container Registry Cloud Spanner
Kubernetes Engine Watch Push Container Migration Deploy -
# Cloud Build Hook Container Registry Kubernetes Engine Watch Push
Container Migration Deploy Create Tag Cloud Spanner -
Schema Migration Migration CREATE TABLE Users ( UserId STRING(36), Name
STRING(1024), NickName STRING(1024) NOT NULL, ) PRIMARY KEY (UserId); -
Schema Migration Strategy 1. ADD COLUMN without NOT NULL 3.
Fill the new columns of existing rows with non NULL value 4. ALTER COLUMN with NOT NULL 2. Deploy application which doesn’t insert new rows with NULL value anymore. -
Schema Migration Strategy 1. ADD COLUMN without NOT NULL 3.
Fill the new columns of existing rows with non NULL value 4. ALTER COLUMN with NOT NULL 2. Deploy application which doesn’t insert new rows with NULL value anymore. -
Schema Migration Wrench ./db/migrations #"" 000001.sql # Add column $
wrench migrate create -
Kubernetes Engine Cloud Spanner ᶃ Fetch `Job` Manifest ᶅ Exec
`wrench migrate` ᶄ Run Migration Job -
Schema Migration apiVersion: batch/v1 kind: Job metadata: name: awesome-job namespace:
awesome-namespace spec: activeDeadlineSeconds: 3600 backoffLimit: 1 completions: 1 parallelism: 1 template: spec: containers: - command: - /db/migrate.sh env: - name: SPANNER_INSTANCE_ID value: xxx - name: SPANNER_DATABASE_ID value: xxx - name: GCP_PROJECT_ID value: xxx - name: GOOGLE_APPLICATION_CREDENTIALS value: xxx image: gcr.io/xxxx… -
Schema Migration #!/bin/sh set -euxo pipefail /wrench \ --project "${GCP_PROJECT_ID}"
\ --instance "${PARTNER_SERVICE_SPANNER_INSTANCE_ID}" \ --database "${PARTNER_SERVICE_SPANNER_DATABASE_ID}" \ --directory /db \ migrate up -
Schema Migration UserId Name NickName “aaaaaaaa-…” “Alice” (NULL) “bbbbbbbb-…” “Bob”
(NULL) “cccccccc-…” “Carol” (NULL) … … … Users New -
Schema Migration Strategy 1. ADD COLUMN without NOT NULL 3.
Fill the new columns of existing rows with non NULL value 4. ALTER COLUMN with NOT NULL 2. Deploy application which doesn’t insert new rows with NULL value anymore. -
Schema Migration Strategy 1. ADD COLUMN without NOT NULL 3.
Fill the new columns of existing rows with non NULL value 4. ALTER COLUMN with NOT NULL 2. Deploy application which doesn’t insert new rows with NULL value anymore. -
Kubernetes Engine ᶃ Fetch `Deployment` Manifest ᶄ Apply Deployment Manifest
(Deploy New Container) -
Schema Migration UserId Name NickName “aaaaaaaa-…” “Alice (NULL) “bbbbbbbb-…” “Bob”
(NULL) “cccccccc-…” “Carol” (NULL) “dddddddd-…” “Dany” “Ddd” … … … Users New -
Schema Migration UserId Name NickName “aaaaaaaa-…” “Alice (NULL) “bbbbbbbb-…” “Bob”
(NULL) “cccccccc-…” “Carol” (NULL) “dddddddd-…” “Dany” “Ddd” … … … Users New -
Schema Migration Strategy 1. ADD COLUMN without NOT NULL 3.
Fill the new columns of existing rows with non NULL value 4. ALTER COLUMN with NOT NULL 2. Deploy application which doesn’t insert new rows with NULL value anymore. -
Schema Migration Strategy 1. ADD COLUMN without NOT NULL 3.
Fill the new columns of existing rows with non NULL value 4. ALTER COLUMN with NOT NULL 2. Deploy application which doesn’t insert new rows with NULL value anymore. -
Schema Migration Wrench $ wrench migrate create ./db/migrations !"" 000001.sql
# Add column !"" 000002.sql # Update existing rows #"" 000003.sql # Add NULL constraint -
Schema Migration Wrench UPDATE Users SET NickName = “” WHERE
NickName IS NULL; -
Schema Migration Wrench UPDATE Users SET NickName = “” WHERE
NickName IS NULL; as Partitioned-DML -
Schema Migration Partitioned-DML https://cloud.google.com/spanner/docs/dml-partitioned#partitionable-idempotent “enables large-scale, database-wide operations with minimal
impact on concurrent transaction processing -
Schema Migration UserId Name NickName “aaaaaaaa-…” “Alice “” “bbbbbbbb-…” “Bob”
“” “cccccccc-…” “Carol” “” “dddddddd-…” “Dany” “Ddd” … … … Users New -
Schema Migration Strategy 1. ADD COLUMN without NOT NULL 3.
Fill the new columns of existing rows with non NULL value 4. ALTER COLUMN with NOT NULL 2. Deploy application which doesn’t insert new rows with NULL value anymore. -
Schema Migration Strategy 1. ADD COLUMN without NOT NULL 3.
Fill the new columns of existing rows with non NULL value 4. ALTER COLUMN with NOT NULL 2. Deploy application which doesn’t insert new rows with NULL value anymore. -
Schema Migration Wrench $ wrench migrate create ./db/migrations !"" 000001.sql
# Add column !"" 000002.sql # Update existing rows #"" 000003.sql # Add NULL constraint -
Schema Migration Complete CREATE TABLE Users ( UserId STRING(36), Name
STRING(1024), NickName STRING(1024) NOT NULL, ) PRIMARY KEY (UserId); -
Schema Migration 3 Phase QA 1. ADD COLUMN without NOT
NULL 3. Fill the new columns of existing rows with non NULL value 4. ALTER COLUMN with NOT NULL 2. Deploy application which doesn’t insert new rows with NULL value anymore. QA 1 QA 2 QA 3 -
# Cloud Build Hook Container Registry Kubernetes Engine Watch Push
Container Migration Deploy Create Tag Cloud Spanner -
Kubernetes Engine Cloud Spanner ᶃ Fetch `Job` Manifest ᶅ Exec
`wrench migrate` ᶄ Run Migration Job