Skip to content
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

fix: return only the latest namespace entry when fetching tables from warehouse #4287

Merged
merged 8 commits into from
Jan 10, 2024
2 changes: 1 addition & 1 deletion warehouse/internal/repo/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (sh *WHSchema) GetTablesForConnection(ctx context.Context, connections []wa
(source_id, destination_id) IN (` + strings.Join(sourceIDDestinationIDPairs, ", ") + `)
AND
schema::text <> '{}'::text
GROUP BY id
GROUP BY source_id, destination_id
)`
rows, err := sh.db.QueryContext(
ctx,
Expand Down
38 changes: 31 additions & 7 deletions warehouse/internal/repo/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package repo_test
import (
"context"
"errors"
"slices"
"testing"
"time"

Expand Down Expand Up @@ -122,12 +121,14 @@ func TestWHSchemasRepo(t *testing.T) {
connection := warehouseutils.SourceIDDestinationID{SourceID: sourceID, DestinationID: destinationID}
expectedTableNames, err := r.GetTablesForConnection(ctx, []warehouseutils.SourceIDDestinationID{connection})
require.NoError(t, err)
require.Equal(t, len(expectedTableNames), 1)
require.Equal(t, expectedTableNames[0].SourceID, sourceID)
require.Equal(t, expectedTableNames[0].DestinationID, destinationID)
require.Equal(t, expectedTableNames[0].Namespace, namespace)
require.True(t, slices.Contains(expectedTableNames[0].Tables, "table_name_1"))
require.True(t, slices.Contains(expectedTableNames[0].Tables, "table_name_2"))
require.ElementsMatch(t, expectedTableNames, []warehouseutils.FetchTableInfo{
{
SourceID: sourceID,
DestinationID: destinationID,
Namespace: namespace,
Tables: []string{"table_name_1", "table_name_2"},
},
})

t.Log("cancelled context")
_, err = r.GetTablesForConnection(cancelledCtx, []warehouseutils.SourceIDDestinationID{connection})
Expand All @@ -142,5 +143,28 @@ func TestWHSchemasRepo(t *testing.T) {
t.Log("empty")
_, err = r.GetTablesForConnection(ctx, []warehouseutils.SourceIDDestinationID{})
require.EqualError(t, err, errors.New("no source id and destination id pairs provided").Error())

t.Log("multiple")
latestNamespace := "latest_namespace"
schemaLatest := model.WHSchema{
UploadID: 2,
SourceID: sourceID,
Namespace: latestNamespace,
DestinationID: destinationID,
DestinationType: destinationType,
Schema: schemaModel,
CreatedAt: now,
UpdatedAt: now,
}
_, err = r.Insert(ctx, &schemaLatest)
require.NoError(t, err)
expectedTableNames, err = r.GetTablesForConnection(ctx, []warehouseutils.SourceIDDestinationID{connection})
require.NoError(t, err)
require.ElementsMatch(t, expectedTableNames, []warehouseutils.FetchTableInfo{{
SourceID: sourceID,
DestinationID: destinationID,
Namespace: latestNamespace,
Tables: []string{"table_name_1", "table_name_2"},
}}, "the new schema should not have changed the returned tables since we already had a schema for the same source id and destination id pair")
})
}
Loading