Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
pull_request:

jobs:
test:
test-pg:
runs-on: ubuntu-latest

strategy:
Expand All @@ -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
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ."
Expand Down
35 changes: 26 additions & 9 deletions postgresql/resource_postgresql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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); {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/docker-compose-crdb.yml
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion tests/docker-compose.yml → tests/docker-compose-pg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ services:
test: [ "CMD-SHELL", "pg_isready" ]
interval: 10s
timeout: 5s
retries: 5
retries: 5
9 changes: 9 additions & 0 deletions tests/switch_crdb.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/testacc_cleanup.sh
Original file line number Diff line number Diff line change
@@ -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
22 changes: 15 additions & 7 deletions tests/testacc_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ log() {
}

setup() {
"$(pwd)"/tests/testacc_setup.sh
"$(pwd)"/tests/testacc_setup.sh $1
}

run() {
Expand All @@ -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


2 changes: 1 addition & 1 deletion tests/testacc_setup.sh
Original file line number Diff line number Diff line change
@@ -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