Skip to content

Commit 4b1dbf3

Browse files
committed
fix(postgresqlpublication): Add forgotten management of publication ownership
1 parent cc50370 commit 4b1dbf3

File tree

6 files changed

+207
-44
lines changed

6 files changed

+207
-44
lines changed

internal/controller/postgresql/postgres/create-publication-builder.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type CreatePublicationBuilder struct {
1010
tablesPart string
1111
allTables string
1212
withPart string
13+
owner string
1314
tables []string
1415
schemaList []string
1516
}
@@ -79,6 +80,12 @@ func (b *CreatePublicationBuilder) SetForAllTables() *CreatePublicationBuilder {
7980
return b
8081
}
8182

83+
func (b *CreatePublicationBuilder) SetOwner(n string) *CreatePublicationBuilder {
84+
b.owner = n
85+
86+
return b
87+
}
88+
8289
func (b *CreatePublicationBuilder) SetName(n string) *CreatePublicationBuilder {
8390
b.name = n
8491

internal/controller/postgresql/postgres/postgres.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type PG interface { //nolint:interfacebloat // This is needed
6767
GetPublication(ctx context.Context, dbname, name string) (*PublicationResult, error)
6868
CreatePublication(ctx context.Context, dbname string, builder *CreatePublicationBuilder) error
6969
UpdatePublication(ctx context.Context, dbname, publicationName string, builder *UpdatePublicationBuilder) error
70+
ChangePublicationOwner(ctx context.Context, dbname string, publicationName string, owner string) error
7071
DropReplicationSlot(ctx context.Context, name string) error
7172
CreateReplicationSlot(ctx context.Context, dbname, name, plugin string) error
7273
GetReplicationSlot(ctx context.Context, name string) (*ReplicationSlotResult, error)

internal/controller/postgresql/postgres/publication.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ const (
1212
CreatePublicationSQLTemplate = `CREATE PUBLICATION "%s" %s %s`
1313
DropPublicationSQLTemplate = `DROP PUBLICATION "%s"`
1414
AlterPublicationRenameSQLTemplate = `ALTER PUBLICATION "%s" RENAME TO "%s"`
15+
AlterPublicationChangeOwnerSQLTemplate = `ALTER PUBLICATION "%s" OWNER TO "%s"`
1516
AlterPublicationGeneralOperationSQLTemplate = `ALTER PUBLICATION "%s" SET %s`
1617
GetPublicationSQLTemplate = `SELECT
17-
puballtables, pubinsert, pubupdate, pubdelete, pubtruncate, pubviaroot
18+
pg_catalog.pg_get_userbyid(pubowner), puballtables, pubinsert, pubupdate, pubdelete, pubtruncate, pubviaroot
1819
FROM pg_catalog.pg_publication
1920
WHERE pubname = '%s';`
2021
GetReplicationSlotSQLTemplate = `SELECT slot_name,plugin,database FROM pg_replication_slots WHERE slot_name = '%s'`
@@ -23,6 +24,7 @@ WHERE pubname = '%s';`
2324
)
2425

2526
type PublicationResult struct {
27+
Owner string
2628
AllTables bool
2729
Insert bool
2830
Update bool
@@ -178,6 +180,23 @@ func (c *pg) UpdatePublication(ctx context.Context, dbname, publicationName stri
178180
return nil
179181
}
180182

183+
func (c *pg) ChangePublicationOwner(ctx context.Context, dbname string, publicationName string, owner string) error {
184+
// Connect to db
185+
err := c.connect(dbname)
186+
if err != nil {
187+
return err
188+
}
189+
190+
// Change owner
191+
_, err = c.db.ExecContext(ctx, fmt.Sprintf(AlterPublicationChangeOwnerSQLTemplate, publicationName, owner))
192+
if err != nil {
193+
return err
194+
}
195+
196+
// Default
197+
return nil
198+
}
199+
181200
func (c *pg) CreatePublication(ctx context.Context, dbname string, builder *CreatePublicationBuilder) error {
182201
// Connect to db
183202
err := c.connect(dbname)
@@ -193,6 +212,12 @@ func (c *pg) CreatePublication(ctx context.Context, dbname string, builder *Crea
193212
return err
194213
}
195214

215+
// Change owner
216+
err = c.ChangePublicationOwner(ctx, dbname, builder.name, builder.owner)
217+
if err != nil {
218+
return err
219+
}
220+
196221
// Default
197222
return nil
198223
}
@@ -217,7 +242,7 @@ func (c *pg) GetPublication(ctx context.Context, dbname, name string) (*Publicat
217242

218243
for rows.Next() {
219244
// Scan
220-
err = rows.Scan(&res.AllTables, &res.Insert, &res.Update, &res.Delete, &res.Truncate, &res.PublicationViaRoot)
245+
err = rows.Scan(&res.Owner, &res.AllTables, &res.Insert, &res.Update, &res.Delete, &res.Truncate, &res.PublicationViaRoot)
221246
// Check error
222247
if err != nil {
223248
return nil, err

internal/controller/postgresql/postgresqlpublication_controller.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,18 @@ func (r *PostgresqlPublicationReconciler) mainReconcile(
258258
return r.manageError(ctx, reqLogger, instance, originalPatch, err)
259259
}
260260
}
261+
262+
// Check if owner are aligned
263+
if pgDB.Status.Roles.Owner != pubRes.Owner {
264+
reqLogger.Info("Owner aren't aligned, update need to be done")
265+
266+
// Change owner
267+
err = pg.ChangePublicationOwner(ctx, pgDB.Status.Database, nameToSearch, pgDB.Status.Roles.Owner)
268+
// Check error
269+
if err != nil {
270+
return r.manageError(ctx, reqLogger, instance, originalPatch, err)
271+
}
272+
}
261273
}
262274

263275
// Get replication slot
@@ -384,6 +396,9 @@ func (*PostgresqlPublicationReconciler) manageCreate(
384396
builder = builder.AddTable(table.TableName, table.Columns, table.AdditionalWhere)
385397
})
386398

399+
// Manage owner
400+
builder = builder.SetOwner(pgDB.Status.Roles.Owner)
401+
387402
// Create publication
388403
err := pg.CreatePublication(ctx, pgDB.Status.Database, builder)
389404
// Check error

0 commit comments

Comments
 (0)