Skip to content

Commit

Permalink
Add support for MariaDB
Browse files Browse the repository at this point in the history
  • Loading branch information
petoju committed Aug 3, 2022
1 parent 1a819a4 commit deefecd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
14 changes: 13 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bin/terraform:
testacc: fmtcheck bin/terraform
PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout=60s

acceptance: testversion5.6 testversion5.7 testversion8.0 testpercona5.7 testpercona8.0 testtidb6.1.0
acceptance: testversion5.6 testversion5.7 testversion8.0 testpercona5.7 testpercona8.0 testmariadb10.3 testmariadb10.8 testtidb6.1.0

testversion%:
$(MAKE) MYSQL_VERSION=$* MYSQL_PORT=33$(shell echo "$*" | tr -d '.') testversion
Expand Down Expand Up @@ -59,6 +59,18 @@ testtidb:
MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc
docker rm -f test-tidb$(MYSQL_VERSION)

testmariadb%:
$(MAKE) MYSQL_VERSION=$* MYSQL_PORT=36$(shell echo "$*" | tr -d '.') testmariadb

testmariadb:
-docker run --rm --name test-mariadb$(MYSQL_VERSION) -e MYSQL_ROOT_PASSWORD="$(TEST_PASSWORD)" -d -p $(MYSQL_PORT):3306 mariadb:$(MYSQL_VERSION)
@echo 'Waiting for MySQL...'
@while ! mysql -h 127.0.0.1 -P $(MYSQL_PORT) -u "$(TEST_USER)" -p"$(TEST_PASSWORD)" -e 'SELECT 1' >/dev/null 2>&1; do printf '.'; sleep 1; done ; echo ; echo "Connected!"
MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="$(TEST_PASSWORD)" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc
docker rm -f test-mariadb$(MYSQL_VERSION)



vet:
@echo "go vet ."
@go vet $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \
Expand Down
17 changes: 17 additions & 0 deletions mysql/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,20 @@ func testAccPreCheckSkipTiDB(t *testing.T) {
t.Skip("Skip on TiDB")
}
}

func testAccPreCheckSkipMariaDB(t *testing.T) {
testAccPreCheck(t)
db, err := connectToMySQL(testAccProvider.Meta().(*MySQLConfiguration))
if err != nil {
return
}

currentVersionString, err := serverVersionString(db)
if err != nil {
return
}

if strings.Contains(currentVersionString, "MariaDB") {
t.Skip("Skip on MariaDB")
}
}
2 changes: 1 addition & 1 deletion mysql/resource_global_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestAccGlobalVar_basic(t *testing.T) {
resourceName := "mysql_global_variable.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipMariaDB(t) },
Providers: testAccProviders,
CheckDestroy: testAccGlobalVarCheckDestroy(varName),
Steps: []resource.TestStep{
Expand Down
12 changes: 9 additions & 3 deletions mysql/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,16 @@ func ReadUser(d *schema.ResourceData, meta interface{}) error {
d.Set("auth_plugin", m[3])
d.Set("auth_string_hashed", m[4])
d.Set("tls_option", m[5])
} else {
return fmt.Errorf("Create user couldn't be parsed - it is %s", createUserStmt)
return nil
}
return nil

// Try 2 - just whether the user is there.
re2 := regexp.MustCompile("^CREATE USER")
if m := re2.FindStringSubmatch(createUserStmt); m != nil {
// Ok, we have at least something - it's probably in MariaDB.
return nil
}
return fmt.Errorf("Create user couldn't be parsed - it is %s", createUserStmt)
} else {
// Worse user detection, only for compat with MySQL 5.6
stmtSQL := fmt.Sprintf("SELECT USER FROM mysql.user WHERE USER='%s'",
Expand Down
4 changes: 2 additions & 2 deletions mysql/resource_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestAccUser_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipMariaDB(t) },
Providers: testAccProviders,
CheckDestroy: testAccUserCheckDestroy,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestAccUser_basic(t *testing.T) {

func TestAccUser_auth(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckSkipTiDB(t) },
PreCheck: func() { testAccPreCheckSkipTiDB(t); testAccPreCheckSkipMariaDB(t) },
Providers: testAccProviders,
CheckDestroy: testAccUserCheckDestroy,
Steps: []resource.TestStep{
Expand Down

0 comments on commit deefecd

Please sign in to comment.