diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1ec3320b..14e75b40 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,7 +7,7 @@ on: pull_request: jobs: - test: + test-pg: runs-on: ubuntu-latest strategy: @@ -33,4 +33,32 @@ jobs: run: make vet - name: testacc - run: make testacc + run: make testacc tech=pg + + test-crdb: + runs-on: ubuntu-latest + + strategy: + matrix: + crdbversion: [ v23.2 ] + + env: + CRDBVERSION: ${{ matrix.crdbversion }} + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.20' + + - name: test + run: make test + + - name: vet + run: make vet + + - name: testacc + run: make testacc tech=crdb diff --git a/.goreleaser.yml b/.goreleaser.yml index 34fef1d2..a5366780 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,5 +1,6 @@ # Visit https://goreleaser.com for documentation on how to customize this # behavior. +version: 2 before: hooks: # this is just an example and not a requirement for provider building/publishing diff --git a/Makefile b/Makefile index 8c764715..d2c02787 100644 --- a/Makefile +++ b/Makefile @@ -13,13 +13,13 @@ test: fmtcheck xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4 testacc_setup: fmtcheck - @sh -c "'$(CURDIR)/tests/testacc_setup.sh'" + @sh -c "'$(CURDIR)/tests/testacc_setup.sh' $(tech)" testacc_cleanup: fmtcheck - @sh -c "'$(CURDIR)/tests/testacc_cleanup.sh'" + @sh -c "'$(CURDIR)/tests/testacc_cleanup.sh' $(tech)" testacc: fmtcheck - @sh -c "'$(CURDIR)/tests/testacc_full.sh'" + sh -c "'$(CURDIR)/tests/testacc_full.sh' $(tech)" vet: @echo "go vet ." diff --git a/postgresql/resource_postgresql_database.go b/postgresql/resource_postgresql_database.go index efd4226c..cb14afc0 100644 --- a/postgresql/resource_postgresql_database.go +++ b/postgresql/resource_postgresql_database.go @@ -165,17 +165,29 @@ func createDatabase(db *DBConnection, d *schema.ResourceData) error { } //cockroachdb support only encoding = 'UTF-8' (instead of UTF8), not supports DEFAULT + //switch v, ok := d.GetOk(dbEncodingAttr); { + //case ok && db.dbType == dbTypePostgresql: + // fmt.Fprintf(b, " ENCODING = '%s' ", pqQuoteLiteral(v.(string))) + //case ok && v.(string) != "UTF8" && v.(string) != "UNICODE" && db.dbType == dbTypeCockroachdb: + // log.Printf("[ERROR] Cockroachdb does not support %s encoding", v.(string)) + //case v.(string) == "" && db.dbType == dbTypePostgresql: + // fmt.Fprint(b, ` ENCODING = 'UTF8'`) + //} + switch v, ok := d.GetOk(dbEncodingAttr); { case ok && strings.ToUpper(v.(string)) == "DEFAULT" && db.dbType == dbTypePostgresql: - fmt.Fprintf(b, " ENCODING = DEFAULT") - case ok: + fmt.Fprintf(b, " ENCODING DEFAULT") + case ok && db.dbType == dbTypePostgresql: + fmt.Fprintf(b, " ENCODING '%s' ", pqQuoteLiteral(v.(string))) + case ok && db.dbType == dbTypeCockroachdb && v.(string) != "UTF8" && v.(string) != "UTF-8" && v.(string) != "UNICODE": + log.Printf("[ERROR] Cockroachdb does not support %s encoding", v.(string)) + case ok && db.dbType == dbTypeCockroachdb && (v.(string) == "UTF8" || v.(string) == "UTF-8" || v.(string) != "UNICODE"): fmt.Fprintf(b, " ENCODING = '%s' ", pqQuoteLiteral(v.(string))) case v.(string) == "" && db.dbType == dbTypePostgresql: - fmt.Fprint(b, ` ENCODING = 'UTF8'`) + fmt.Fprint(b, ` ENCODING 'UTF8'`) case v.(string) == "" && db.dbType == dbTypeCockroachdb: - fmt.Fprint(b, ` ENCODING = 'UTF-8'`) + fmt.Fprint(b, ` ENCODING = 'UTF8'`) } - // Don't specify LC_COLLATE if user didn't specify it // This will use the default one (usually the one defined in the template database) switch v, ok := d.GetOk(dbCollationAttr); { @@ -208,8 +220,10 @@ func createDatabase(db *DBConnection, d *schema.ResourceData) error { fmt.Fprint(b, " ALLOW_CONNECTIONS ", val) } - { - val := d.Get(dbConnLimitAttr).(int) + val := d.Get(dbConnLimitAttr).(int) + if db.dbType == dbTypeCockroachdb && val == 0 { + log.Printf("[ERROR] Cockroachdb does not support setting CONNECTION LIMIT to 0") + } else { fmt.Fprint(b, " CONNECTION LIMIT ", val) } @@ -506,16 +520,19 @@ func setDBTablespace(db QueryAble, d *schema.ResourceData) error { return nil } -func setDBConnLimit(db QueryAble, d *schema.ResourceData) error { +func setDBConnLimit(db *DBConnection, d *schema.ResourceData) error { if !d.HasChange(dbConnLimitAttr) { return nil } connLimit := d.Get(dbConnLimitAttr).(int) dbName := d.Get(dbNameAttr).(string) + if db.dbType == dbTypeCockroachdb && connLimit == 0 { + return fmt.Errorf("Cockroachdb does not support setting CONNECTION LIMIT to 0") + } sql := fmt.Sprintf("ALTER DATABASE %s CONNECTION LIMIT = %d", pq.QuoteIdentifier(dbName), connLimit) if _, err := db.Exec(sql); err != nil { - return fmt.Errorf("Error updating database CONNECTION LIMIT: %w", err) + return fmt.Errorf("Error updating database CONNECTION LIMIT: %w %s", err, sql) } return nil diff --git a/tests/docker-compose-crdb.yml b/tests/docker-compose-crdb.yml new file mode 100644 index 00000000..361f0d2a --- /dev/null +++ b/tests/docker-compose-crdb.yml @@ -0,0 +1,20 @@ +version: "3" + +services: + cockroachdb: + image: cockroachdb/cockroach:latest-${CRDBVERSION} + command: + - "start-single-node" + - "--accept-sql-without-tls" + ports: + - 26257:26257 + - 8080:8080 + environment: + - COCKROACH_USER=${PGUSER} + - COCKROACH_PASSWORD=${PGPASSWORD} + healthcheck: + test: [ "CMD", "curl", "http://tests-cockroachdb-1:8080/health?ready=1" ] + interval: 2s + timeout: 1m + retries: 5 + diff --git a/tests/docker-compose.yml b/tests/docker-compose-pg.yml similarity index 95% rename from tests/docker-compose.yml rename to tests/docker-compose-pg.yml index 177994bf..1dd6c223 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose-pg.yml @@ -18,4 +18,4 @@ services: test: [ "CMD-SHELL", "pg_isready" ] interval: 10s timeout: 5s - retries: 5 + retries: 5 \ No newline at end of file diff --git a/tests/switch_crdb.sh b/tests/switch_crdb.sh new file mode 100755 index 00000000..13017375 --- /dev/null +++ b/tests/switch_crdb.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +export TF_ACC=true +export PGHOST=localhost +export PGPORT=26257 +export PGUSER=postgres +export PGPASSWORD=postgres +export PGSSLMODE=disable +export PGSUPERUSER=true diff --git a/tests/testacc_cleanup.sh b/tests/testacc_cleanup.sh index c6cb970b..3cf3006e 100755 --- a/tests/testacc_cleanup.sh +++ b/tests/testacc_cleanup.sh @@ -1,5 +1,5 @@ #!/bin/bash source "$(pwd)"/tests/switch_superuser.sh -docker compose -f "$(pwd)"/tests/docker-compose.yml down +docker compose -f "$(pwd)"/tests/docker-compose-${1}.yml down unset TF_ACC PGHOST PGPORT PGUSER PGPASSWORD PGSSLMODE PGSUPERUSER diff --git a/tests/testacc_full.sh b/tests/testacc_full.sh index 39b2941f..09addc5b 100755 --- a/tests/testacc_full.sh +++ b/tests/testacc_full.sh @@ -8,7 +8,7 @@ log() { } setup() { - "$(pwd)"/tests/testacc_setup.sh + "$(pwd)"/tests/testacc_setup.sh $1 } run() { @@ -19,16 +19,24 @@ run() { } cleanup() { - "$(pwd)"/tests/testacc_cleanup.sh + "$(pwd)"/tests/testacc_cleanup.sh $1 } run_suite() { suite=${1?} - log "setup ($1)" && setup + tech=${2?} + log "setup ($1)" && setup "$tech" source "./tests/switch_$suite.sh" - log "run ($1)" && run || (log "cleanup" && cleanup && exit 1) - log "cleanup ($1)" && cleanup + log "run ($1)" && run || (log "cleanup" && cleanup $tech && exit 1) + log "cleanup ($1)" && cleanup $tech } -run_suite "superuser" -run_suite "rds" + +if [ "$1" == "pg" ]; then + run_suite "superuser" "pg" + run_suite "rds" "pg" +else + run_suite "crdb" "crdb" +fi + + diff --git a/tests/testacc_setup.sh b/tests/testacc_setup.sh index 5b26abb8..121eddc0 100755 --- a/tests/testacc_setup.sh +++ b/tests/testacc_setup.sh @@ -1,4 +1,4 @@ #!/bin/bash source "$(pwd)"/tests/switch_superuser.sh -docker compose -f "$(pwd)"/tests/docker-compose.yml up -d --wait +docker compose -f "$(pwd)"/tests/docker-compose-${1}.yml up -d --wait