Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema: add support for diff policy #105

Merged
merged 13 commits into from
Nov 26, 2023
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
- '1.3.*'
- '1.4.*'
- '1.5.*'
- '1.6.*'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
Expand Down Expand Up @@ -124,6 +125,31 @@ jobs:
name: Integration (Terraform ${{ matrix.terraform-version }})
runs-on: ubuntu-latest
needs: [build-dev]
services:
mysql8prod:
image: mysql:8
env:
MYSQL_ROOT_PASSWORD: pass
ports:
- 3306:3306
options: >-
--health-cmd "mysqladmin ping -ppass"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
mysql8dev:
image: mysql:8
env:
MYSQL_ROOT_PASSWORD: pass
ports:
- 3307:3306
options: >-
--health-cmd "mysqladmin ping -ppass"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
strategy:
fail-fast: false
matrix:
Expand All @@ -132,6 +158,7 @@ jobs:
- '1.3.*'
- '1.4.*'
- '1.5.*'
- '1.6.*'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
Expand Down Expand Up @@ -174,3 +201,23 @@ jobs:
! cat stdout.txt | grep --silent "Warning: version is unset"
env:
TF_VAR_atlas_token: ${{ secrets.ATLAS_TOKEN }}
- name: Terraform (skip-policy)
working-directory: integration-tests/skip-policy
run: |
../../scripts/local.sh ../../dist 0.0.0-pre.0
terraform init > /dev/null
terraform apply --auto-approve
terraform plan -no-color -var schema="schema-t2" > stdout.txt
cat stdout.txt | grep --silent "Plan: 0 to add, 1 to change, 0 to destroy."
cat stdout.txt | grep --silent "DROP TABLE \`test\`.\`t2\`"

