Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions server/internal/resource/migrations/1_0_0.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ func (v *Version_1_0_0) Run(state *resource.State) error {
}

type v1_0_0_databaseParams struct {
databaseID string
databaseName string
databaseOwner string
renameFrom string
hasRestoreConfig bool
}

func (v *Version_1_0_0) extractDatabaseParams(instances map[string]*resource.ResourceData) (*v1_0_0_databaseParams, error) {
var databaseID string
var databaseName string
var databaseOwner string
var renameFrom string
Expand All @@ -92,6 +94,7 @@ func (v *Version_1_0_0) extractDatabaseParams(instances map[string]*resource.Res
if instance.Spec.DatabaseName == "" {
continue
}
databaseID = instance.Spec.DatabaseID
databaseName = instance.Spec.DatabaseName
for _, user := range instance.Spec.DatabaseUsers {
if user.DBOwner {
Expand All @@ -106,6 +109,7 @@ func (v *Version_1_0_0) extractDatabaseParams(instances map[string]*resource.Res
}

return &v1_0_0_databaseParams{
databaseID: databaseID,
databaseName: databaseName,
databaseOwner: databaseOwner,
renameFrom: renameFrom,
Expand All @@ -117,6 +121,7 @@ func (v *Version_1_0_0) addDatabaseResources(state *resource.State, nodes map[st
for _, node := range nodes {
nodeName := node.Identifier.ID
dbResource := &v1_0_0.PostgresDatabaseResource{
DatabaseID: params.databaseID,
NodeName: nodeName,
DatabaseName: params.databaseName,
Owner: params.databaseOwner,
Expand Down Expand Up @@ -289,13 +294,35 @@ func (v *Version_1_0_0) migrateReplicationSlotResources(state *resource.State, p
return nil
}

func (v *Version_1_0_0) newReplicationSlotResource(sub v1_0_0.SubscriptionResource) (*resource.ResourceData, error) {
new := v1_0_0.ReplicationSlotResource{
ProviderNode: sub.ProviderNode,
SubscriberNode: sub.SubscriberNode,
DatabaseName: sub.DatabaseName,
}
attrs, err := json.Marshal(new)
if err != nil {
return nil, fmt.Errorf("failed to marshal new replication slot resource: %w", err)
}
return &resource.ResourceData{
Identifier: v1_0_0.ReplicationSlotResourceIdentifier(new.ProviderNode, new.SubscriberNode, new.DatabaseName),
Attributes: attrs,
Dependencies: []resource.Identifier{
v1_0_0.PostgresDatabaseResourceIdentifier(new.ProviderNode, new.DatabaseName),
},
Executor: resource.PrimaryExecutor(sub.ProviderNode),
ResourceVersion: "1",
}, nil
}

func (v *Version_1_0_0) migrateSubscriptionResources(state *resource.State, params *v1_0_0_databaseParams) error {
resources, ok := state.Resources[v0_0_0.ResourceTypeSubscription]
slots := state.Resources[v0_0_0.ResourceTypeReplicationSlot]
subs, ok := state.Resources[v0_0_0.ResourceTypeSubscription]
if !ok {
return nil
}
adds := make([]*resource.ResourceData, 0, len(resources))
for oldID, data := range resources {
adds := make([]*resource.ResourceData, 0, len(subs)*2)
for oldID, data := range subs {
var old v0_0_0.SubscriptionResource
if err := json.Unmarshal(data.Attributes, &old); err != nil {
return fmt.Errorf("failed to unmarshal old subscription resource: %w", err)
Expand Down Expand Up @@ -350,7 +377,18 @@ func (v *Version_1_0_0) migrateSubscriptionResources(state *resource.State, para
Error: data.Error,
TypeDependencies: data.TypeDependencies,
})
delete(resources, oldID)
// We're checking both identifier versions here so that we're not
// dependent on the order of migration calls.
_, v0SlotExists := slots[v0_0_0.ReplicationSlotResourceIdentifier(new.ProviderNode, new.SubscriberNode).ID]
_, v1SlotExists := slots[v1_0_0.ReplicationSlotResourceIdentifier(new.ProviderNode, new.SubscriberNode, new.DatabaseName).ID]
if !v0SlotExists && !v1SlotExists {
slot, err := v.newReplicationSlotResource(new)
if err != nil {
return err
}
adds = append(adds, slot)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
delete(subs, oldID)
}
state.Add(adds...)

Expand Down
19 changes: 19 additions & 0 deletions server/internal/resource/migrations/1_0_0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ func TestVersion_1_0_0(t *testing.T) {
v0_0_0_subscriptionResource(t, "n3", "n2"),
},
},
{
// Databases created before v0.7.0 will not have replication slot
// resources.
name: "three nodes without slots",
in: []*resource.ResourceData{
v0_0_0_node(t, "n1", "instance-1"),
v0_0_0_node(t, "n2", "instance-2"),
v0_0_0_node(t, "n3", "instance-3"),
v0_0_0_instance(t, "instance-1", "host-1", "n1"),
v0_0_0_instance(t, "instance-2", "host-2", "n2"),
v0_0_0_instance(t, "instance-3", "host-3", "n3"),
v0_0_0_subscriptionResource(t, "n1", "n2"),
v0_0_0_subscriptionResource(t, "n1", "n3"),
v0_0_0_subscriptionResource(t, "n2", "n1"),
v0_0_0_subscriptionResource(t, "n2", "n3"),
v0_0_0_subscriptionResource(t, "n3", "n1"),
v0_0_0_subscriptionResource(t, "n3", "n2"),
},
},
{
// This is what it would look like if we were to migrate a state
// while it's partway through an "add node" operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
"type": "database.postgres_database"
},
"attributes": {
"database_id": "database-1",
"node_name": "n1",
"database_name": "test",
"owner": "admin",
Expand Down Expand Up @@ -282,6 +283,7 @@
"type": "database.postgres_database"
},
"attributes": {
"database_id": "database-1",
"node_name": "n2",
"database_name": "test",
"owner": "admin",
Expand Down Expand Up @@ -312,6 +314,7 @@
"type": "database.postgres_database"
},
"attributes": {
"database_id": "database-1",
"node_name": "n3",
"database_name": "test",
"owner": "admin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
"type": "database.postgres_database"
},
"attributes": {
"database_id": "database-1",
"node_name": "n1",
"database_name": "test",
"owner": "admin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
"type": "database.postgres_database"
},
"attributes": {
"database_id": "database-1",
"node_name": "n1",
"database_name": "test",
"owner": "admin",
Expand Down Expand Up @@ -239,6 +240,7 @@
"type": "database.postgres_database"
},
"attributes": {
"database_id": "database-1",
"node_name": "n2",
"database_name": "test",
"owner": "admin",
Expand Down Expand Up @@ -269,6 +271,7 @@
"type": "database.postgres_database"
},
"attributes": {
"database_id": "database-1",
"node_name": "n3",
"database_name": "test",
"owner": "admin",
Expand Down
Loading