Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cloud Spanner Schema Migration

Cloud Spanner Schema Migration

Yuki Ito

June 21, 2019
Tweet

More Decks by Yuki Ito

Other Decks in Technology

Transcript

  1. Schema Migration After Migration CREATE TABLE Users ( UserId STRING(36),

    Name STRING(1024), NickName STRING(1024) NOT NULL, ) PRIMARY KEY (UserId);
  2. 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.
  3. Schema Migration Wrench ɾSchema version management ./db/migrations !"" 000001.sql !""

    000002.sql !"" 000003.sql !"" 000004.sql #"" 000005.sql
  4. Schema Migration Wrench CREATE TABLE SchemaMigrations ( Version INT64 NOT

    NULL, Dirty BOOL NOT NULL, ) PRIMARY KEY(Version); ɾSchema version management
  5. # Create Tag Cloud Build Hook Container Registry Cloud Spanner

    Kubernetes Engine Watch Push Container Migration Deploy
  6. # Cloud Build Hook Container Registry Kubernetes Engine Watch Push

    Container Migration Deploy Create Tag Cloud Spanner
  7. Schema Migration Migration CREATE TABLE Users ( UserId STRING(36), Name

    STRING(1024), NickName STRING(1024) NOT NULL, ) PRIMARY KEY (UserId);
  8. 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.
  9. 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.
  10. 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…
  11. 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
  12. 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.
  13. 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.
  14. Schema Migration UserId Name NickName “aaaaaaaa-…” “Alice (NULL) “bbbbbbbb-…” “Bob”

    (NULL) “cccccccc-…” “Carol” (NULL) “dddddddd-…” “Dany” “Ddd” … … … Users New
  15. Schema Migration UserId Name NickName “aaaaaaaa-…” “Alice (NULL) “bbbbbbbb-…” “Bob”

    (NULL) “cccccccc-…” “Carol” (NULL) “dddddddd-…” “Dany” “Ddd” … … … Users New
  16. 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.
  17. 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.
  18. Schema Migration Wrench $ wrench migrate create ./db/migrations !"" 000001.sql

    # Add column !"" 000002.sql # Update existing rows #"" 000003.sql # Add NULL constraint
  19. Schema Migration UserId Name NickName “aaaaaaaa-…” “Alice “” “bbbbbbbb-…” “Bob”

    “” “cccccccc-…” “Carol” “” “dddddddd-…” “Dany” “Ddd” … … … Users New
  20. 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.
  21. 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.
  22. Schema Migration Wrench $ wrench migrate create ./db/migrations !"" 000001.sql

    # Add column !"" 000002.sql # Update existing rows #"" 000003.sql # Add NULL constraint
  23. Schema Migration Complete CREATE TABLE Users ( UserId STRING(36), Name

    STRING(1024), NickName STRING(1024) NOT NULL, ) PRIMARY KEY (UserId);
  24. 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
  25. # Cloud Build Hook Container Registry Kubernetes Engine Watch Push

    Container Migration Deploy Create Tag Cloud Spanner