Skip to content

Commit

Permalink
chore: sort create and update options with same order they are docume…
Browse files Browse the repository at this point in the history
…nted in PG docs og CREATE and ALTER database

Signed-off-by: Gabriele Quaresima <gabriele.quaresima@enterprisedb.com>
  • Loading branch information
gabriele-wolfox authored and mnencia committed Sep 19, 2024
1 parent 8e52707 commit 9b240c0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 49 deletions.
56 changes: 28 additions & 28 deletions internal/management/controller/database_controller_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ func createDatabase(
obj *apiv1.Database,
) error {
sqlCreateDatabase := fmt.Sprintf("CREATE DATABASE %s ", pgx.Identifier{obj.Spec.Name}.Sanitize())
if obj.Spec.IsTemplate != nil {
sqlCreateDatabase += fmt.Sprintf(" IS_TEMPLATE %v", *obj.Spec.IsTemplate)
}
if len(obj.Spec.Owner) > 0 {
sqlCreateDatabase += fmt.Sprintf(" OWNER %s", pgx.Identifier{obj.Spec.Owner}.Sanitize())
}
Expand All @@ -72,6 +69,9 @@ func createDatabase(
if obj.Spec.ConnectionLimit != nil {
sqlCreateDatabase += fmt.Sprintf(" CONNECTION LIMIT %v", *obj.Spec.ConnectionLimit)
}
if obj.Spec.IsTemplate != nil {
sqlCreateDatabase += fmt.Sprintf(" IS_TEMPLATE %v", *obj.Spec.IsTemplate)
}

_, err := db.ExecContext(ctx, sqlCreateDatabase)

Expand All @@ -83,30 +83,6 @@ func updateDatabase(
db *sql.DB,
obj *apiv1.Database,
) error {
if len(obj.Spec.Owner) > 0 {
changeOwnerSQL := fmt.Sprintf(
"ALTER DATABASE %s OWNER TO %s",
pgx.Identifier{obj.Spec.Name}.Sanitize(),
pgx.Identifier{obj.Spec.Owner}.Sanitize())

if _, err := db.ExecContext(ctx, changeOwnerSQL); err != nil {
return fmt.Errorf("while altering database %q owner %s to: %w",
obj.Spec.Name, obj.Spec.Owner, err)
}
}

if obj.Spec.IsTemplate != nil {
changeIsTemplateSQL := fmt.Sprintf(
"ALTER DATABASE %s WITH IS_TEMPLATE %v",
pgx.Identifier{obj.Spec.Name}.Sanitize(),
*obj.Spec.IsTemplate)

if _, err := db.ExecContext(ctx, changeIsTemplateSQL); err != nil {
return fmt.Errorf("while altering database %q with is_template %t: %w",
obj.Spec.Name, *obj.Spec.IsTemplate, err)
}
}

if obj.Spec.AllowConnections != nil {
changeAllowConnectionsSQL := fmt.Sprintf(
"ALTER DATABASE %s WITH ALLOW_CONNECTIONS %v",
Expand All @@ -131,14 +107,38 @@ func updateDatabase(
}
}

if obj.Spec.IsTemplate != nil {
changeIsTemplateSQL := fmt.Sprintf(
"ALTER DATABASE %s WITH IS_TEMPLATE %v",
pgx.Identifier{obj.Spec.Name}.Sanitize(),
*obj.Spec.IsTemplate)

if _, err := db.ExecContext(ctx, changeIsTemplateSQL); err != nil {
return fmt.Errorf("while altering database %q with is_template %t: %w",
obj.Spec.Name, *obj.Spec.IsTemplate, err)
}
}

if len(obj.Spec.Owner) > 0 {
changeOwnerSQL := fmt.Sprintf(
"ALTER DATABASE %s OWNER TO %s",
pgx.Identifier{obj.Spec.Name}.Sanitize(),
pgx.Identifier{obj.Spec.Owner}.Sanitize())

if _, err := db.ExecContext(ctx, changeOwnerSQL); err != nil {
return fmt.Errorf("while altering database %q owner %s to: %w",
obj.Spec.Name, obj.Spec.Owner, err)
}
}

if len(obj.Spec.Tablespace) > 0 {
changeTablespaceSQL := fmt.Sprintf(
"ALTER DATABASE %s SET TABLESPACE %s",
pgx.Identifier{obj.Spec.Name}.Sanitize(),
pgx.Identifier{obj.Spec.Tablespace}.Sanitize())

if _, err := db.ExecContext(ctx, changeTablespaceSQL); err != nil {
return fmt.Errorf("while altering database %q when setting tablespace %s: %w",
return fmt.Errorf("while altering database %q tablespace %s: %w",
obj.Spec.Name, obj.Spec.Tablespace, err)
}
}
Expand Down
42 changes: 21 additions & 21 deletions internal/management/controller/database_controller_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ var _ = Describe("Managed Database SQL", func() {

expectedValue := sqlmock.NewResult(0, 1)
expectedQuery := fmt.Sprintf(
"CREATE DATABASE %s IS_TEMPLATE %v OWNER %s TABLESPACE %s "+
"ALLOW_CONNECTIONS %v CONNECTION LIMIT %v",
pgx.Identifier{database.Spec.Name}.Sanitize(), *database.Spec.IsTemplate,
pgx.Identifier{database.Spec.Owner}.Sanitize(), pgx.Identifier{database.Spec.Tablespace}.Sanitize(),
*database.Spec.AllowConnections, *database.Spec.ConnectionLimit,
"CREATE DATABASE %s OWNER %s TABLESPACE %s "+
"ALLOW_CONNECTIONS %t CONNECTION LIMIT %d IS_TEMPLATE %t",
pgx.Identifier{database.Spec.Name}.Sanitize(), pgx.Identifier{database.Spec.Owner}.Sanitize(),
pgx.Identifier{database.Spec.Tablespace}.Sanitize(), *database.Spec.AllowConnections,
*database.Spec.ConnectionLimit, *database.Spec.IsTemplate,
)
dbMock.ExpectExec(expectedQuery).WillReturnResult(expectedValue)

Expand All @@ -117,22 +117,6 @@ var _ = Describe("Managed Database SQL", func() {

expectedValue := sqlmock.NewResult(0, 1)

// Mock Owner DDL
ownerExpectedQuery := fmt.Sprintf(
"ALTER DATABASE %s OWNER TO %s",
pgx.Identifier{database.Spec.Name}.Sanitize(),
pgx.Identifier{database.Spec.Owner}.Sanitize(),
)
dbMock.ExpectExec(ownerExpectedQuery).WillReturnResult(expectedValue)

// Mock IsTemplate DDL
isTemplateExpectedQuery := fmt.Sprintf(
"ALTER DATABASE %s WITH IS_TEMPLATE %v",
pgx.Identifier{database.Spec.Name}.Sanitize(),
*database.Spec.IsTemplate,
)
dbMock.ExpectExec(isTemplateExpectedQuery).WillReturnResult(expectedValue)

// Mock AllowConnections DDL
allowConnectionsExpectedQuery := fmt.Sprintf(
"ALTER DATABASE %s WITH ALLOW_CONNECTIONS %v",
Expand All @@ -149,6 +133,22 @@ var _ = Describe("Managed Database SQL", func() {
)
dbMock.ExpectExec(connectionLimitExpectedQuery).WillReturnResult(expectedValue)

// Mock IsTemplate DDL
isTemplateExpectedQuery := fmt.Sprintf(
"ALTER DATABASE %s WITH IS_TEMPLATE %v",
pgx.Identifier{database.Spec.Name}.Sanitize(),
*database.Spec.IsTemplate,
)
dbMock.ExpectExec(isTemplateExpectedQuery).WillReturnResult(expectedValue)

// Mock Owner DDL
ownerExpectedQuery := fmt.Sprintf(
"ALTER DATABASE %s OWNER TO %s",
pgx.Identifier{database.Spec.Name}.Sanitize(),
pgx.Identifier{database.Spec.Owner}.Sanitize(),
)
dbMock.ExpectExec(ownerExpectedQuery).WillReturnResult(expectedValue)

// Mock Tablespace DDL
tablespaceExpectedQuery := fmt.Sprintf(
"ALTER DATABASE %s SET TABLESPACE %s",
Expand Down

0 comments on commit 9b240c0

Please # to comment.