Skip to content

Commit

Permalink
feat: support more sql database & user names (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored May 2, 2022
1 parent 1ed43f3 commit 4194459
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion adapters/mssql_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions adapters/mysql_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions adapters/postgres_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions adapters/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4194459

Please sign in to comment.