-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a 'set replica identity' operation (#201)
Add support for a new **set replica identity** operation type. The new operation looks like this: ```json { "name": "29_set_replica_identity", "operations": [ { "set_replica_identity": { "table": "fruits", "identity": { "type": "full" } } } ] } ``` This sets the replica identity for the `fruits` table to `FULL`. The other supported operation types are 'nothing', 'default', and 'index'. If the replica identity is being set to an index, the operation looks like: ```json { "name": "29_set_replica_identity", "operations": [ { "set_replica_identity": { "table": "fruits", "identity": { "type": "index", "index": "some_index_name" } } } ] } ``` The replica identity is set directly on the underlying table on operation start. This means that both versions of the table exposed in the new and old versioned views will have the new replica identity set. The docs have been updated with the details of the new operation [here](https://github.com/xataio/pgroll/blob/set-replica-identity/docs/README.md#set-replica-identity).
- Loading branch information
1 parent
edc78ac
commit 4f4d549
Showing
7 changed files
with
374 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "29_set_replica_identity", | ||
"operations": [ | ||
{ | ||
"set_replica_identity": { | ||
"table": "fruits", | ||
"identity": { | ||
"type": "index", | ||
"index": "_pgroll_new_fruits_pkey" | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/lib/pq" | ||
"github.com/xataio/pgroll/pkg/schema" | ||
) | ||
|
||
type OpSetReplicaIdentity struct { | ||
Table string `json:"table"` | ||
Identity ReplicaIdentity `json:"identity"` | ||
} | ||
|
||
type ReplicaIdentity struct { | ||
Type string `json:"type"` | ||
Index string `json:"index"` | ||
} | ||
|
||
var _ Operation = (*OpSetReplicaIdentity)(nil) | ||
|
||
func (o *OpSetReplicaIdentity) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error { | ||
// build the correct form of the `SET REPLICA IDENTITY` statement based on the`identity type | ||
identitySQL := strings.ToUpper(o.Identity.Type) | ||
if identitySQL == "INDEX" { | ||
identitySQL = fmt.Sprintf("USING INDEX %s", pq.QuoteIdentifier(o.Identity.Index)) | ||
} | ||
|
||
// set the replica identity on the underlying table | ||
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s REPLICA IDENTITY %s", o.Table, identitySQL)) | ||
return err | ||
} | ||
|
||
func (o *OpSetReplicaIdentity) Complete(ctx context.Context, conn *sql.DB) error { | ||
// No-op | ||
return nil | ||
} | ||
|
||
func (o *OpSetReplicaIdentity) Rollback(ctx context.Context, conn *sql.DB) error { | ||
// No-op | ||
return nil | ||
} | ||
|
||
func (o *OpSetReplicaIdentity) Validate(ctx context.Context, s *schema.Schema) error { | ||
identityType := strings.ToUpper(o.Identity.Type) | ||
|
||
table := s.GetTable(o.Table) | ||
if table == nil { | ||
return TableDoesNotExistError{Name: o.Table} | ||
} | ||
|
||
identities := []string{"NOTHING", "DEFAULT", "INDEX", "FULL"} | ||
if !slices.Contains(identities, identityType) { | ||
return InvalidReplicaIdentityError{Table: o.Table, Identity: o.Identity.Type} | ||
} | ||
|
||
if identityType == "INDEX" { | ||
if _, ok := table.Indexes[o.Identity.Index]; !ok { | ||
return IndexDoesNotExistError{Name: o.Identity.Index} | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.