terraform plan -no-color -var schema="schema-t2" \
-var "skip_drop_table=true" > stdout.txt
cat stdout.txt | grep --silent "Plan: 0 to add, 1 to change, 0 to destroy."
# Expect no drop table statement in the plan
! cat stdout.txt | grep --silent "DROP TABLE \`test\`.\`t2\`"
terraform apply --auto-approve -no-color -var="schema=schema-t2" -var="skip_drop_table=true"
# Confirm that the table was not dropped
../../scripts/atlas-cli.sh 0.0.0-pre.0 schema inspect \
-u "mysql://root:pass@localhost:3306/" > actual.hcl
cmp -s "expected.hcl" "actual.hcl"
29 changes: 29 additions & 0 deletions docs/resources/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,37 @@ resource "atlas_schema" "market" {

- `dev_db_url` (String, Sensitive, Deprecated) Use `dev_url` instead.
- `dev_url` (String, Sensitive) The url of the dev-db see https://atlasgo.io/cli/url
- `diff` (Block, Optional) (see [below for nested schema](#nestedblock--diff))
- `exclude` (List of String) Filter out resources matching the given glob pattern. See https://atlasgo.io/declarative/inspect#exclude-schemas

### Read-Only

- `id` (String) The ID of this resource

<a id="nestedblock--diff"></a>
### Nested Schema for `diff`

Optional:

- `skip` (Block, Optional) The skip changes policy (see [below for nested schema](#nestedblock--diff--skip))

<a id="nestedblock--diff--skip"></a>
### Nested Schema for `diff.skip`

Optional:

- `add_column` (Boolean) Whether to skip adding columns
- `add_foreign_key` (Boolean) Whether to skip adding foreign keys
- `add_index` (Boolean) Whether to skip adding indexes
- `add_schema` (Boolean) Whether to skip adding schemas
- `add_table` (Boolean) Whether to skip adding tables
- `drop_column` (Boolean) Whether to skip dropping columns
- `drop_foreign_key` (Boolean) Whether to skip dropping foreign keys
- `drop_index` (Boolean) Whether to skip dropping indexes
- `drop_schema` (Boolean) Whether to skip dropping schemas
- `drop_table` (Boolean) Whether to skip dropping tables
- `modify_column` (Boolean) Whether to skip modifying columns
- `modify_foreign_key` (Boolean) Whether to skip modifying foreign keys
- `modify_index` (Boolean) Whether to skip modifying indexes
- `modify_schema` (Boolean) Whether to skip modifying schemas
- `modify_table` (Boolean) Whether to skip modifying tables
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ go 1.21

require (
ariga.io/atlas v0.15.1-0.20231104142243-962427888bf2
ariga.io/atlas-go-sdk v0.1.1-0.20231102105853-9ab4fb4ddd54
ariga.io/atlas-go-sdk v0.2.2-0.20231126120643-dda5b31d605a
github.com/go-sql-driver/mysql v1.7.1
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-framework v1.4.2
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.19.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v1.14.17
github.com/mitchellh/go-homedir v1.1.0
github.com/stretchr/testify v1.8.4
golang.org/x/mod v0.12.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ariga.io/atlas v0.15.1-0.20231104142243-962427888bf2 h1:uw5P4SCzPO3dACfbtt6TuwrWY72B4EKrNck+hKPM6dg=
ariga.io/atlas v0.15.1-0.20231104142243-962427888bf2/go.mod h1:isZrlzJ5cpoCoKFoY9knZug7Lq4pP1cm8g3XciLZ0Pw=
ariga.io/atlas-go-sdk v0.1.1-0.20231102105853-9ab4fb4ddd54 h1:/LzEDWzKDJH2e8vht7+kl2VO5hmkSC5SCyKAZtkLhiI=
ariga.io/atlas-go-sdk v0.1.1-0.20231102105853-9ab4fb4ddd54/go.mod h1:owkEEXw6jqne5KPVDfKsYB7cwMiMk3jtOiAAeKxS/yU=
ariga.io/atlas-go-sdk v0.2.2-0.20231126120643-dda5b31d605a h1:q/FyO3DkUEQNEWjKiNRYjRYD5gkbPO1/V+fWHit/UlM=
ariga.io/atlas-go-sdk v0.2.2-0.20231126120643-dda5b31d605a/go.mod h1:owkEEXw6jqne5KPVDfKsYB7cwMiMk3jtOiAAeKxS/yU=
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
Expand Down Expand Up @@ -143,6 +143,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
**/.terraform/*
**/terraform.d
**/*.db
**/stdout.txt
**/stdout.txt
**/actual.hcl
32 changes: 32 additions & 0 deletions integration-tests/skip-policy/expected.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
table "t1" {
schema = schema.test
column "c1" {
null = false
type = int
}
column "c2" {
null = true
type = text
}
primary_key {
columns = [column.c1]
}
}
table "t2" {
schema = schema.test
column "c1" {
null = false
type = int
}
column "c2" {
null = true
type = text
}
primary_key {
columns = [column.c1]
}
}
schema "test" {
charset = "utf8mb4"
collate = "utf8mb4_0900_ai_ci"
}
34 changes: 34 additions & 0 deletions integration-tests/skip-policy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
terraform {
required_providers {
atlas = {
source = "ariga/atlas"
version = "0.0.0-pre.0"
}
}
}

variable "schema" {
type = string
default = "schema"
}

variable "skip_drop_table" {
type = bool
default = false
}

data "atlas_schema" "db" {
src = "file://${var.schema}.hcl"
dev_url = "mysql://root:pass@localhost:3307/"
}

resource "atlas_schema" "db" {
hcl = data.atlas_schema.db.hcl
url = "mysql://root:pass@localhost:3306/"
dev_url = "mysql://root:pass@localhost:3307/"
diff {
skip {
drop_table = var.skip_drop_table
}
}
}
15 changes: 15 additions & 0 deletions integration-tests/skip-policy/schema-t2.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
table "t1" {
schema = schema.test
column "c1" {
type = int
}
column "c2" {
null = true
type = text
}
primary_key {
columns = [column.c1]
}
}
schema "test" {
}
28 changes: 28 additions & 0 deletions integration-tests/skip-policy/schema.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
table "t1" {
schema = schema.test
column "c1" {
type = int
}
column "c2" {
null = true
type = text
}
primary_key {
columns = [column.c1]
}
}
table "t2" {
schema = schema.test
column "c1" {
type = int
}
column "c2" {
null = true
type = text
}
primary_key {
columns = [column.c1]
}
}
schema "test" {
}
Loading