diff --git a/adapters/mssql_adapter.go b/adapters/mssql_adapter.go index f50c69b..8260b8a 100644 --- a/adapters/mssql_adapter.go +++ b/adapters/mssql_adapter.go @@ -65,7 +65,7 @@ func (adapter mssqlAdapter) CreateDatabaseUser(ctx context.Context, database str } func (adapter mssqlAdapter) DeleteDatabaseUser(ctx context.Context, database string, username string) error { - query := fmt.Sprintf("USE [%s]; DROP USER %s;", database, username) + query := fmt.Sprintf("USE [%s]; DROP USER [%s];", database, username) _, err := adapter.db.ExecContext(ctx, query) return err } diff --git a/adapters/mysql_adapter.go b/adapters/mysql_adapter.go index 09737ad..abe5be0 100644 --- a/adapters/mysql_adapter.go +++ b/adapters/mysql_adapter.go @@ -21,13 +21,13 @@ func (adapter mysqlAdapter) HasDatabase(ctx context.Context, database string) (b } func (adapter mysqlAdapter) CreateDatabase(ctx context.Context, database string) error { - query := fmt.Sprintf("CREATE DATABASE %s;", database) + query := fmt.Sprintf("CREATE DATABASE `%s`;", database) _, err := adapter.db.ExecContext(ctx, query) return err } func (adapter mysqlAdapter) DeleteDatabase(ctx context.Context, database string) error { - query := fmt.Sprintf("DROP DATABASE %s;", database) + query := fmt.Sprintf("DROP DATABASE `%s`;", database) _, err := adapter.db.ExecContext(ctx, query) return err } @@ -42,13 +42,13 @@ func (adapter mysqlAdapter) HasDatabaseUserWithAccess(ctx context.Context, datab func (adapter mysqlAdapter) CreateDatabaseUser(ctx context.Context, database string, username string, password string) error { // make password sql safe quotedPassword := QuoteLiteral(password) - query := fmt.Sprintf("CREATE USER '%s'@'%%' IDENTIFIED BY %s;", username, quotedPassword) + query := fmt.Sprintf("CREATE USER `%s`@'%%' IDENTIFIED BY %s;", username, quotedPassword) _, err := adapter.db.ExecContext(ctx, query) if err != nil { return err } - query = fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%%';", database, username) + query = fmt.Sprintf("GRANT ALL PRIVILEGES ON `%s`.* TO `%s`@'%%';", database, username) _, err = adapter.db.ExecContext(ctx, query) if err != nil { return err @@ -59,7 +59,7 @@ func (adapter mysqlAdapter) CreateDatabaseUser(ctx context.Context, database str } func (adapter mysqlAdapter) DeleteDatabaseUser(ctx context.Context, database string, username string) error { - query := fmt.Sprintf("DROP USER %s;", username) + query := fmt.Sprintf("DROP USER '%s';", username) _, err := adapter.db.ExecContext(ctx, query) return err } diff --git a/adapters/postgres_adapter.go b/adapters/postgres_adapter.go index a53ab3d..ed68889 100644 --- a/adapters/postgres_adapter.go +++ b/adapters/postgres_adapter.go @@ -22,13 +22,13 @@ func (adapter postgresAdapter) HasDatabase(ctx context.Context, database string) } func (adapter postgresAdapter) CreateDatabase(ctx context.Context, database string) error { - query := fmt.Sprintf("CREATE DATABASE %s", database) + query := fmt.Sprintf("CREATE DATABASE \"%s\";", database) _, err := adapter.db.Exec(ctx, query) return err } func (adapter postgresAdapter) DeleteDatabase(ctx context.Context, database string) error { - query := fmt.Sprintf("DROP DATABASE %s", database) + query := fmt.Sprintf("DROP DATABASE \"%s\";", database) _, err := adapter.db.Exec(ctx, query) return err } @@ -46,13 +46,13 @@ func (adapter postgresAdapter) HasDatabaseUserWithAccess(ctx context.Context, da func (adapter postgresAdapter) CreateDatabaseUser(ctx context.Context, database string, username string, password string) error { // make password sql safe quotedPassword := QuoteLiteral(password) - query := fmt.Sprintf("CREATE USER %s WITH PASSWORD %s", username, quotedPassword) + query := fmt.Sprintf("CREATE USER \"%s\" WITH PASSWORD %s", username, quotedPassword) _, err := adapter.db.Exec(ctx, query) if err != nil { return err } - query = fmt.Sprintf("GRANT ALL PRIVILEGES ON DATABASE %s TO %s;", database, username) + query = fmt.Sprintf("GRANT ALL PRIVILEGES ON DATABASE \"%s\" TO \"%s\";", database, username) _, err = adapter.db.Exec(ctx, query) return err @@ -72,19 +72,19 @@ func (adapter postgresAdapter) DeleteDatabaseUser(ctx context.Context, database } defer conn.Close(ctx) - query := fmt.Sprintf("DROP OWNED BY %s;", username) + query := fmt.Sprintf("DROP OWNED BY \"%s\";", username) _, err = conn.Exec(ctx, query) if err != nil { return err } - query = fmt.Sprintf("REVOKE ALL PRIVILEGES ON DATABASE %s FROM %s; REVOKE ALL ON SCHEMA public FROM %s;", database, username, username) + query = fmt.Sprintf("REVOKE ALL PRIVILEGES ON DATABASE \"%s\" FROM \"%s\"; REVOKE ALL ON SCHEMA public FROM \"%s\";", database, username, username) _, err = adapter.db.Exec(ctx, query) if err != nil { return err } - query = fmt.Sprintf("DROP USER %s;", username) + query = fmt.Sprintf("DROP USER \"%s\";", username) _, err = adapter.db.Exec(ctx, query) return err } diff --git a/adapters/utils_test.go b/adapters/utils_test.go index 95517ce..b068df2 100644 --- a/adapters/utils_test.go +++ b/adapters/utils_test.go @@ -12,9 +12,9 @@ type ClientConnectTest func(ctx context.Context, databaseName string, databaseUs func testHelper(t *testing.T, ctx context.Context, adapter adapters.DatabaseAdapter, clientConnectTest ClientConnectTest) { // given var err error - databaseName := "guestbook" - databaseUsername := "guestbook_admin" - databasePassword := "top-secret-123" + databaseName := "guestbook-admin_123" + databaseUsername := "guestbook-admin_123" + databasePassword := "top_secret-123!" t.Cleanup(func() { if err = adapter.DeleteDatabaseUser(ctx, databaseName, databaseUsername); err != nil {