Skip to content

Commit b09740d

Browse files
authored
adding new spanner-migrations runner module under community folder. (#5597)
adding new module spanner-migrations runner under community folder.
1 parent 091f69d commit b09740d

6 files changed

Lines changed: 236 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## Description
2+
3+
This module runs Spanner DDL migrations from a specified directory using `gcloud`.
4+
It looks for `.up.sql` files in the specified `migrations_dir` (or an optional `sub_directory` within it) and applies them in alphabetical order.
5+
6+
### Software Requirements
7+
8+
* `gcloud` CLI
9+
10+
### Example
11+
12+
```yaml
13+
- id: run_migrations
14+
source: community/modules/scripts/spanner-migrations-runner
15+
settings:
16+
project_id: $(vars.project_id)
17+
instance_name: my-spanner-instance
18+
database_name: my-database
19+
migrations_dir: $(ghpc_stage("my-assets/migrations"))
20+
sub_directory: db1
21+
```
22+
23+
## License
24+
25+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
26+
Copyright 2026 Google LLC
27+
28+
Licensed under the Apache License, Version 2.0 (the "License");
29+
you may not use this file except in compliance with the License.
30+
You may obtain a copy of the License at
31+
32+
http://www.apache.org/licenses/LICENSE-2.0
33+
34+
Unless required by applicable law or agreed to in writing, software
35+
distributed under the License is distributed on an "AS IS" BASIS,
36+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37+
See the License for the specific language governing permissions and
38+
limitations under the License.
39+
40+
## Requirements
41+
42+
| Name | Version |
43+
| ---- | ------- |
44+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | = 1.12.2 |
45+
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.0 |
46+
47+
## Providers
48+
49+
| Name | Version |
50+
| ---- | ------- |
51+
| <a name="provider_null"></a> [null](#provider\_null) | >= 3.0 |
52+
53+
## Modules
54+
55+
No modules.
56+
57+
## Resources
58+
59+
| Name | Type |
60+
| ---- | ---- |
61+
| [null_resource.run_migrations](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
62+
63+
## Inputs
64+
65+
| Name | Description | Type | Default | Required |
66+
| ---- | ----------- | ---- | ------- | :------: |
67+
| <a name="input_database_name"></a> [database\_name](#input\_database\_name) | The Spanner database name. | `string` | n/a | yes |
68+
| <a name="input_instance_name"></a> [instance\_name](#input\_instance\_name) | The Spanner instance name. | `string` | n/a | yes |
69+
| <a name="input_migrations_dir"></a> [migrations\_dir](#input\_migrations\_dir) | The migrations directory. | `string` | n/a | yes |
70+
| <a name="input_project_id"></a> [project\_id](#input\_project\_id) | The project ID to deploy to. | `string` | n/a | yes |
71+
| <a name="input_proto_descriptors_file"></a> [proto\_descriptors\_file](#input\_proto\_descriptors\_file) | Optional path to a compiled proto descriptors file (.pb) needed for custom types in migrations. | `string` | `null` | no |
72+
| <a name="input_sub_directory"></a> [sub\_directory](#input\_sub\_directory) | Optional sub-directory within migrations\_dir to search for SQL files. | `string` | `""` | no |
73+
74+
## Outputs
75+
76+
| Name | Description |
77+
| ---- | ----------- |
78+
| <a name="output_migration_completion_id"></a> [migration\_completion\_id](#output\_migration\_completion\_id) | The ID of the migration completion anchor. |
79+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
locals {
18+
target_dir = var.sub_directory != "" ? "${var.migrations_dir}/${var.sub_directory}" : var.migrations_dir
19+
}
20+
21+
resource "null_resource" "run_migrations" {
22+
triggers = {
23+
migrations_hash = sha256(join("", [for f in fileset(local.target_dir, "*.up.sql") : filesha256("${local.target_dir}/${f}")]))
24+
25+
proto_hash = var.proto_descriptors_file != null ? filesha256(var.proto_descriptors_file) : ""
26+
instance_name = var.instance_name
27+
database_name = var.database_name
28+
project_id = var.project_id
29+
}
30+
31+
provisioner "local-exec" {
32+
command = <<EOF
33+
set -e
34+
for f in "${local.target_dir}"/*.up.sql; do
35+
[ -e "$f" ] || continue
36+
echo "Applying $f..."
37+
gcloud spanner databases ddl update "${var.database_name}" \
38+
--instance="${var.instance_name}" \
39+
--project="${var.project_id}" \
40+
--ddl-file="$f" \
41+
${var.proto_descriptors_file != null ? "--proto-descriptors-file=\"${var.proto_descriptors_file}\"" : ""} || exit 1
42+
done
43+
EOF
44+
}
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
---
15+
16+
spec:
17+
requirements:
18+
services:
19+
- spanner.googleapis.com
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "migration_completion_id" {
18+
description = "The ID of the migration completion anchor."
19+
value = null_resource.run_migrations.id
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "project_id" {
18+
description = "The project ID to deploy to."
19+
type = string
20+
}
21+
22+
variable "instance_name" {
23+
description = "The Spanner instance name."
24+
type = string
25+
}
26+
27+
variable "database_name" {
28+
description = "The Spanner database name."
29+
type = string
30+
}
31+
32+
variable "migrations_dir" {
33+
description = "The migrations directory."
34+
type = string
35+
}
36+
37+
variable "sub_directory" {
38+
description = "Optional sub-directory within migrations_dir to search for SQL files."
39+
type = string
40+
default = ""
41+
}
42+
43+
variable "proto_descriptors_file" {
44+
description = "Optional path to a compiled proto descriptors file (.pb) needed for custom types in migrations."
45+
type = string
46+
default = null
47+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
terraform {
18+
required_providers {
19+
null = {
20+
source = "hashicorp/null"
21+
version = ">= 3.0"
22+
}
23+
}
24+
25+
required_version = "= 1.12.2"
26+
}

0 commit comments

Comments
 (0)