-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add state migration to remove ServiceUserRole resources #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rshoemaker
merged 2 commits into
main
from
feat/PLAT-555/state-migration-service-user-role
Apr 21, 2026
+214
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,39 @@ | ||
| package migrations | ||
|
|
||
| import ( | ||
| "github.com/pgEdge/control-plane/server/internal/ds" | ||
| "github.com/pgEdge/control-plane/server/internal/resource" | ||
| ) | ||
|
|
||
| var _ resource.StateMigration = (*Version_1_1_0)(nil) | ||
|
|
||
| // Version_1_1_0 removes swarm.service_user_role resources and scrubs | ||
| // references to them from all other resources' dependency lists. | ||
| // Services now use connect_as to reference database_users directly. | ||
| type Version_1_1_0 struct{} | ||
|
|
||
| func (v *Version_1_1_0) Version() *ds.Version { | ||
| return resource.StateVersion_1_1_0 | ||
| } | ||
|
|
||
| func (v *Version_1_1_0) Run(state *resource.State) error { | ||
| const serviceUserRoleType resource.Type = "swarm.service_user_role" | ||
|
|
||
| // 1. Delete all service_user_role resources from state | ||
| delete(state.Resources, serviceUserRoleType) | ||
|
|
||
| // 2. Remove service_user_role from all other resources' dependency lists | ||
| for _, resources := range state.Resources { | ||
| for _, data := range resources { | ||
| filtered := data.Dependencies[:0] | ||
| for _, dep := range data.Dependencies { | ||
| if dep.Type != serviceUserRoleType { | ||
| filtered = append(filtered, dep) | ||
| } | ||
| } | ||
| data.Dependencies = filtered | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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,168 @@ | ||
| package migrations_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/pgEdge/control-plane/server/internal/resource" | ||
| "github.com/pgEdge/control-plane/server/internal/resource/migrations" | ||
| ) | ||
|
|
||
| func TestVersion_1_1_0(t *testing.T) { | ||
| serviceUserRoleType := resource.Type("swarm.service_user_role") | ||
| mcpConfigType := resource.Type("swarm.mcp_config") | ||
| serviceInstanceSpecType := resource.Type("swarm.service_instance_spec") | ||
| networkType := resource.Type("swarm.network") | ||
| dirType := resource.Type("swarm.dir") | ||
|
|
||
| svcRoleRO := resource.Identifier{ID: "appmcp-ro", Type: serviceUserRoleType} | ||
| svcRoleRW := resource.Identifier{ID: "appmcp-rw", Type: serviceUserRoleType} | ||
| networkDep := resource.Identifier{ID: "db-network", Type: networkType} | ||
| dirDep := resource.Identifier{ID: "data-dir", Type: dirType} | ||
|
|
||
| t.Run("removes service_user_role resources", func(t *testing.T) { | ||
| state := &resource.State{ | ||
| Version: resource.StateVersion_1_0_0.Clone(), | ||
| Resources: map[resource.Type]map[string]*resource.ResourceData{ | ||
| serviceUserRoleType: { | ||
| "appmcp-ro": {Identifier: svcRoleRO}, | ||
| "appmcp-rw": {Identifier: svcRoleRW}, | ||
| }, | ||
| mcpConfigType: { | ||
| "mcp-cfg": { | ||
| Identifier: resource.Identifier{ID: "mcp-cfg", Type: mcpConfigType}, | ||
| Dependencies: []resource.Identifier{dirDep, svcRoleRO, svcRoleRW}, | ||
| }, | ||
| }, | ||
| serviceInstanceSpecType: { | ||
| "svc-spec": { | ||
| Identifier: resource.Identifier{ID: "svc-spec", Type: serviceInstanceSpecType}, | ||
| Dependencies: []resource.Identifier{networkDep, svcRoleRO, svcRoleRW}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| migration := &migrations.Version_1_1_0{} | ||
| err := migration.Run(state) | ||
| require.NoError(t, err) | ||
|
|
||
| // service_user_role resources should be gone | ||
| _, exists := state.Resources[serviceUserRoleType] | ||
| assert.False(t, exists, "service_user_role resources should be deleted") | ||
|
|
||
| // mcp_config should have service_user_role deps removed | ||
| mcpCfg := state.Resources[mcpConfigType]["mcp-cfg"] | ||
| require.NotNil(t, mcpCfg) | ||
| assert.Equal(t, []resource.Identifier{dirDep}, mcpCfg.Dependencies) | ||
|
|
||
| // service_instance_spec should have service_user_role deps removed | ||
| svcSpec := state.Resources[serviceInstanceSpecType]["svc-spec"] | ||
| require.NotNil(t, svcSpec) | ||
| assert.Equal(t, []resource.Identifier{networkDep}, svcSpec.Dependencies) | ||
| }) | ||
|
|
||
| t.Run("removes service_user_role resources across all service types", func(t *testing.T) { | ||
| ragConfigType := resource.Type("swarm.rag_config") | ||
| postgrestConfigType := resource.Type("swarm.postgrest_config") | ||
|
|
||
| mcpRO := resource.Identifier{ID: "appmcp-ro", Type: serviceUserRoleType} | ||
| mcpRW := resource.Identifier{ID: "appmcp-rw", Type: serviceUserRoleType} | ||
| ragRO := resource.Identifier{ID: "apprag-ro", Type: serviceUserRoleType} | ||
| prstRO := resource.Identifier{ID: "appprst-ro", Type: serviceUserRoleType} | ||
| prstRW := resource.Identifier{ID: "appprst-rw", Type: serviceUserRoleType} | ||
|
|
||
| state := &resource.State{ | ||
| Version: resource.StateVersion_1_0_0.Clone(), | ||
| Resources: map[resource.Type]map[string]*resource.ResourceData{ | ||
| serviceUserRoleType: { | ||
| "appmcp-ro": {Identifier: mcpRO}, | ||
| "appmcp-rw": {Identifier: mcpRW}, | ||
| "apprag-ro": {Identifier: ragRO}, | ||
| "appprst-ro": {Identifier: prstRO}, | ||
| "appprst-rw": {Identifier: prstRW}, | ||
| }, | ||
| mcpConfigType: { | ||
| "mcp-cfg": { | ||
| Identifier: resource.Identifier{ID: "mcp-cfg", Type: mcpConfigType}, | ||
| Dependencies: []resource.Identifier{dirDep, mcpRO, mcpRW}, | ||
| }, | ||
| }, | ||
| ragConfigType: { | ||
| "rag-cfg": { | ||
| Identifier: resource.Identifier{ID: "rag-cfg", Type: ragConfigType}, | ||
| Dependencies: []resource.Identifier{dirDep, ragRO}, | ||
| }, | ||
| }, | ||
| postgrestConfigType: { | ||
| "prst-cfg": { | ||
| Identifier: resource.Identifier{ID: "prst-cfg", Type: postgrestConfigType}, | ||
| Dependencies: []resource.Identifier{dirDep, prstRO, prstRW}, | ||
| }, | ||
| }, | ||
| serviceInstanceSpecType: { | ||
| "svc-spec": { | ||
| Identifier: resource.Identifier{ID: "svc-spec", Type: serviceInstanceSpecType}, | ||
| Dependencies: []resource.Identifier{networkDep, mcpRO, mcpRW, ragRO, prstRO, prstRW}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| migration := &migrations.Version_1_1_0{} | ||
| err := migration.Run(state) | ||
| require.NoError(t, err) | ||
|
|
||
| // All service_user_role resources should be gone | ||
| _, exists := state.Resources[serviceUserRoleType] | ||
| assert.False(t, exists, "service_user_role resources should be deleted") | ||
|
|
||
| // MCP config: only dirDep remains | ||
| assert.Equal(t, []resource.Identifier{dirDep}, state.Resources[mcpConfigType]["mcp-cfg"].Dependencies) | ||
|
|
||
| // RAG config: only dirDep remains | ||
| assert.Equal(t, []resource.Identifier{dirDep}, state.Resources[ragConfigType]["rag-cfg"].Dependencies) | ||
|
|
||
| // PostgREST config: only dirDep remains | ||
| assert.Equal(t, []resource.Identifier{dirDep}, state.Resources[postgrestConfigType]["prst-cfg"].Dependencies) | ||
|
|
||
| // service_instance_spec: only networkDep remains | ||
| assert.Equal(t, []resource.Identifier{networkDep}, state.Resources[serviceInstanceSpecType]["svc-spec"].Dependencies) | ||
| }) | ||
|
|
||
| t.Run("no-op when no service_user_role resources exist", func(t *testing.T) { | ||
| state := &resource.State{ | ||
| Version: resource.StateVersion_1_0_0.Clone(), | ||
| Resources: map[resource.Type]map[string]*resource.ResourceData{ | ||
| mcpConfigType: { | ||
| "mcp-cfg": { | ||
| Identifier: resource.Identifier{ID: "mcp-cfg", Type: mcpConfigType}, | ||
| Dependencies: []resource.Identifier{dirDep}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| migration := &migrations.Version_1_1_0{} | ||
| err := migration.Run(state) | ||
| require.NoError(t, err) | ||
|
|
||
| // mcp_config should be untouched | ||
| mcpCfg := state.Resources[mcpConfigType]["mcp-cfg"] | ||
| require.NotNil(t, mcpCfg) | ||
| assert.Equal(t, []resource.Identifier{dirDep}, mcpCfg.Dependencies) | ||
| }) | ||
|
|
||
| t.Run("empty state", func(t *testing.T) { | ||
| state := &resource.State{ | ||
| Version: resource.StateVersion_1_0_0.Clone(), | ||
| Resources: map[resource.Type]map[string]*resource.ResourceData{}, | ||
| } | ||
|
|
||
| migration := &migrations.Version_1_1_0{} | ||
| err := migration.Run(state) | ||
| require.NoError(t, err) | ||
| }) | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.