Skip to content

Commit c282342

Browse files
committed
fix(postgresqldatabase): Avoid alters on tables & types when owner is already the right one
1 parent f3b09ff commit c282342

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

internal/controller/postgresql/postgres/database.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ const (
2121
GrantUsageSchemaSQLTemplate = `GRANT USAGE ON SCHEMA "%s" TO "%s"`
2222
GrantAllTablesSQLTemplate = `GRANT %s ON ALL TABLES IN SCHEMA "%s" TO "%s"`
2323
DefaultPrivsSchemaSQLTemplate = `ALTER DEFAULT PRIVILEGES FOR ROLE "%s" IN SCHEMA "%s" GRANT %s ON TABLES TO "%s"`
24-
GetTablesFromSchemaSQLTemplate = `SELECT table_name FROM information_schema.tables WHERE table_schema = '%s'`
24+
GetTablesFromSchemaSQLTemplate = `SELECT tablename,tableowner FROM pg_tables WHERE schemaname = '%s'`
2525
ChangeTableOwnerSQLTemplate = `ALTER TABLE IF EXISTS "%s" OWNER TO "%s"`
2626
ChangeTypeOwnerSQLTemplate = `ALTER TYPE "%s"."%s" OWNER TO "%s"`
2727
// Got and edited from : https://stackoverflow.com/questions/3660787/how-to-list-custom-types-using-postgres-information-schema
28-
GetTypesFromSchemaSQLTemplate = `SELECT t.typname as type
28+
GetTypesFromSchemaSQLTemplate = `SELECT t.typname as type, pg_catalog.pg_get_userbyid(t.typowner) as owner
2929
FROM pg_type t
3030
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
3131
WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid))
@@ -34,6 +34,16 @@ AND n.nspname = '%s';`
3434
DuplicateDatabaseErrorCode = "42P04"
3535
)
3636

37+
type TableOwnership struct {
38+
TableName string
39+
Owner string
40+
}
41+
42+
type TypeOwnership struct {
43+
TypeName string
44+
Owner string
45+
}
46+
3747
func (c *pg) IsDatabaseExist(dbname string) (bool, error) {
3848
err := c.connect(c.defaultDatabase)
3949
if err != nil {
@@ -114,7 +124,7 @@ func (c *pg) CreateSchema(db, role, schema string) error {
114124
return nil
115125
}
116126

117-
func (c *pg) GetTablesInSchema(db, schema string) ([]string, error) {
127+
func (c *pg) GetTablesInSchema(db, schema string) ([]*TableOwnership, error) {
118128
err := c.connect(db)
119129
if err != nil {
120130
return nil, err
@@ -127,18 +137,18 @@ func (c *pg) GetTablesInSchema(db, schema string) ([]string, error) {
127137

128138
defer rows.Close()
129139

130-
res := []string{}
140+
res := []*TableOwnership{}
131141

132142
for rows.Next() {
133-
tableName := ""
143+
it := &TableOwnership{}
134144
// Scan
135-
err = rows.Scan(&tableName)
145+
err = rows.Scan(&it.TableName, &it.Owner)
136146
// Check error
137147
if err != nil {
138148
return nil, err
139149
}
140150
// Save
141-
res = append(res, tableName)
151+
res = append(res, it)
142152
}
143153

144154
// Rows error
@@ -165,7 +175,7 @@ func (c *pg) ChangeTableOwner(db, table, owner string) error {
165175
return nil
166176
}
167177

168-
func (c *pg) GetTypesInSchema(db, schema string) ([]string, error) {
178+
func (c *pg) GetTypesInSchema(db, schema string) ([]*TypeOwnership, error) {
169179
err := c.connect(db)
170180
if err != nil {
171181
return nil, err
@@ -178,18 +188,18 @@ func (c *pg) GetTypesInSchema(db, schema string) ([]string, error) {
178188

179189
defer rows.Close()
180190

181-
res := []string{}
191+
res := []*TypeOwnership{}
182192

183193
for rows.Next() {
184-
typeName := ""
194+
it := &TypeOwnership{}
185195
// Scan
186-
err = rows.Scan(&typeName)
196+
err = rows.Scan(&it.TypeName, &it.Owner)
187197
// Check error
188198
if err != nil {
189199
return nil, err
190200
}
191201
// Save
192-
res = append(res, typeName)
202+
res = append(res, it)
193203
}
194204

195205
// Rows error

internal/controller/postgresql/postgres/postgres.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ type PG interface { //nolint:interfacebloat // This is needed
4444
DropSchema(database, schema string, cascade bool) error
4545
DropExtension(database, extension string, cascade bool) error
4646
GetRoleMembership(role string) ([]string, error)
47-
GetTablesInSchema(db, schema string) ([]string, error)
47+
GetTablesInSchema(db, schema string) ([]*TableOwnership, error)
4848
ChangeTableOwner(db, table, owner string) error
49-
GetTypesInSchema(db, schema string) ([]string, error)
49+
GetTypesInSchema(db, schema string) ([]*TypeOwnership, error)
5050
ChangeTypeOwnerInSchema(db, schema, typeName, owner string) error
5151
DropPublication(dbname, name string) error
5252
RenamePublication(dbname, oldname, newname string) error

internal/controller/postgresql/postgresqldatabase_controller.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -512,32 +512,38 @@ func (*PostgresqlDatabaseReconciler) manageSchemas(pg postgres.PG, instance *pos
512512
}
513513

514514
// Get list of tables inside schema
515-
tableNames, err := pg.GetTablesInSchema(instance.Spec.Database, schema)
515+
tableOwnerships, err := pg.GetTablesInSchema(instance.Spec.Database, schema)
516516
if err != nil {
517517
return err
518518
}
519519

520520
// Loop over all tables to force owner
521-
for _, tableName := range tableNames {
522-
// Force table owner
523-
err = pg.ChangeTableOwner(instance.Spec.Database, tableName, owner)
524-
if err != nil {
525-
return err
521+
for _, tableOwnershipItem := range tableOwnerships {
522+
// Check if it is needed to patch owner
523+
if tableOwnershipItem.Owner != owner {
524+
// Force table owner
525+
err = pg.ChangeTableOwner(instance.Spec.Database, tableOwnershipItem.TableName, owner)
526+
if err != nil {
527+
return err
528+
}
526529
}
527530
}
528531

529-
// Get list of types inside schema
530-
types, err := pg.GetTypesInSchema(instance.Spec.Database, schema)
532+
// Get list of typeOwnerships inside schema
533+
typeOwnerships, err := pg.GetTypesInSchema(instance.Spec.Database, schema)
531534
if err != nil {
532535
return err
533536
}
534537

535538
// Loop over all types to force owner
536-
for _, typeName := range types {
537-
// Force table owner
538-
err = pg.ChangeTypeOwnerInSchema(instance.Spec.Database, schema, typeName, owner)
539-
if err != nil {
540-
return err
539+
for _, typeOwnershipItem := range typeOwnerships {
540+
// Check if it is needed to patch owner
541+
if typeOwnershipItem.Owner != owner {
542+
// Force table owner
543+
err = pg.ChangeTypeOwnerInSchema(instance.Spec.Database, schema, typeOwnershipItem.TypeName, owner)
544+
if err != nil {
545+
return err
546+
}
541547
}
542548
}
543549

0 commit comments

Comments
 (0)