Correlations: Add organization id (#72258)

* Add org_id to correlations

* Add tests

* Allow org_id to be null in case org_id=0 is used

* Create organization to ensure stable id is generated

* Fix linting

* Ensure backwards compatibility

* Add deprecation information

* Migrate correlations indices

* Default org_id when migrating

* Remove redundant default

* Make PK non-nullable
This commit is contained in:
Piotr Jamróz
2023-08-24 09:39:30 +02:00
committed by GitHub
parent 9b891480d6
commit b30e0aa5aa
11 changed files with 153 additions and 7 deletions
@@ -28,7 +28,8 @@ var (
withoutDefaults = "testdata/appliedDefaults"
invalidAccess = "testdata/invalid-access"
oneDatasourceWithTwoCorrelations = "testdata/one-datasource-two-correlations"
oneDatasourceWithTwoCorrelations = "testdata/one-datasource-two-correlations"
correlationsDifferentOrganizations = "testdata/correlations-different-organizations"
)
func TestDatasourceAsConfig(t *testing.T) {
@@ -282,6 +283,26 @@ func TestDatasourceAsConfig(t *testing.T) {
require.Equal(t, 1, len(correlationsStore.deletedBySourceUID))
require.Equal(t, 1, len(correlationsStore.deletedByTargetUID))
})
t.Run("Using correct organization id", func(t *testing.T) {
store := &spyStore{items: []*datasources.DataSource{{Name: "Foo", OrgID: 2, ID: 1}}}
orgFake := &orgtest.FakeOrgService{}
correlationsStore := &mockCorrelationsStore{}
dc := newDatasourceProvisioner(logger, store, correlationsStore, orgFake)
err := dc.applyChanges(context.Background(), correlationsDifferentOrganizations)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
}
require.Equal(t, 2, len(correlationsStore.created))
// triggered twice - clean up on delete + update (because of the store setup above)
require.Equal(t, 2, len(correlationsStore.deletedBySourceUID))
require.Equal(t, int64(2), correlationsStore.deletedBySourceUID[0].OrgId)
require.Equal(t, int64(2), correlationsStore.deletedBySourceUID[1].OrgId)
// triggered once - just the clean up
require.Equal(t, 1, len(correlationsStore.deletedByTargetUID))
require.Equal(t, int64(2), correlationsStore.deletedByTargetUID[0].OrgId)
})
})
}