Skip to content

Commit

Permalink
fix: Fixed a bug where a subset query was not generated when provided
Browse files Browse the repository at this point in the history
A problems appears after RuntimeContext refactoring

Covered with regression
  • Loading branch information
wwoytenko committed Nov 16, 2024
1 parent 09be53a commit 63dc967
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/overrides/main.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "base.html" %}

{% block announce %}
A new version 0.2.4 (2024.11.16) is <a href="https://github.com/GreenmaskIO/greenmask/releases/tag/v0.2.4">released</a>
A new version 0.2.5 (2024.11.16) is <a href="https://github.com/GreenmaskIO/greenmask/releases/tag/v0.2.5">released</a>
{% endblock %}

{% block outdated %}
Expand Down
21 changes: 21 additions & 0 deletions docs/release_notes/greenmask_0_2_5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Greenmask 0.2.5

This release introduces bug fixes.

## Changes

* Fixed a bug where a subset query was not generated when provided [#247](https://github.com/GreenmaskIO/greenmask/pull/247).


#### Full Changelog: [v0.2.4...v0.2.5](https://github.com/GreenmaskIO/greenmask/compare/v0.2.4...v0.2.5)

## Links

Feel free to reach out to us if you have any questions or need assistance:

* [Greenmask Roadmap](https://github.com/orgs/GreenmaskIO/projects/6)
* [Email](mailto:support@greenmask.io)
* [Twitter](https://twitter.com/GreenmaskIO)
* [Telegram](https://t.me/greenmask_community)
* [Discord](https://discord.gg/tAJegUKSTB)
* [DockerHub](https://hub.docker.com/r/greenmask/greenmask)
28 changes: 16 additions & 12 deletions internal/db/postgres/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,13 @@ func NewRuntimeContext(
vr = nil
}

// Build graph of Tables
graph, err := subset.NewGraph(ctx, tx, slices.Clone(tables), vr)
if err != nil {
return nil, fmt.Errorf("error creating graph: %w", err)
}
if hasSubset(tables) {
// If table has subset the restoration must be in the topological order
// The Tables must be dumped one by one
if err = subset.SetSubsetQueries(graph); err != nil {
return nil, fmt.Errorf("cannot set subset queries: %w", err)
}
debugQueries(tables)
} else {
// if there are no subset Tables, we can sort them by size and transformation costs
// TODO: Implement Tables ordering for subsetted Tables as well
scoreTablesEntriesAndSort(tables)
}

// Validate, build entries config and initialize transformers, set subset conditions to all Tables if provided
buildWarns, err := validateAndBuildEntriesConfig(
ctx, tx, tables, typeMap, cfg, r, version, types, graph,
)
Expand All @@ -121,6 +111,20 @@ func NewRuntimeContext(
}, nil
}

// Set subset queries for Tables if they have subset conditions or sort Tables by size and transformation costs
if hasSubset(tables) {
// If table has subset the restoration must be in the topological order
// The Tables must be dumped one by one
if err = subset.SetSubsetQueries(graph); err != nil {
return nil, fmt.Errorf("cannot set subset queries: %w", err)
}
debugQueries(tables)
} else {
// if there are no subset Tables, we can sort them by size and transformation costs
// TODO: Implement Tables ordering for subsetted Tables as well
scoreTablesEntriesAndSort(tables)
}

var dataSectionObjects []entries.Entry
for _, seq := range sequences {
dataSectionObjects = append(dataSectionObjects, seq)
Expand Down
47 changes: 47 additions & 0 deletions internal/db/postgres/context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,50 @@ func TestNewRuntimeContext_regression_244(t *testing.T) {
}
}
}

func TestNewRuntimeContext_regression_247(t *testing.T) {
// This test is a regression test for https://github.com/GreenmaskIO/greenmask/issues/247
// It validates that subset conditions are correctly applied to the query
ctx := context.Background()
// Start the PostgreSQL container
connStr, cleanup, err := runPostgresContainer(ctx)
require.NoError(t, err)
defer cleanup() // Ensure the container is terminated after the test

con, err := pgx.Connect(ctx, connStr)
require.NoError(t, err)
defer con.Close(ctx) // nolint: errcheck
require.NoError(t, initTables(ctx, con, contextTestDb))
tx, err := con.Begin(ctx)
require.NoError(t, err)
defer tx.Rollback(ctx) // nolint: errcheck
cfg := &domains.Dump{
Transformation: []*domains.Table{
{
Schema: "public",
Name: "users",
SubsetConds: []string{
"public.users.user_id = '62c8c546-2420-4ca6-9961-d2cce26f7cb2'",
},
},
},
}
rc, err := NewRuntimeContext(ctx, tx, cfg, utils.DefaultTransformerRegistry, nil, testContainerPgVersion*10000)
require.NoError(t, err)
require.NotNil(t, rc)
require.False(t, rc.IsFatal())

expectedTablesWithSubsetQuery := map[string]string{
"users": "SELECT \"public\".\"users\".* FROM \"public\".\"users\" WHERE ( ( public.users.user_id = '62c8c546-2420-4ca6-9961-d2cce26f7cb2' ) )",
"orders": "SELECT \"public\".\"orders\".* FROM \"public\".\"orders\" LEFT JOIN \"public\".\"users\" ON \"public\".\"orders\".\"user_id\" = \"public\".\"users\".\"user_id\" AND ( public.users.user_id = '62c8c546-2420-4ca6-9961-d2cce26f7cb2' ) WHERE ( ((\"public\".\"orders\".\"user_id\" IS NULL OR \"public\".\"users\".\"user_id\" IS NOT NULL)) )",
"foo": "",
}

for _, table := range rc.DataSectionObjects {
tab, ok := table.(*entries.Table)
if !ok {
continue
}
assert.Equalf(t, expectedTablesWithSubsetQuery[tab.Name], tab.Query, "Table %s", tab.Name)
}
}
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ nav:
- Faker function: built_in_transformers/advanced_transformers/custom_functions/faker_function.md
- About: index.md
- Release notes:
- Greenmask 0.2.5: release_notes/greenmask_0_2_5.md
- Greenmask 0.2.4: release_notes/greenmask_0_2_4.md
- Greenmask 0.2.3: release_notes/greenmask_0_2_3.md
- Greenmask 0.2.2: release_notes/greenmask_0_2_2.md
Expand Down

0 comments on commit 63dc967

Please sign in to comment.