Skip to content

Commit

Permalink
schema: fixed absolute path for SQLite (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Nov 28, 2023
1 parent 09d4b37 commit 0694cf2
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 41 deletions.
91 changes: 55 additions & 36 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ jobs:
fail-fast: false
matrix:
terraform-version:
- '1.2.*'
- '1.3.*'
- '1.4.*'
- '1.5.*'
- '1.6.*'
steps:
Expand Down Expand Up @@ -122,41 +119,13 @@ jobs:
retention-days: 5
if-no-files-found: error
integration:
name: Integration (Terraform ${{ matrix.terraform-version }})
name: Integration SQLite (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:
terraform-version:
- '1.2.*'
- '1.3.*'
- '1.4.*'
- '1.5.*'
- '1.6.*'
steps:
Expand All @@ -177,7 +146,12 @@ jobs:
run: |
../../scripts/local.sh ../../dist 0.0.0-pre.0
terraform init
terraform apply --auto-approve
echo "Apply terraform plan"
terraform apply -no-color --auto-approve > stdout.txt
cat stdout.txt | grep --silent "Apply complete! Resources: 1 added, 0 changed, 0 destroyed."
echo "Ensure that there is no diff"
terraform plan -no-color > stdout.txt
cat stdout.txt | grep --silent "No changes. Your infrastructure matches the configuration."
- name: Terraform (no-dev-url)
working-directory: integration-tests/no-dev-url
run: |
Expand All @@ -201,6 +175,54 @@ jobs:
! cat stdout.txt | grep --silent "Warning: version is unset"
env:
TF_VAR_atlas_token: ${{ secrets.ATLAS_TOKEN }}
integration-mysql:
name: Integration MySQL (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:
terraform-version:
- '1.5.*'
- '1.6.*'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.21'
- uses: actions/download-artifact@v3
with:
name: atlas-provider
path: ./dist
- uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ matrix.terraform-version }}
terraform_wrapper: false
- name: Terraform (skip-policy)
working-directory: integration-tests/skip-policy
run: |
Expand Down Expand Up @@ -254,9 +276,6 @@ jobs:
fail-fast: false
matrix:
terraform-version:
- '1.2.*'
- '1.3.*'
- '1.4.*'
- '1.5.*'
- '1.6.*'
steps:
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
**/.terraform.lock.hcl
**/.terraform/*
**/terraform.d
**/terraform.tfstate*
**/*.db
**/stdout.txt
**/actual.hcl
**/stdout.txt
22 changes: 18 additions & 4 deletions internal/provider/atlas_schema_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,17 @@ func (r *AtlasSchemaResource) ModifyPlan(ctx context.Context, req resource.Modif
}

func PrintPlanSQL(ctx context.Context, c *atlas.Client, devURL string, data *AtlasSchemaResourceModel) (diags diag.Diagnostics) {
u, err := absPath(data.URL.ValueString())
if err != nil {
diags.AddError("URL Error",
fmt.Sprintf("Unable to get absolute path for URL, got error: %s", err),
)
return
}
d := &schemaData{
Source: "schema.hcl",
URL: data.URL.ValueString(),
URL: u,
DevURL: devURL,
Source: "schema.hcl",
Diff: data.Diff,
}
diags.Append(data.GetExclude(ctx, &d.Exclude)...)
Expand Down Expand Up @@ -369,10 +376,17 @@ func PrintPlanSQL(ctx context.Context, c *atlas.Client, devURL string, data *Atl
}

func (r *AtlasSchemaResource) applySchema(ctx context.Context, data *AtlasSchemaResourceModel) (diags diag.Diagnostics) {
u, err := absPath(data.URL.ValueString())
if err != nil {
diags.AddError("URL Error",
fmt.Sprintf("Unable to get absolute path for URL, got error: %s", err),
)
return
}
d := &schemaData{
Source: "schema.hcl",
URL: data.URL.ValueString(),
URL: u,
DevURL: r.getDevURL(data.DevURL, data.DeprecatedDevURL),
Source: "schema.hcl",
Diff: data.Diff,
}
diags.Append(data.GetExclude(ctx, &d.Exclude)...)
Expand Down
20 changes: 20 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"bytes"
"context"
"fmt"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"

"github.com/mitchellh/go-homedir"

Expand Down Expand Up @@ -292,3 +294,21 @@ func execPath(dir, name string) (file string, err error) {
// try to find it in the PATH.
return exec.LookPath(name)
}

// absPath returns the absolute path of a file URL.
func absPath(path string) (string, error) {
u, err := url.Parse(path)
if err != nil {
return "", err
}
switch s := u.Scheme; strings.ToLower(s) {
case "file", "sqlite":
scheme := fmt.Sprintf("%s://", s)
p, err := filepath.Abs(strings.TrimPrefix(path, scheme))
if err != nil {
return "", err
}
return scheme + p, nil
}
return path, nil
}

0 comments on commit 0694cf2

Please sign in to comment.