diff --git a/pkg/registry/apis/dashboard/legacy/queries.go b/pkg/registry/apis/dashboard/legacy/queries.go index cc92980b310..af3af94b96e 100644 --- a/pkg/registry/apis/dashboard/legacy/queries.go +++ b/pkg/registry/apis/dashboard/legacy/queries.go @@ -29,7 +29,7 @@ var ( ) type sqlQuery struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface Query *DashboardQuery } diff --git a/pkg/registry/apis/dashboard/legacy/queries_test.go b/pkg/registry/apis/dashboard/legacy/queries_test.go index 4d596bd2090..ca0c276bf95 100644 --- a/pkg/registry/apis/dashboard/legacy/queries_test.go +++ b/pkg/registry/apis/dashboard/legacy/queries_test.go @@ -1,167 +1,69 @@ package legacy import ( - "embed" - "os" - "path/filepath" "testing" "text/template" - "github.com/google/go-cmp/cmp" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate" + "github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate/mocks" ) -//go:embed testdata/* -var testdataFS embed.FS - -func testdata(t *testing.T, filename string) []byte { - t.Helper() - b, err := testdataFS.ReadFile(`testdata/` + filename) - if err != nil { - writeTestData(filename, "") - assert.Fail(t, "missing test file") - } - return b -} - -func writeTestData(filename, value string) { - _ = os.WriteFile(filepath.Join("testdata", filename), []byte(value), 0777) -} - func TestQueries(t *testing.T) { - t.Parallel() - - // Check each dialect - dialects := []sqltemplate.Dialect{ - sqltemplate.MySQL, - sqltemplate.SQLite, - sqltemplate.PostgreSQL, - } - - // Each template has one or more test cases, each identified with a - // descriptive name (e.g. "happy path", "error twiddling the frobb"). Each - // of them will test that for the same input data they must produce a result - // that will depend on the Dialect. Expected queries should be defined in - // separate files in the testdata directory. This improves the testing - // experience by separating test data from test code, since mixing both - // tends to make it more difficult to reason about what is being done, - // especially as we want testing code to scale and make it easy to add - // tests. - type ( - testCase = struct { - Name string - - // Data should be the struct passed to the template. - Data sqltemplate.SQLTemplateIface - } - ) - - // Define tests cases. Most templates are trivial and testing that they - // generate correct code for a single Dialect is fine, since the one thing - // that always changes is how SQL placeholder arguments are passed (most - // Dialects use `?` while PostgreSQL uses `$1`, `$2`, etc.), and that is - // something that should be tested in the Dialect implementation instead of - // here. We will ask to have at least one test per SQL template, and we will - // lean to test MySQL. Templates containing branching (conditionals, loops, - // etc.) should be exercised at least once in each of their branches. - // - // NOTE: in the Data field, make sure to have pointers populated to simulate - // data is set as it would be in a real request. The data being correctly - // populated in each case should be tested in integration tests, where the - // data will actually flow to and from a real database. In this tests we - // only care about producing the correct SQL. - testCases := map[*template.Template][]*testCase{ - sqlQueryDashboards: { - { - Name: "history_uid", - Data: &sqlQuery{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &DashboardQuery{ - OrgID: 2, - UID: "UUU", + mocks.CheckQuerySnapshots(t, mocks.TemplateTestSetup{ + RootDir: "testdata", + Templates: map[*template.Template][]mocks.TemplateTestCase{ + sqlQueryDashboards: { + { + Name: "history_uid", + Data: &sqlQuery{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &DashboardQuery{ + OrgID: 2, + UID: "UUU", + }, }, }, - }, - { - Name: "history_uid_at_version", - Data: &sqlQuery{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &DashboardQuery{ - OrgID: 2, - UID: "UUU", - Version: 3, + { + Name: "history_uid_at_version", + Data: &sqlQuery{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &DashboardQuery{ + OrgID: 2, + UID: "UUU", + Version: 3, + }, }, }, - }, - { - Name: "history_uid_second_page", - Data: &sqlQuery{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &DashboardQuery{ - OrgID: 2, - UID: "UUU", - LastID: 7, + { + Name: "history_uid_second_page", + Data: &sqlQuery{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &DashboardQuery{ + OrgID: 2, + UID: "UUU", + LastID: 7, + }, }, }, - }, - { - Name: "dashboard", - Data: &sqlQuery{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &DashboardQuery{ - OrgID: 2, + { + Name: "dashboard", + Data: &sqlQuery{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &DashboardQuery{ + OrgID: 2, + }, }, }, - }, - { - Name: "dashboard_next_page", - Data: &sqlQuery{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &DashboardQuery{ - OrgID: 2, - LastID: 22, + { + Name: "dashboard_next_page", + Data: &sqlQuery{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &DashboardQuery{ + OrgID: 2, + LastID: 22, + }, }, }, }, }, - } - - // Execute test cases - for tmpl, tcs := range testCases { - t.Run(tmpl.Name(), func(t *testing.T) { - t.Parallel() - - for _, tc := range tcs { - t.Run(tc.Name, func(t *testing.T) { - t.Parallel() - - for _, dialect := range dialects { - filename := dialect.DialectName() + "__" + tc.Name + ".sql" - t.Run(filename, func(t *testing.T) { - // not parallel because we're sharing tc.Data, not - // worth it deep cloning - - expectedQuery := string(testdata(t, filename)) - //expectedQuery := sqltemplate.FormatSQL(rawQuery) - - tc.Data.SetDialect(dialect) - err := tc.Data.Validate() - require.NoError(t, err) - got, err := sqltemplate.Execute(tmpl, tc.Data) - require.NoError(t, err) - - got = sqltemplate.RemoveEmptyLines(got) - if diff := cmp.Diff(expectedQuery, got); diff != "" { - writeTestData(filename, got) - t.Errorf("%s: %s", tc.Name, diff) - } - }) - } - }) - } - }) - } + }) } diff --git a/pkg/registry/apis/dashboard/legacy/sql_dashboards.go b/pkg/registry/apis/dashboard/legacy/sql_dashboards.go index 93e6bbd401a..499930de6f7 100644 --- a/pkg/registry/apis/dashboard/legacy/sql_dashboards.go +++ b/pkg/registry/apis/dashboard/legacy/sql_dashboards.go @@ -92,8 +92,8 @@ func (a *dashboardSqlAccess) getRows(ctx context.Context, query *DashboardQuery) } req := sqlQuery{ - SQLTemplate: sqltemplate.New(a.dialect), - Query: query, + SQLTemplateIface: sqltemplate.New(a.dialect), + Query: query, } tmpl := sqlQueryDashboards diff --git a/pkg/registry/apis/dashboard/legacy/testdata/mysql__dashboard.sql b/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-dashboard.sql similarity index 96% rename from pkg/registry/apis/dashboard/legacy/testdata/mysql__dashboard.sql rename to pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-dashboard.sql index 20109d629c2..c5da8ec1fea 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/mysql__dashboard.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-dashboard.sql @@ -14,5 +14,5 @@ SELECT LEFT OUTER JOIN `user` AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN `user` AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = ? + AND dashboard.org_id = 2 ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/mysql__dashboard_next_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-dashboard_next_page.sql similarity index 94% rename from pkg/registry/apis/dashboard/legacy/testdata/mysql__dashboard_next_page.sql rename to pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-dashboard_next_page.sql index b6ca893dd04..0fc8146965f 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/mysql__dashboard_next_page.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-dashboard_next_page.sql @@ -14,7 +14,7 @@ SELECT LEFT OUTER JOIN `user` AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN `user` AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = ? - AND dashboard.id > ? + AND dashboard.org_id = 2 + AND dashboard.id > 22 AND dashboard.deleted IS NULL ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-history_uid.sql b/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-history_uid.sql new file mode 100755 index 00000000000..c2385d49c1d --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-history_uid.sql @@ -0,0 +1,19 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN `user` AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN `user` AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/mysql__history_uid_at_version.sql b/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-history_uid_at_version.sql similarity index 93% rename from pkg/registry/apis/dashboard/legacy/testdata/mysql__history_uid_at_version.sql rename to pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-history_uid_at_version.sql index 628913ed3fc..93be3353bc8 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/mysql__history_uid_at_version.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-history_uid_at_version.sql @@ -15,6 +15,6 @@ SELECT LEFT OUTER JOIN `user` AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN `user` AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = ? - AND dashboard_version.version = ? + AND dashboard.org_id = 2 + AND dashboard_version.version = 3 ORDER BY dashboard_version.version DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-history_uid_second_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-history_uid_second_page.sql new file mode 100755 index 00000000000..dcdfa47e3f7 --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/mysql--query_dashboards-history_uid_second_page.sql @@ -0,0 +1,20 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN `user` AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN `user` AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + AND dashboard.deleted IS NULL + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/mysql__history_uid.sql b/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-dashboard.sql similarity index 93% rename from pkg/registry/apis/dashboard/legacy/testdata/mysql__history_uid.sql rename to pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-dashboard.sql index f78abfda947..c5da8ec1fea 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/mysql__history_uid.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-dashboard.sql @@ -14,6 +14,5 @@ SELECT LEFT OUTER JOIN `user` AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN `user` AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = ? - AND dashboard.uid = ? + AND dashboard.org_id = 2 ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/mysql__history_uid_second_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-dashboard_next_page.sql similarity index 94% rename from pkg/registry/apis/dashboard/legacy/testdata/mysql__history_uid_second_page.sql rename to pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-dashboard_next_page.sql index 240e937dead..0fc8146965f 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/mysql__history_uid_second_page.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-dashboard_next_page.sql @@ -14,7 +14,7 @@ SELECT LEFT OUTER JOIN `user` AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN `user` AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = ? - AND dashboard.uid = ? + AND dashboard.org_id = 2 + AND dashboard.id > 22 AND dashboard.deleted IS NULL ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-history_uid.sql b/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-history_uid.sql new file mode 100755 index 00000000000..c2385d49c1d --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-history_uid.sql @@ -0,0 +1,19 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN `user` AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN `user` AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-history_uid_at_version.sql b/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-history_uid_at_version.sql new file mode 100755 index 00000000000..93be3353bc8 --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-history_uid_at_version.sql @@ -0,0 +1,20 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard_version.created, updated_user.uid as updated_by,updated_user.id as created_by_id, + dashboard_version.version, dashboard_version.message, dashboard_version.data + FROM dashboard + LEFT OUTER JOIN dashboard_version ON dashboard.id = dashboard_version.dashboard_id + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN `user` AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN `user` AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard_version.version = 3 + ORDER BY dashboard_version.version DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-history_uid_second_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-history_uid_second_page.sql new file mode 100755 index 00000000000..dcdfa47e3f7 --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/mysql-query_dashboards-history_uid_second_page.sql @@ -0,0 +1,20 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN `user` AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN `user` AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + AND dashboard.deleted IS NULL + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/sqlite__dashboard.sql b/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-dashboard.sql similarity index 96% rename from pkg/registry/apis/dashboard/legacy/testdata/sqlite__dashboard.sql rename to pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-dashboard.sql index 764fbb0d5e2..64a659963b8 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/sqlite__dashboard.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-dashboard.sql @@ -14,5 +14,5 @@ SELECT LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = ? + AND dashboard.org_id = 2 ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/sqlite__dashboard_next_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-dashboard_next_page.sql similarity index 94% rename from pkg/registry/apis/dashboard/legacy/testdata/sqlite__dashboard_next_page.sql rename to pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-dashboard_next_page.sql index 9ad7aeecb70..cda07bd2fd5 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/sqlite__dashboard_next_page.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-dashboard_next_page.sql @@ -14,7 +14,7 @@ SELECT LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = ? - AND dashboard.id > ? + AND dashboard.org_id = 2 + AND dashboard.id > 22 AND dashboard.deleted IS NULL ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-history_uid.sql b/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-history_uid.sql new file mode 100755 index 00000000000..71f0756bb3f --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-history_uid.sql @@ -0,0 +1,19 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/sqlite__history_uid_at_version.sql b/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-history_uid_at_version.sql similarity index 93% rename from pkg/registry/apis/dashboard/legacy/testdata/sqlite__history_uid_at_version.sql rename to pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-history_uid_at_version.sql index 5bf79fbcec0..002d1e9fdae 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/sqlite__history_uid_at_version.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-history_uid_at_version.sql @@ -15,6 +15,6 @@ SELECT LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = ? - AND dashboard_version.version = ? + AND dashboard.org_id = 2 + AND dashboard_version.version = 3 ORDER BY dashboard_version.version DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-history_uid_second_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-history_uid_second_page.sql new file mode 100755 index 00000000000..f28d6fac178 --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/postgres--query_dashboards-history_uid_second_page.sql @@ -0,0 +1,20 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + AND dashboard.deleted IS NULL + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/postgres__dashboard.sql b/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-dashboard.sql similarity index 96% rename from pkg/registry/apis/dashboard/legacy/testdata/postgres__dashboard.sql rename to pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-dashboard.sql index c92b21bb165..64a659963b8 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/postgres__dashboard.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-dashboard.sql @@ -14,5 +14,5 @@ SELECT LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = $1 + AND dashboard.org_id = 2 ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/sqlite__history_uid_second_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-dashboard_next_page.sql similarity index 94% rename from pkg/registry/apis/dashboard/legacy/testdata/sqlite__history_uid_second_page.sql rename to pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-dashboard_next_page.sql index 5d28ea3f625..cda07bd2fd5 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/sqlite__history_uid_second_page.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-dashboard_next_page.sql @@ -14,7 +14,7 @@ SELECT LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = ? - AND dashboard.uid = ? + AND dashboard.org_id = 2 + AND dashboard.id > 22 AND dashboard.deleted IS NULL ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-history_uid.sql b/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-history_uid.sql new file mode 100755 index 00000000000..71f0756bb3f --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-history_uid.sql @@ -0,0 +1,19 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/postgres__history_uid_at_version.sql b/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-history_uid_at_version.sql similarity index 93% rename from pkg/registry/apis/dashboard/legacy/testdata/postgres__history_uid_at_version.sql rename to pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-history_uid_at_version.sql index ed5baa52705..002d1e9fdae 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/postgres__history_uid_at_version.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-history_uid_at_version.sql @@ -15,6 +15,6 @@ SELECT LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = $1 - AND dashboard_version.version = $2 + AND dashboard.org_id = 2 + AND dashboard_version.version = 3 ORDER BY dashboard_version.version DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-history_uid_second_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-history_uid_second_page.sql new file mode 100755 index 00000000000..f28d6fac178 --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/postgres-query_dashboards-history_uid_second_page.sql @@ -0,0 +1,20 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + AND dashboard.deleted IS NULL + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/sqlite__history_uid.sql b/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-dashboard.sql similarity index 93% rename from pkg/registry/apis/dashboard/legacy/testdata/sqlite__history_uid.sql rename to pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-dashboard.sql index 34135c79e17..64a659963b8 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/sqlite__history_uid.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-dashboard.sql @@ -14,6 +14,5 @@ SELECT LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = ? - AND dashboard.uid = ? + AND dashboard.org_id = 2 ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/postgres__dashboard_next_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-dashboard_next_page.sql similarity index 94% rename from pkg/registry/apis/dashboard/legacy/testdata/postgres__dashboard_next_page.sql rename to pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-dashboard_next_page.sql index 437a4c4bfbb..cda07bd2fd5 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/postgres__dashboard_next_page.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-dashboard_next_page.sql @@ -14,7 +14,7 @@ SELECT LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = $1 - AND dashboard.id > $2 + AND dashboard.org_id = 2 + AND dashboard.id > 22 AND dashboard.deleted IS NULL ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-history_uid.sql b/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-history_uid.sql new file mode 100755 index 00000000000..71f0756bb3f --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-history_uid.sql @@ -0,0 +1,19 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-history_uid_at_version.sql b/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-history_uid_at_version.sql new file mode 100755 index 00000000000..002d1e9fdae --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-history_uid_at_version.sql @@ -0,0 +1,20 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard_version.created, updated_user.uid as updated_by,updated_user.id as created_by_id, + dashboard_version.version, dashboard_version.message, dashboard_version.data + FROM dashboard + LEFT OUTER JOIN dashboard_version ON dashboard.id = dashboard_version.dashboard_id + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard_version.version = 3 + ORDER BY dashboard_version.version DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-history_uid_second_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-history_uid_second_page.sql new file mode 100755 index 00000000000..f28d6fac178 --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/sqlite--query_dashboards-history_uid_second_page.sql @@ -0,0 +1,20 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + AND dashboard.deleted IS NULL + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/postgres__history_uid.sql b/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-dashboard.sql similarity index 93% rename from pkg/registry/apis/dashboard/legacy/testdata/postgres__history_uid.sql rename to pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-dashboard.sql index db6f9b3a29a..64a659963b8 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/postgres__history_uid.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-dashboard.sql @@ -14,6 +14,5 @@ SELECT LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = $1 - AND dashboard.uid = $2 + AND dashboard.org_id = 2 ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/postgres__history_uid_second_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-dashboard_next_page.sql similarity index 93% rename from pkg/registry/apis/dashboard/legacy/testdata/postgres__history_uid_second_page.sql rename to pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-dashboard_next_page.sql index 692a44fcf67..cda07bd2fd5 100755 --- a/pkg/registry/apis/dashboard/legacy/testdata/postgres__history_uid_second_page.sql +++ b/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-dashboard_next_page.sql @@ -14,7 +14,7 @@ SELECT LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id WHERE dashboard.is_folder = false - AND dashboard.org_id = $1 - AND dashboard.uid = $2 + AND dashboard.org_id = 2 + AND dashboard.id > 22 AND dashboard.deleted IS NULL ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-history_uid.sql b/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-history_uid.sql new file mode 100755 index 00000000000..71f0756bb3f --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-history_uid.sql @@ -0,0 +1,19 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-history_uid_at_version.sql b/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-history_uid_at_version.sql new file mode 100755 index 00000000000..002d1e9fdae --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-history_uid_at_version.sql @@ -0,0 +1,20 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard_version.created, updated_user.uid as updated_by,updated_user.id as created_by_id, + dashboard_version.version, dashboard_version.message, dashboard_version.data + FROM dashboard + LEFT OUTER JOIN dashboard_version ON dashboard.id = dashboard_version.dashboard_id + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard_version.version = 3 + ORDER BY dashboard_version.version DESC diff --git a/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-history_uid_second_page.sql b/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-history_uid_second_page.sql new file mode 100755 index 00000000000..f28d6fac178 --- /dev/null +++ b/pkg/registry/apis/dashboard/legacy/testdata/sqlite-query_dashboards-history_uid_second_page.sql @@ -0,0 +1,20 @@ +SELECT + dashboard.org_id, dashboard.id, + dashboard.uid, dashboard.folder_uid, + dashboard.deleted, plugin_id, + dashboard_provisioning.name as origin_name, + dashboard_provisioning.external_id as origin_path, + dashboard_provisioning.check_sum as origin_key, + dashboard_provisioning.updated as origin_ts, + dashboard.created, created_user.uid as created_by, dashboard.created_by as created_by_id, + dashboard.updated, updated_user.uid as updated_by, dashboard.updated_by as updated_by_id, + dashboard.version, '' as message, dashboard.data + FROM dashboard + LEFT OUTER JOIN dashboard_provisioning ON dashboard.id = dashboard_provisioning.dashboard_id + LEFT OUTER JOIN "user" AS created_user ON dashboard.created_by = created_user.id + LEFT OUTER JOIN "user" AS updated_user ON dashboard.updated_by = updated_user.id + WHERE dashboard.is_folder = false + AND dashboard.org_id = 2 + AND dashboard.uid = 'UUU' + AND dashboard.deleted IS NULL + ORDER BY dashboard.id DESC diff --git a/pkg/registry/apis/identity/legacy/legacy_sql.go b/pkg/registry/apis/identity/legacy/legacy_sql.go index 65c66172bfa..91cce7d61f0 100644 --- a/pkg/registry/apis/identity/legacy/legacy_sql.go +++ b/pkg/registry/apis/identity/legacy/legacy_sql.go @@ -55,8 +55,8 @@ func (s *legacySQLStore) ListTeams(ctx context.Context, ns claims.NamespaceInfo, } req := sqlQueryListTeams{ - SQLTemplate: sqltemplate.New(s.dialect), - Query: &query, + SQLTemplateIface: sqltemplate.New(s.dialect), + Query: &query, } rawQuery, err := sqltemplate.Execute(sqlQueryTeams, req) @@ -117,8 +117,8 @@ func (s *legacySQLStore) ListUsers(ctx context.Context, ns claims.NamespaceInfo, } return s.queryUsers(ctx, sqlQueryUsers, sqlQueryListUsers{ - SQLTemplate: sqltemplate.New(s.dialect), - Query: &query, + SQLTemplateIface: sqltemplate.New(s.dialect), + Query: &query, }, limit, query.UID != "") } @@ -180,7 +180,7 @@ func (s *legacySQLStore) GetDisplay(ctx context.Context, ns claims.NamespaceInfo } return s.queryUsers(ctx, sqlQueryDisplay, sqlQueryGetDisplay{ - SQLTemplate: sqltemplate.New(s.dialect), - Query: &query, + SQLTemplateIface: sqltemplate.New(s.dialect), + Query: &query, }, 10000, false) } diff --git a/pkg/registry/apis/identity/legacy/queries.go b/pkg/registry/apis/identity/legacy/queries.go index be9d88cdc9b..55bdd20e32c 100644 --- a/pkg/registry/apis/identity/legacy/queries.go +++ b/pkg/registry/apis/identity/legacy/queries.go @@ -31,7 +31,7 @@ var ( ) type sqlQueryListUsers struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface Query *ListUserQuery } @@ -40,7 +40,7 @@ func (r sqlQueryListUsers) Validate() error { } type sqlQueryListTeams struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface Query *ListTeamQuery } @@ -49,7 +49,7 @@ func (r sqlQueryListTeams) Validate() error { } type sqlQueryGetDisplay struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface Query *GetUserDisplayQuery } diff --git a/pkg/registry/apis/identity/legacy/queries_test.go b/pkg/registry/apis/identity/legacy/queries_test.go index 0e9dfabc167..e49e36082f2 100644 --- a/pkg/registry/apis/identity/legacy/queries_test.go +++ b/pkg/registry/apis/identity/legacy/queries_test.go @@ -1,207 +1,109 @@ package legacy import ( - "embed" - "os" - "path/filepath" "testing" "text/template" - "github.com/google/go-cmp/cmp" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate" + "github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate/mocks" ) -//go:embed testdata/* -var testdataFS embed.FS - -func testdata(t *testing.T, filename string) []byte { - t.Helper() - b, err := testdataFS.ReadFile(`testdata/` + filename) - if err != nil { - writeTestData(filename, "") - assert.Fail(t, "missing test file") - } - return b -} - -func writeTestData(filename, value string) { - _ = os.WriteFile(filepath.Join("testdata", filename), []byte(value), 0777) -} - func TestQueries(t *testing.T) { - t.Parallel() - - // Check each dialect - dialects := []sqltemplate.Dialect{ - sqltemplate.MySQL, - sqltemplate.SQLite, - sqltemplate.PostgreSQL, - } - - // Each template has one or more test cases, each identified with a - // descriptive name (e.g. "happy path", "error twiddling the frobb"). Each - // of them will test that for the same input data they must produce a result - // that will depend on the Dialect. Expected queries should be defined in - // separate files in the testdata directory. This improves the testing - // experience by separating test data from test code, since mixing both - // tends to make it more difficult to reason about what is being done, - // especially as we want testing code to scale and make it easy to add - // tests. - type ( - testCase = struct { - Name string - - // Data should be the struct passed to the template. - Data sqltemplate.SQLTemplateIface - } - ) - - // Define tests cases. Most templates are trivial and testing that they - // generate correct code for a single Dialect is fine, since the one thing - // that always changes is how SQL placeholder arguments are passed (most - // Dialects use `?` while PostgreSQL uses `$1`, `$2`, etc.), and that is - // something that should be tested in the Dialect implementation instead of - // here. We will ask to have at least one test per SQL template, and we will - // lean to test MySQL. Templates containing branching (conditionals, loops, - // etc.) should be exercised at least once in each of their branches. - // - // NOTE: in the Data field, make sure to have pointers populated to simulate - // data is set as it would be in a real request. The data being correctly - // populated in each case should be tested in integration tests, where the - // data will actually flow to and from a real database. In this tests we - // only care about producing the correct SQL. - testCases := map[*template.Template][]*testCase{ - sqlQueryTeams: { - { - Name: "teams_uid", - Data: &sqlQueryListTeams{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &ListTeamQuery{ - UID: "abc", + mocks.CheckQuerySnapshots(t, mocks.TemplateTestSetup{ + RootDir: "testdata", + Templates: map[*template.Template][]mocks.TemplateTestCase{ + sqlQueryTeams: { + { + Name: "teams_uid", + Data: &sqlQueryListTeams{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &ListTeamQuery{ + UID: "abc", + }, + }, + }, + { + Name: "teams_page_1", + Data: &sqlQueryListTeams{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &ListTeamQuery{ + Limit: 5, + }, + }, + }, + { + Name: "teams_page_2", + Data: &sqlQueryListTeams{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &ListTeamQuery{ + ContinueID: 1, + Limit: 2, + }, }, }, }, - { - Name: "teams_page_1", - Data: &sqlQueryListTeams{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &ListTeamQuery{ - Limit: 5, + sqlQueryUsers: { + { + Name: "users_uid", + Data: &sqlQueryListUsers{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &ListUserQuery{ + UID: "abc", + }, + }, + }, + { + Name: "users_page_1", + Data: &sqlQueryListUsers{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &ListUserQuery{ + Limit: 5, + }, + }, + }, + { + Name: "users_page_2", + Data: &sqlQueryListUsers{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &ListUserQuery{ + ContinueID: 1, + Limit: 2, + }, }, }, }, - { - Name: "teams_page_2", - Data: &sqlQueryListTeams{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &ListTeamQuery{ - ContinueID: 1, - Limit: 2, + sqlQueryDisplay: { + { + Name: "display_uids", + Data: &sqlQueryGetDisplay{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &GetUserDisplayQuery{ + OrgID: 2, + UIDs: []string{"a", "b"}, + }, + }, + }, + { + Name: "display_ids", + Data: &sqlQueryGetDisplay{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &GetUserDisplayQuery{ + OrgID: 2, + IDs: []int64{1, 2}, + }, + }, + }, + { + Name: "display_ids_uids", + Data: &sqlQueryGetDisplay{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Query: &GetUserDisplayQuery{ + OrgID: 2, + UIDs: []string{"a", "b"}, + IDs: []int64{1, 2}, + }, }, }, }, }, - sqlQueryUsers: { - { - Name: "users_uid", - Data: &sqlQueryListUsers{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &ListUserQuery{ - UID: "abc", - }, - }, - }, - { - Name: "users_page_1", - Data: &sqlQueryListUsers{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &ListUserQuery{ - Limit: 5, - }, - }, - }, - { - Name: "users_page_2", - Data: &sqlQueryListUsers{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &ListUserQuery{ - ContinueID: 1, - Limit: 2, - }, - }, - }, - }, - sqlQueryDisplay: { - { - Name: "display_uids", - Data: &sqlQueryGetDisplay{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &GetUserDisplayQuery{ - OrgID: 2, - UIDs: []string{"a", "b"}, - }, - }, - }, - { - Name: "display_ids", - Data: &sqlQueryGetDisplay{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &GetUserDisplayQuery{ - OrgID: 2, - IDs: []int64{1, 2}, - }, - }, - }, - { - Name: "display_ids_uids", - Data: &sqlQueryGetDisplay{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Query: &GetUserDisplayQuery{ - OrgID: 2, - UIDs: []string{"a", "b"}, - IDs: []int64{1, 2}, - }, - }, - }, - }, - } - - // Execute test cases - for tmpl, tcs := range testCases { - t.Run(tmpl.Name(), func(t *testing.T) { - t.Parallel() - - for _, tc := range tcs { - t.Run(tc.Name, func(t *testing.T) { - t.Parallel() - - for _, dialect := range dialects { - filename := dialect.DialectName() + "__" + tc.Name + ".sql" - t.Run(filename, func(t *testing.T) { - // not parallel because we're sharing tc.Data, not - // worth it deep cloning - - expectedQuery := string(testdata(t, filename)) - //expectedQuery := sqltemplate.FormatSQL(rawQuery) - - tc.Data.SetDialect(dialect) - err := tc.Data.Validate() - require.NoError(t, err) - got, err := sqltemplate.Execute(tmpl, tc.Data) - require.NoError(t, err) - - got = sqltemplate.RemoveEmptyLines(got) - if diff := cmp.Diff(expectedQuery, got); diff != "" { - writeTestData(filename, got) - t.Errorf("%s: %s", tc.Name, diff) - } - }) - } - }) - } - }) - } + }) } diff --git a/pkg/registry/apis/identity/legacy/testdata/mysql__display_ids.sql b/pkg/registry/apis/identity/legacy/testdata/mysql--query_display-display_ids.sql similarity index 79% rename from pkg/registry/apis/identity/legacy/testdata/mysql__display_ids.sql rename to pkg/registry/apis/identity/legacy/testdata/mysql--query_display-display_ids.sql index bff3eddd1f9..d82de71a1df 100755 --- a/pkg/registry/apis/identity/legacy/testdata/mysql__display_ids.sql +++ b/pkg/registry/apis/identity/legacy/testdata/mysql--query_display-display_ids.sql @@ -1,8 +1,8 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM `user` as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? AND ( 1=2 - OR u.id IN (?, ?) + WHERE org_user.org_id = 2 AND ( 1=2 + OR u.id IN (1, 2) ) ORDER BY u.id asc LIMIT 500 diff --git a/pkg/registry/apis/identity/legacy/testdata/mysql__display_ids_uids.sql b/pkg/registry/apis/identity/legacy/testdata/mysql--query_display-display_ids_uids.sql similarity index 73% rename from pkg/registry/apis/identity/legacy/testdata/mysql__display_ids_uids.sql rename to pkg/registry/apis/identity/legacy/testdata/mysql--query_display-display_ids_uids.sql index ab8d701ba34..7dfe9de371c 100755 --- a/pkg/registry/apis/identity/legacy/testdata/mysql__display_ids_uids.sql +++ b/pkg/registry/apis/identity/legacy/testdata/mysql--query_display-display_ids_uids.sql @@ -1,9 +1,9 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM `user` as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? AND ( 1=2 - OR uid IN (?, ?) - OR u.id IN (?, ?) + WHERE org_user.org_id = 2 AND ( 1=2 + OR uid IN ('a', 'b') + OR u.id IN (1, 2) ) ORDER BY u.id asc LIMIT 500 diff --git a/pkg/registry/apis/identity/legacy/testdata/mysql__display_uids.sql b/pkg/registry/apis/identity/legacy/testdata/mysql--query_display-display_uids.sql similarity index 78% rename from pkg/registry/apis/identity/legacy/testdata/mysql__display_uids.sql rename to pkg/registry/apis/identity/legacy/testdata/mysql--query_display-display_uids.sql index 10786ade099..fd1d8da6a9f 100755 --- a/pkg/registry/apis/identity/legacy/testdata/mysql__display_uids.sql +++ b/pkg/registry/apis/identity/legacy/testdata/mysql--query_display-display_uids.sql @@ -1,8 +1,8 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM `user` as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? AND ( 1=2 - OR uid IN (?, ?) + WHERE org_user.org_id = 2 AND ( 1=2 + OR uid IN ('a', 'b') ) ORDER BY u.id asc LIMIT 500 diff --git a/pkg/registry/apis/identity/legacy/testdata/mysql__teams_page_1.sql b/pkg/registry/apis/identity/legacy/testdata/mysql--query_teams-teams_page_1.sql similarity index 74% rename from pkg/registry/apis/identity/legacy/testdata/mysql__teams_page_1.sql rename to pkg/registry/apis/identity/legacy/testdata/mysql--query_teams-teams_page_1.sql index 56ac17f8539..d216e8bbead 100755 --- a/pkg/registry/apis/identity/legacy/testdata/mysql__teams_page_1.sql +++ b/pkg/registry/apis/identity/legacy/testdata/mysql--query_teams-teams_page_1.sql @@ -1,5 +1,5 @@ SELECT id, uid, name, email, created, updated FROM "team" - WHERE org_id = ? + WHERE org_id = 0 ORDER BY id asc - LIMIT ? + LIMIT 5 diff --git a/pkg/registry/apis/identity/legacy/testdata/mysql__teams_page_2.sql b/pkg/registry/apis/identity/legacy/testdata/mysql--query_teams-teams_page_2.sql similarity index 65% rename from pkg/registry/apis/identity/legacy/testdata/mysql__teams_page_2.sql rename to pkg/registry/apis/identity/legacy/testdata/mysql--query_teams-teams_page_2.sql index d520548b8aa..cb147eeb524 100755 --- a/pkg/registry/apis/identity/legacy/testdata/mysql__teams_page_2.sql +++ b/pkg/registry/apis/identity/legacy/testdata/mysql--query_teams-teams_page_2.sql @@ -1,6 +1,6 @@ SELECT id, uid, name, email, created, updated FROM "team" - WHERE org_id = ? - AND id > ? + WHERE org_id = 0 + AND id > 1 ORDER BY id asc - LIMIT ? + LIMIT 2 diff --git a/pkg/registry/apis/identity/legacy/testdata/mysql__teams_uid.sql b/pkg/registry/apis/identity/legacy/testdata/mysql--query_teams-teams_uid.sql similarity index 62% rename from pkg/registry/apis/identity/legacy/testdata/mysql__teams_uid.sql rename to pkg/registry/apis/identity/legacy/testdata/mysql--query_teams-teams_uid.sql index 2e88bccd126..49cb5588bef 100755 --- a/pkg/registry/apis/identity/legacy/testdata/mysql__teams_uid.sql +++ b/pkg/registry/apis/identity/legacy/testdata/mysql--query_teams-teams_uid.sql @@ -1,6 +1,6 @@ SELECT id, uid, name, email, created, updated FROM "team" - WHERE org_id = ? - AND uid = ? + WHERE org_id = 0 + AND uid = 'abc' ORDER BY id asc - LIMIT ? + LIMIT 0 diff --git a/pkg/registry/apis/identity/legacy/testdata/mysql__users_page_1.sql b/pkg/registry/apis/identity/legacy/testdata/mysql--query_users-users_page_1.sql similarity index 74% rename from pkg/registry/apis/identity/legacy/testdata/mysql__users_page_1.sql rename to pkg/registry/apis/identity/legacy/testdata/mysql--query_users-users_page_1.sql index e93a3522534..e09464e6e8b 100755 --- a/pkg/registry/apis/identity/legacy/testdata/mysql__users_page_1.sql +++ b/pkg/registry/apis/identity/legacy/testdata/mysql--query_users-users_page_1.sql @@ -1,7 +1,7 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM `user` as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? - AND u.is_service_account = ? + WHERE org_user.org_id = 0 + AND u.is_service_account = FALSE ORDER BY u.id asc - LIMIT ? + LIMIT 5 diff --git a/pkg/registry/apis/identity/legacy/testdata/mysql__users_page_2.sql b/pkg/registry/apis/identity/legacy/testdata/mysql--query_users-users_page_2.sql similarity index 71% rename from pkg/registry/apis/identity/legacy/testdata/mysql__users_page_2.sql rename to pkg/registry/apis/identity/legacy/testdata/mysql--query_users-users_page_2.sql index 1eed7fec311..44e68ce09a1 100755 --- a/pkg/registry/apis/identity/legacy/testdata/mysql__users_page_2.sql +++ b/pkg/registry/apis/identity/legacy/testdata/mysql--query_users-users_page_2.sql @@ -1,8 +1,8 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM `user` as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? - AND u.is_service_account = ? - AND id > ? + WHERE org_user.org_id = 0 + AND u.is_service_account = FALSE + AND id > 1 ORDER BY u.id asc - LIMIT ? + LIMIT 2 diff --git a/pkg/registry/apis/identity/legacy/testdata/mysql--query_users-users_uid.sql b/pkg/registry/apis/identity/legacy/testdata/mysql--query_users-users_uid.sql new file mode 100755 index 00000000000..ddc7286907d --- /dev/null +++ b/pkg/registry/apis/identity/legacy/testdata/mysql--query_users-users_uid.sql @@ -0,0 +1,8 @@ +SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, + u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin + FROM `user` as u JOIN org_user ON u.id = org_user.user_id + WHERE org_user.org_id = 0 + AND u.is_service_account = FALSE + AND uid = 'abc' + ORDER BY u.id asc + LIMIT 0 diff --git a/pkg/registry/apis/identity/legacy/testdata/mysql__users_uid.sql b/pkg/registry/apis/identity/legacy/testdata/mysql__users_uid.sql deleted file mode 100755 index 1d3b1c62fcb..00000000000 --- a/pkg/registry/apis/identity/legacy/testdata/mysql__users_uid.sql +++ /dev/null @@ -1,8 +0,0 @@ -SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, - u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin - FROM `user` as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? - AND u.is_service_account = ? - AND uid = ? - ORDER BY u.id asc - LIMIT ? diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite__display_ids.sql b/pkg/registry/apis/identity/legacy/testdata/postgres--query_display-display_ids.sql similarity index 79% rename from pkg/registry/apis/identity/legacy/testdata/sqlite__display_ids.sql rename to pkg/registry/apis/identity/legacy/testdata/postgres--query_display-display_ids.sql index 70280aca234..1b5d33427cd 100755 --- a/pkg/registry/apis/identity/legacy/testdata/sqlite__display_ids.sql +++ b/pkg/registry/apis/identity/legacy/testdata/postgres--query_display-display_ids.sql @@ -1,8 +1,8 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? AND ( 1=2 - OR u.id IN (?, ?) + WHERE org_user.org_id = 2 AND ( 1=2 + OR u.id IN (1, 2) ) ORDER BY u.id asc LIMIT 500 diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite__display_ids_uids.sql b/pkg/registry/apis/identity/legacy/testdata/postgres--query_display-display_ids_uids.sql similarity index 73% rename from pkg/registry/apis/identity/legacy/testdata/sqlite__display_ids_uids.sql rename to pkg/registry/apis/identity/legacy/testdata/postgres--query_display-display_ids_uids.sql index 18ea9b0d4ef..20b3ce0a36a 100755 --- a/pkg/registry/apis/identity/legacy/testdata/sqlite__display_ids_uids.sql +++ b/pkg/registry/apis/identity/legacy/testdata/postgres--query_display-display_ids_uids.sql @@ -1,9 +1,9 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? AND ( 1=2 - OR uid IN (?, ?) - OR u.id IN (?, ?) + WHERE org_user.org_id = 2 AND ( 1=2 + OR uid IN ('a', 'b') + OR u.id IN (1, 2) ) ORDER BY u.id asc LIMIT 500 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres__display_uids.sql b/pkg/registry/apis/identity/legacy/testdata/postgres--query_display-display_uids.sql similarity index 78% rename from pkg/registry/apis/identity/legacy/testdata/postgres__display_uids.sql rename to pkg/registry/apis/identity/legacy/testdata/postgres--query_display-display_uids.sql index 487b6591c76..70832ccd97e 100755 --- a/pkg/registry/apis/identity/legacy/testdata/postgres__display_uids.sql +++ b/pkg/registry/apis/identity/legacy/testdata/postgres--query_display-display_uids.sql @@ -1,8 +1,8 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = $1 AND ( 1=2 - OR uid IN ($2, $3) + WHERE org_user.org_id = 2 AND ( 1=2 + OR uid IN ('a', 'b') ) ORDER BY u.id asc LIMIT 500 diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite__teams_page_1.sql b/pkg/registry/apis/identity/legacy/testdata/postgres--query_teams-teams_page_1.sql similarity index 74% rename from pkg/registry/apis/identity/legacy/testdata/sqlite__teams_page_1.sql rename to pkg/registry/apis/identity/legacy/testdata/postgres--query_teams-teams_page_1.sql index 56ac17f8539..d216e8bbead 100755 --- a/pkg/registry/apis/identity/legacy/testdata/sqlite__teams_page_1.sql +++ b/pkg/registry/apis/identity/legacy/testdata/postgres--query_teams-teams_page_1.sql @@ -1,5 +1,5 @@ SELECT id, uid, name, email, created, updated FROM "team" - WHERE org_id = ? + WHERE org_id = 0 ORDER BY id asc - LIMIT ? + LIMIT 5 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres--query_teams-teams_page_2.sql b/pkg/registry/apis/identity/legacy/testdata/postgres--query_teams-teams_page_2.sql new file mode 100755 index 00000000000..cb147eeb524 --- /dev/null +++ b/pkg/registry/apis/identity/legacy/testdata/postgres--query_teams-teams_page_2.sql @@ -0,0 +1,6 @@ +SELECT id, uid, name, email, created, updated + FROM "team" + WHERE org_id = 0 + AND id > 1 + ORDER BY id asc + LIMIT 2 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres--query_teams-teams_uid.sql b/pkg/registry/apis/identity/legacy/testdata/postgres--query_teams-teams_uid.sql new file mode 100755 index 00000000000..49cb5588bef --- /dev/null +++ b/pkg/registry/apis/identity/legacy/testdata/postgres--query_teams-teams_uid.sql @@ -0,0 +1,6 @@ +SELECT id, uid, name, email, created, updated + FROM "team" + WHERE org_id = 0 + AND uid = 'abc' + ORDER BY id asc + LIMIT 0 diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite__users_page_1.sql b/pkg/registry/apis/identity/legacy/testdata/postgres--query_users-users_page_1.sql similarity index 74% rename from pkg/registry/apis/identity/legacy/testdata/sqlite__users_page_1.sql rename to pkg/registry/apis/identity/legacy/testdata/postgres--query_users-users_page_1.sql index 2b509ee1617..fcc0399e261 100755 --- a/pkg/registry/apis/identity/legacy/testdata/sqlite__users_page_1.sql +++ b/pkg/registry/apis/identity/legacy/testdata/postgres--query_users-users_page_1.sql @@ -1,7 +1,7 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? - AND u.is_service_account = ? + WHERE org_user.org_id = 0 + AND u.is_service_account = FALSE ORDER BY u.id asc - LIMIT ? + LIMIT 5 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres__users_page_2.sql b/pkg/registry/apis/identity/legacy/testdata/postgres--query_users-users_page_2.sql similarity index 71% rename from pkg/registry/apis/identity/legacy/testdata/postgres__users_page_2.sql rename to pkg/registry/apis/identity/legacy/testdata/postgres--query_users-users_page_2.sql index df6b238e9e2..dba81cfb64b 100755 --- a/pkg/registry/apis/identity/legacy/testdata/postgres__users_page_2.sql +++ b/pkg/registry/apis/identity/legacy/testdata/postgres--query_users-users_page_2.sql @@ -1,8 +1,8 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = $1 - AND u.is_service_account = $2 - AND id > $3 + WHERE org_user.org_id = 0 + AND u.is_service_account = FALSE + AND id > 1 ORDER BY u.id asc - LIMIT $4 + LIMIT 2 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres--query_users-users_uid.sql b/pkg/registry/apis/identity/legacy/testdata/postgres--query_users-users_uid.sql new file mode 100755 index 00000000000..3e2429254e7 --- /dev/null +++ b/pkg/registry/apis/identity/legacy/testdata/postgres--query_users-users_uid.sql @@ -0,0 +1,8 @@ +SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, + u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin + FROM "user" as u JOIN org_user ON u.id = org_user.user_id + WHERE org_user.org_id = 0 + AND u.is_service_account = FALSE + AND uid = 'abc' + ORDER BY u.id asc + LIMIT 0 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres__display_ids_uids.sql b/pkg/registry/apis/identity/legacy/testdata/postgres__display_ids_uids.sql deleted file mode 100755 index e26b4f4fde4..00000000000 --- a/pkg/registry/apis/identity/legacy/testdata/postgres__display_ids_uids.sql +++ /dev/null @@ -1,9 +0,0 @@ -SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, - u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin - FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = $1 AND ( 1=2 - OR uid IN ($2, $3) - OR u.id IN ($4, $5) - ) - ORDER BY u.id asc - LIMIT 500 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres__teams_page_2.sql b/pkg/registry/apis/identity/legacy/testdata/postgres__teams_page_2.sql deleted file mode 100755 index 6afdd3c7613..00000000000 --- a/pkg/registry/apis/identity/legacy/testdata/postgres__teams_page_2.sql +++ /dev/null @@ -1,6 +0,0 @@ -SELECT id, uid, name, email, created, updated - FROM "team" - WHERE org_id = $1 - AND id > $2 - ORDER BY id asc - LIMIT $3 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres__teams_uid.sql b/pkg/registry/apis/identity/legacy/testdata/postgres__teams_uid.sql deleted file mode 100755 index 1d009e16123..00000000000 --- a/pkg/registry/apis/identity/legacy/testdata/postgres__teams_uid.sql +++ /dev/null @@ -1,6 +0,0 @@ -SELECT id, uid, name, email, created, updated - FROM "team" - WHERE org_id = $1 - AND uid = $2 - ORDER BY id asc - LIMIT $3 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres__users_uid.sql b/pkg/registry/apis/identity/legacy/testdata/postgres__users_uid.sql deleted file mode 100755 index cfbfe5af9cf..00000000000 --- a/pkg/registry/apis/identity/legacy/testdata/postgres__users_uid.sql +++ /dev/null @@ -1,8 +0,0 @@ -SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, - u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin - FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = $1 - AND u.is_service_account = $2 - AND uid = $3 - ORDER BY u.id asc - LIMIT $4 diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite__display_uids.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_display-display_ids.sql similarity index 79% rename from pkg/registry/apis/identity/legacy/testdata/sqlite__display_uids.sql rename to pkg/registry/apis/identity/legacy/testdata/sqlite--query_display-display_ids.sql index 9140b071fb1..1b5d33427cd 100755 --- a/pkg/registry/apis/identity/legacy/testdata/sqlite__display_uids.sql +++ b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_display-display_ids.sql @@ -1,8 +1,8 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? AND ( 1=2 - OR uid IN (?, ?) + WHERE org_user.org_id = 2 AND ( 1=2 + OR u.id IN (1, 2) ) ORDER BY u.id asc LIMIT 500 diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite--query_display-display_ids_uids.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_display-display_ids_uids.sql new file mode 100755 index 00000000000..20b3ce0a36a --- /dev/null +++ b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_display-display_ids_uids.sql @@ -0,0 +1,9 @@ +SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, + u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin + FROM "user" as u JOIN org_user ON u.id = org_user.user_id + WHERE org_user.org_id = 2 AND ( 1=2 + OR uid IN ('a', 'b') + OR u.id IN (1, 2) + ) + ORDER BY u.id asc + LIMIT 500 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres__display_ids.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_display-display_uids.sql similarity index 78% rename from pkg/registry/apis/identity/legacy/testdata/postgres__display_ids.sql rename to pkg/registry/apis/identity/legacy/testdata/sqlite--query_display-display_uids.sql index 2f7693b762a..70832ccd97e 100755 --- a/pkg/registry/apis/identity/legacy/testdata/postgres__display_ids.sql +++ b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_display-display_uids.sql @@ -1,8 +1,8 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = $1 AND ( 1=2 - OR u.id IN ($2, $3) + WHERE org_user.org_id = 2 AND ( 1=2 + OR uid IN ('a', 'b') ) ORDER BY u.id asc LIMIT 500 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres__teams_page_1.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_teams-teams_page_1.sql similarity index 72% rename from pkg/registry/apis/identity/legacy/testdata/postgres__teams_page_1.sql rename to pkg/registry/apis/identity/legacy/testdata/sqlite--query_teams-teams_page_1.sql index f36df0cf98b..d216e8bbead 100755 --- a/pkg/registry/apis/identity/legacy/testdata/postgres__teams_page_1.sql +++ b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_teams-teams_page_1.sql @@ -1,5 +1,5 @@ SELECT id, uid, name, email, created, updated FROM "team" - WHERE org_id = $1 + WHERE org_id = 0 ORDER BY id asc - LIMIT $2 + LIMIT 5 diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite--query_teams-teams_page_2.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_teams-teams_page_2.sql new file mode 100755 index 00000000000..cb147eeb524 --- /dev/null +++ b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_teams-teams_page_2.sql @@ -0,0 +1,6 @@ +SELECT id, uid, name, email, created, updated + FROM "team" + WHERE org_id = 0 + AND id > 1 + ORDER BY id asc + LIMIT 2 diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite--query_teams-teams_uid.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_teams-teams_uid.sql new file mode 100755 index 00000000000..49cb5588bef --- /dev/null +++ b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_teams-teams_uid.sql @@ -0,0 +1,6 @@ +SELECT id, uid, name, email, created, updated + FROM "team" + WHERE org_id = 0 + AND uid = 'abc' + ORDER BY id asc + LIMIT 0 diff --git a/pkg/registry/apis/identity/legacy/testdata/postgres__users_page_1.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_users-users_page_1.sql similarity index 74% rename from pkg/registry/apis/identity/legacy/testdata/postgres__users_page_1.sql rename to pkg/registry/apis/identity/legacy/testdata/sqlite--query_users-users_page_1.sql index 74d102b6e4a..fcc0399e261 100755 --- a/pkg/registry/apis/identity/legacy/testdata/postgres__users_page_1.sql +++ b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_users-users_page_1.sql @@ -1,7 +1,7 @@ SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = $1 - AND u.is_service_account = $2 + WHERE org_user.org_id = 0 + AND u.is_service_account = FALSE ORDER BY u.id asc - LIMIT $3 + LIMIT 5 diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite--query_users-users_page_2.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_users-users_page_2.sql new file mode 100755 index 00000000000..dba81cfb64b --- /dev/null +++ b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_users-users_page_2.sql @@ -0,0 +1,8 @@ +SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, + u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin + FROM "user" as u JOIN org_user ON u.id = org_user.user_id + WHERE org_user.org_id = 0 + AND u.is_service_account = FALSE + AND id > 1 + ORDER BY u.id asc + LIMIT 2 diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite--query_users-users_uid.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_users-users_uid.sql new file mode 100755 index 00000000000..3e2429254e7 --- /dev/null +++ b/pkg/registry/apis/identity/legacy/testdata/sqlite--query_users-users_uid.sql @@ -0,0 +1,8 @@ +SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, + u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin + FROM "user" as u JOIN org_user ON u.id = org_user.user_id + WHERE org_user.org_id = 0 + AND u.is_service_account = FALSE + AND uid = 'abc' + ORDER BY u.id asc + LIMIT 0 diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite__teams_page_2.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite__teams_page_2.sql deleted file mode 100755 index d520548b8aa..00000000000 --- a/pkg/registry/apis/identity/legacy/testdata/sqlite__teams_page_2.sql +++ /dev/null @@ -1,6 +0,0 @@ -SELECT id, uid, name, email, created, updated - FROM "team" - WHERE org_id = ? - AND id > ? - ORDER BY id asc - LIMIT ? diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite__teams_uid.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite__teams_uid.sql deleted file mode 100755 index 2e88bccd126..00000000000 --- a/pkg/registry/apis/identity/legacy/testdata/sqlite__teams_uid.sql +++ /dev/null @@ -1,6 +0,0 @@ -SELECT id, uid, name, email, created, updated - FROM "team" - WHERE org_id = ? - AND uid = ? - ORDER BY id asc - LIMIT ? diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite__users_page_2.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite__users_page_2.sql deleted file mode 100755 index 703f36d44fc..00000000000 --- a/pkg/registry/apis/identity/legacy/testdata/sqlite__users_page_2.sql +++ /dev/null @@ -1,8 +0,0 @@ -SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, - u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin - FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? - AND u.is_service_account = ? - AND id > ? - ORDER BY u.id asc - LIMIT ? diff --git a/pkg/registry/apis/identity/legacy/testdata/sqlite__users_uid.sql b/pkg/registry/apis/identity/legacy/testdata/sqlite__users_uid.sql deleted file mode 100755 index 32f04d7b7c2..00000000000 --- a/pkg/registry/apis/identity/legacy/testdata/sqlite__users_uid.sql +++ /dev/null @@ -1,8 +0,0 @@ -SELECT org_user.org_id, u.id, u.uid, u.login, u.email, u.name, - u.created, u.updated, u.is_service_account, u.is_disabled, u.is_admin - FROM "user" as u JOIN org_user ON u.id = org_user.user_id - WHERE org_user.org_id = ? - AND u.is_service_account = ? - AND uid = ? - ORDER BY u.id asc - LIMIT ? diff --git a/pkg/storage/unified/sql/backend.go b/pkg/storage/unified/sql/backend.go index d9157131ba9..99158184e51 100644 --- a/pkg/storage/unified/sql/backend.go +++ b/pkg/storage/unified/sql/backend.go @@ -136,18 +136,18 @@ func (b *backend) create(ctx context.Context, event resource.WriteEvent) (int64, // 1. Insert into resource if _, err := dbutil.Exec(ctx, tx, sqlResourceInsert, sqlResourceRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - WriteEvent: event, - GUID: guid, + SQLTemplateIface: sqltemplate.New(b.dialect), + WriteEvent: event, + GUID: guid, }); err != nil { return fmt.Errorf("insert into resource: %w", err) } // 2. Insert into resource history if _, err := dbutil.Exec(ctx, tx, sqlResourceHistoryInsert, sqlResourceRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - WriteEvent: event, - GUID: guid, + SQLTemplateIface: sqltemplate.New(b.dialect), + WriteEvent: event, + GUID: guid, }); err != nil { return fmt.Errorf("insert into resource history: %w", err) } @@ -162,17 +162,17 @@ func (b *backend) create(ctx context.Context, event resource.WriteEvent) (int64, // 5. Update the RV in both resource and resource_history if _, err = dbutil.Exec(ctx, tx, sqlResourceHistoryUpdateRV, sqlResourceUpdateRVRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - GUID: guid, - ResourceVersion: rv, + SQLTemplateIface: sqltemplate.New(b.dialect), + GUID: guid, + ResourceVersion: rv, }); err != nil { return fmt.Errorf("update resource_history rv: %w", err) } if _, err = dbutil.Exec(ctx, tx, sqlResourceUpdateRV, sqlResourceUpdateRVRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - GUID: guid, - ResourceVersion: rv, + SQLTemplateIface: sqltemplate.New(b.dialect), + GUID: guid, + ResourceVersion: rv, }); err != nil { return fmt.Errorf("update resource rv: %w", err) } @@ -194,9 +194,9 @@ func (b *backend) update(ctx context.Context, event resource.WriteEvent) (int64, // 1. Update resource _, err := dbutil.Exec(ctx, tx, sqlResourceUpdate, sqlResourceRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - WriteEvent: event, - GUID: guid, + SQLTemplateIface: sqltemplate.New(b.dialect), + WriteEvent: event, + GUID: guid, }) if err != nil { return fmt.Errorf("initial resource update: %w", err) @@ -204,9 +204,9 @@ func (b *backend) update(ctx context.Context, event resource.WriteEvent) (int64, // 2. Insert into resource history if _, err := dbutil.Exec(ctx, tx, sqlResourceHistoryInsert, sqlResourceRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - WriteEvent: event, - GUID: guid, + SQLTemplateIface: sqltemplate.New(b.dialect), + WriteEvent: event, + GUID: guid, }); err != nil { return fmt.Errorf("insert into resource history: %w", err) } @@ -221,17 +221,17 @@ func (b *backend) update(ctx context.Context, event resource.WriteEvent) (int64, // 5. Update the RV in both resource and resource_history if _, err = dbutil.Exec(ctx, tx, sqlResourceHistoryUpdateRV, sqlResourceUpdateRVRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - GUID: guid, - ResourceVersion: rv, + SQLTemplateIface: sqltemplate.New(b.dialect), + GUID: guid, + ResourceVersion: rv, }); err != nil { return fmt.Errorf("update history rv: %w", err) } if _, err = dbutil.Exec(ctx, tx, sqlResourceUpdateRV, sqlResourceUpdateRVRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - GUID: guid, - ResourceVersion: rv, + SQLTemplateIface: sqltemplate.New(b.dialect), + GUID: guid, + ResourceVersion: rv, }); err != nil { return fmt.Errorf("update resource rv: %w", err) } @@ -254,9 +254,9 @@ func (b *backend) delete(ctx context.Context, event resource.WriteEvent) (int64, // 1. delete from resource _, err := dbutil.Exec(ctx, tx, sqlResourceDelete, sqlResourceRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - WriteEvent: event, - GUID: guid, + SQLTemplateIface: sqltemplate.New(b.dialect), + WriteEvent: event, + GUID: guid, }) if err != nil { return fmt.Errorf("delete resource: %w", err) @@ -264,9 +264,9 @@ func (b *backend) delete(ctx context.Context, event resource.WriteEvent) (int64, // 2. Add event to resource history if _, err := dbutil.Exec(ctx, tx, sqlResourceHistoryInsert, sqlResourceRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - WriteEvent: event, - GUID: guid, + SQLTemplateIface: sqltemplate.New(b.dialect), + WriteEvent: event, + GUID: guid, }); err != nil { return fmt.Errorf("insert into resource history: %w", err) } @@ -281,9 +281,9 @@ func (b *backend) delete(ctx context.Context, event resource.WriteEvent) (int64, // 5. Update the RV in resource_history if _, err = dbutil.Exec(ctx, tx, sqlResourceHistoryUpdateRV, sqlResourceUpdateRVRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - GUID: guid, - ResourceVersion: rv, + SQLTemplateIface: sqltemplate.New(b.dialect), + GUID: guid, + ResourceVersion: rv, }); err != nil { return fmt.Errorf("update history rv: %w", err) } @@ -302,9 +302,9 @@ func (b *backend) ReadResource(ctx context.Context, req *resource.ReadRequest) * // TODO: validate key ? readReq := sqlResourceReadRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - Request: req, - readResponse: new(readResponse), + SQLTemplateIface: sqltemplate.New(b.dialect), + Request: req, + readResponse: new(readResponse), } sr := sqlResourceRead @@ -418,8 +418,8 @@ func (b *backend) listLatest(ctx context.Context, req *resource.ListRequest, cb } listReq := sqlResourceListRequest{ - SQLTemplate: sqltemplate.New(b.dialect), - Request: new(resource.ListRequest), + SQLTemplateIface: sqltemplate.New(b.dialect), + Request: new(resource.ListRequest), } listReq.Request = proto.Clone(req).(*resource.ListRequest) @@ -467,7 +467,7 @@ func (b *backend) listAtRevision(ctx context.Context, req *resource.ListRequest, limit = math.MaxInt64 // a limit is required for offset } listReq := sqlResourceHistoryListRequest{ - SQLTemplate: sqltemplate.New(b.dialect), + SQLTemplateIface: sqltemplate.New(b.dialect), Request: &historyListRequest{ ResourceVersion: iter.listRV, Limit: limit, @@ -554,7 +554,7 @@ func (b *backend) poller(ctx context.Context, since groupResourceRV, stream chan func (b *backend) listLatestRVs(ctx context.Context) (groupResourceRV, error) { since := groupResourceRV{} reqRVs := sqlResourceVersionListRequest{ - SQLTemplate: sqltemplate.New(b.dialect), + SQLTemplateIface: sqltemplate.New(b.dialect), groupResourceVersion: new(groupResourceVersion), } query, err := sqltemplate.Execute(sqlResourceVersionList, reqRVs) @@ -585,11 +585,11 @@ func (b *backend) listLatestRVs(ctx context.Context) (groupResourceRV, error) { // fetchLatestRV returns the current maximum RV in the resource table func fetchLatestRV(ctx context.Context, x db.ContextExecer, d sqltemplate.Dialect, group, resource string) (int64, error) { res, err := dbutil.QueryRow(ctx, x, sqlResourceVersionGet, sqlResourceVersionRequest{ - SQLTemplate: sqltemplate.New(d), - Group: group, - Resource: resource, - ReadOnly: true, - resourceVersion: new(resourceVersion), + SQLTemplateIface: sqltemplate.New(d), + Group: group, + Resource: resource, + ReadOnly: true, + resourceVersion: new(resourceVersion), }) if errors.Is(err, sql.ErrNoRows) { return 1, nil @@ -604,7 +604,7 @@ func (b *backend) poll(ctx context.Context, grp string, res string, since int64, defer span.End() pollReq := sqlResourceHistoryPollRequest{ - SQLTemplate: sqltemplate.New(b.dialect), + SQLTemplateIface: sqltemplate.New(b.dialect), Resource: res, Group: grp, SinceResourceVersion: since, @@ -661,19 +661,19 @@ func resourceVersionAtomicInc(ctx context.Context, x db.ContextExecer, d sqltemp // TODO: refactor this code to run in a multi-statement transaction in order to minimize the number of round trips. // 1 Lock the row for update rv, err := dbutil.QueryRow(ctx, x, sqlResourceVersionGet, sqlResourceVersionRequest{ - SQLTemplate: sqltemplate.New(d), - Group: key.Group, - Resource: key.Resource, - resourceVersion: new(resourceVersion), + SQLTemplateIface: sqltemplate.New(d), + Group: key.Group, + Resource: key.Resource, + resourceVersion: new(resourceVersion), }) if errors.Is(err, sql.ErrNoRows) { // if there wasn't a row associated with the given resource, we create one with // version 1 if _, err = dbutil.Exec(ctx, x, sqlResourceVersionInsert, sqlResourceVersionRequest{ - SQLTemplate: sqltemplate.New(d), - Group: key.Group, - Resource: key.Resource, + SQLTemplateIface: sqltemplate.New(d), + Group: key.Group, + Resource: key.Resource, }); err != nil { return 0, fmt.Errorf("insert into resource_version: %w", err) } @@ -687,9 +687,9 @@ func resourceVersionAtomicInc(ctx context.Context, x db.ContextExecer, d sqltemp // 2. Increment the resource version _, err = dbutil.Exec(ctx, x, sqlResourceVersionInc, sqlResourceVersionRequest{ - SQLTemplate: sqltemplate.New(d), - Group: key.Group, - Resource: key.Resource, + SQLTemplateIface: sqltemplate.New(d), + Group: key.Group, + Resource: key.Resource, resourceVersion: &resourceVersion{ ResourceVersion: nextRV, }, diff --git a/pkg/storage/unified/sql/queries.go b/pkg/storage/unified/sql/queries.go index a1b7af5f282..0357546a352 100644 --- a/pkg/storage/unified/sql/queries.go +++ b/pkg/storage/unified/sql/queries.go @@ -58,7 +58,7 @@ var ( ) type sqlResourceRequest struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface GUID string WriteEvent resource.WriteEvent } @@ -80,7 +80,7 @@ func (r *historyPollResponse) Results() (*historyPollResponse, error) { type groupResourceRV map[string]map[string]int64 type sqlResourceHistoryPollRequest struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface Resource string Group string SinceResourceVersion int64 @@ -102,7 +102,7 @@ func (r *readResponse) Results() (*readResponse, error) { } type sqlResourceReadRequest struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface Request *resource.ReadRequest *readResponse } @@ -113,7 +113,7 @@ func (r sqlResourceReadRequest) Validate() error { // List type sqlResourceListRequest struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface Request *resource.ListRequest } @@ -126,7 +126,7 @@ type historyListRequest struct { Options *resource.ListOptions } type sqlResourceHistoryListRequest struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface Request *historyListRequest Response *resource.ResourceWrapper } @@ -150,7 +150,7 @@ func (r sqlResourceHistoryListRequest) Results() (*resource.ResourceWrapper, err // update RV type sqlResourceUpdateRVRequest struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface GUID string ResourceVersion int64 } @@ -174,7 +174,7 @@ func (r *resourceVersion) Results() (*resourceVersion, error) { } type sqlResourceVersionRequest struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface Group, Resource string ReadOnly bool *resourceVersion @@ -185,7 +185,7 @@ func (r sqlResourceVersionRequest) Validate() error { } type sqlResourceVersionListRequest struct { - *sqltemplate.SQLTemplate + sqltemplate.SQLTemplateIface *groupResourceVersion } diff --git a/pkg/storage/unified/sql/queries_test.go b/pkg/storage/unified/sql/queries_test.go index 1f70d8905e1..da2c2665f2e 100644 --- a/pkg/storage/unified/sql/queries_test.go +++ b/pkg/storage/unified/sql/queries_test.go @@ -1,353 +1,184 @@ package sql import ( - "embed" "testing" "text/template" - "github.com/stretchr/testify/require" - "github.com/grafana/grafana/pkg/storage/unified/resource" - "github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate" + "github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate/mocks" ) -//go:embed testdata/* -var testdataFS embed.FS - -func testdata(t *testing.T, filename string) []byte { - t.Helper() - b, err := testdataFS.ReadFile(`testdata/` + filename) - require.NoError(t, err) - - return b -} - func TestQueries(t *testing.T) { - t.Parallel() - - // Each template has one or more test cases, each identified with a - // descriptive name (e.g. "happy path", "error twiddling the frobb"). Each - // of them will test that for the same input data they must produce a result - // that will depend on the Dialect. Expected queries should be defined in - // separate files in the testdata directory. This improves the testing - // experience by separating test data from test code, since mixing both - // tends to make it more difficult to reason about what is being done, - // especially as we want testing code to scale and make it easy to add - // tests. - type ( - // type aliases to make code more semantic and self-documenting - resultSQLFilename = string - dialects = []sqltemplate.Dialect - expected map[resultSQLFilename]dialects - - testCase = struct { - Name string - - // Data should be the struct passed to the template. - Data sqltemplate.SQLTemplateIface - - // Expected maps the filename containing the expected result query - // to the list of dialects that would produce it. For simple - // queries, it is possible that more than one dialect produce the - // same output. The filename is expected to be in the `testdata` - // directory. - Expected expected - } - ) - - // Define tests cases. Most templates are trivial and testing that they - // generate correct code for a single Dialect is fine, since the one thing - // that always changes is how SQL placeholder arguments are passed (most - // Dialects use `?` while PostgreSQL uses `$1`, `$2`, etc.), and that is - // something that should be tested in the Dialect implementation instead of - // here. We will ask to have at least one test per SQL template, and we will - // lean to test MySQL. Templates containing branching (conditionals, loops, - // etc.) should be exercised at least once in each of their branches. - // - // NOTE: in the Data field, make sure to have pointers populated to simulate - // data is set as it would be in a real request. The data being correctly - // populated in each case should be tested in integration tests, where the - // data will actually flow to and from a real database. In this tests we - // only care about producing the correct SQL. - testCases := map[*template.Template][]*testCase{ - sqlResourceDelete: { - { - Name: "single path", - Data: &sqlResourceRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - WriteEvent: resource.WriteEvent{ - Key: &resource.ResourceKey{}, - }, - }, - Expected: expected{ - "resource_delete_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, - }, - "resource_delete_postgres.sql": dialects{ - sqltemplate.PostgreSQL, - }, - }, - }, - }, - - sqlResourceInsert: { - { - Name: "insert into resource", - Data: &sqlResourceRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - WriteEvent: resource.WriteEvent{ - Key: &resource.ResourceKey{}, - }, - }, - Expected: expected{ - "resource_insert_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, - }, - }, - }, - }, - - sqlResourceUpdate: { - { - Name: "single path", - Data: &sqlResourceRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - WriteEvent: resource.WriteEvent{ - Key: &resource.ResourceKey{}, - }, - }, - Expected: expected{ - "resource_update_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, - }, - }, - }, - }, - - sqlResourceRead: { - { - Name: "without resource version", - Data: &sqlResourceReadRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Request: &resource.ReadRequest{ - Key: &resource.ResourceKey{}, - }, - readResponse: new(readResponse), - }, - Expected: expected{ - "resource_read_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, - }, - }, - }, - }, - - sqlResourceList: { - { - Name: "filter on namespace", - Data: &sqlResourceListRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Request: &resource.ListRequest{ - Limit: 10, - Options: &resource.ListOptions{ + mocks.CheckQuerySnapshots(t, mocks.TemplateTestSetup{ + RootDir: "testdata", + Templates: map[*template.Template][]mocks.TemplateTestCase{ + sqlResourceDelete: { + { + Name: "simple", + Data: &sqlResourceRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + WriteEvent: resource.WriteEvent{ Key: &resource.ResourceKey{ - Namespace: "ns", + Namespace: "nn", + Group: "gg", + Resource: "rr", + Name: "name", }, }, }, }, - Expected: expected{ - "resource_list_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, + }, + sqlResourceInsert: { + { + Name: "simple", + Data: &sqlResourceRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + WriteEvent: resource.WriteEvent{ + Key: &resource.ResourceKey{ + Namespace: "nn", + Group: "gg", + Resource: "rr", + Name: "name", + }, + Type: resource.WatchEvent_ADDED, + PreviousRV: 123, + }, + }, + }, + }, + sqlResourceUpdate: { + { + Name: "single path", + Data: &sqlResourceRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + WriteEvent: resource.WriteEvent{ + Key: &resource.ResourceKey{}, + }, + }, + }, + }, + sqlResourceRead: { + { + Name: "without_resource_version", + Data: &sqlResourceReadRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Request: &resource.ReadRequest{ + Key: &resource.ResourceKey{}, + }, + readResponse: new(readResponse), }, }, }, - }, - sqlResourceHistoryList: { - { - Name: "single path", - Data: &sqlResourceHistoryListRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Request: &historyListRequest{ - Limit: 10, - Options: &resource.ListOptions{ - Key: &resource.ResourceKey{ - Namespace: "ns", + sqlResourceList: { + { + Name: "filter_on_namespace", + Data: &sqlResourceListRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Request: &resource.ListRequest{ + Limit: 10, + Options: &resource.ListOptions{ + Key: &resource.ResourceKey{ + Namespace: "ns", + }, }, }, }, - Response: new(resource.ResourceWrapper), }, - Expected: expected{ - "resource_history_list_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, + }, + + sqlResourceHistoryList: { + { + Name: "single path", + Data: &sqlResourceHistoryListRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Request: &historyListRequest{ + Limit: 10, + Options: &resource.ListOptions{ + Key: &resource.ResourceKey{ + Namespace: "ns", + }, + }, + }, + Response: new(resource.ResourceWrapper), }, }, }, - }, - sqlResourceUpdateRV: { - { - Name: "single path", - Data: &sqlResourceUpdateRVRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - }, - Expected: expected{ - "resource_update_rv_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, + sqlResourceUpdateRV: { + { + Name: "single path", + Data: &sqlResourceUpdateRVRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), }, }, }, - }, - sqlResourceHistoryRead: { - { - Name: "single path", - Data: &sqlResourceReadRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - Request: &resource.ReadRequest{ - ResourceVersion: 123, - Key: &resource.ResourceKey{}, - }, - readResponse: new(readResponse), - }, - Expected: expected{ - "resource_history_read_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, + sqlResourceHistoryRead: { + { + Name: "single path", + Data: &sqlResourceReadRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + Request: &resource.ReadRequest{ + ResourceVersion: 123, + Key: &resource.ResourceKey{}, + }, + readResponse: new(readResponse), }, }, }, - }, - sqlResourceHistoryUpdateRV: { - { - Name: "single path", - Data: &sqlResourceUpdateRVRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - }, - Expected: expected{ - "resource_history_update_rv_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, + sqlResourceHistoryUpdateRV: { + { + Name: "single path", + Data: &sqlResourceUpdateRVRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), }, }, }, - }, - sqlResourceHistoryInsert: { - { - Name: "insert into resource_history", - Data: &sqlResourceRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - WriteEvent: resource.WriteEvent{ - Key: &resource.ResourceKey{}, - }, - }, - Expected: expected{ - "resource_history_insert_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, + sqlResourceHistoryInsert: { + { + Name: "insert into resource_history", + Data: &sqlResourceRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + WriteEvent: resource.WriteEvent{ + Key: &resource.ResourceKey{}, + }, }, }, }, - }, - sqlResourceVersionGet: { - { - Name: "single path", - Data: &sqlResourceVersionRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - resourceVersion: new(resourceVersion), - ReadOnly: false, - }, - Expected: expected{ - "resource_version_get_mysql.sql": dialects{ - sqltemplate.MySQL, - }, - "resource_version_get_sqlite.sql": dialects{ - sqltemplate.SQLite, + sqlResourceVersionGet: { + { + Name: "single path", + Data: &sqlResourceVersionRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + resourceVersion: new(resourceVersion), + ReadOnly: false, }, }, }, - }, - sqlResourceVersionInc: { - { - Name: "increment resource version", - Data: &sqlResourceVersionRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - resourceVersion: &resourceVersion{ - ResourceVersion: 123, - }, - }, - Expected: expected{ - "resource_version_inc_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, + sqlResourceVersionInc: { + { + Name: "increment resource version", + Data: &sqlResourceVersionRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), + resourceVersion: &resourceVersion{ + ResourceVersion: 123, + }, }, }, }, - }, - sqlResourceVersionInsert: { - { - Name: "single path", - Data: &sqlResourceVersionRequest{ - SQLTemplate: new(sqltemplate.SQLTemplate), - }, - Expected: expected{ - "resource_version_insert_mysql_sqlite.sql": dialects{ - sqltemplate.MySQL, - sqltemplate.SQLite, + sqlResourceVersionInsert: { + { + Name: "single path", + Data: &sqlResourceVersionRequest{ + SQLTemplateIface: mocks.NewTestingSQLTemplate(), }, }, }, - }, - } - - // Execute test cases - for tmpl, tcs := range testCases { - t.Run(tmpl.Name(), func(t *testing.T) { - t.Parallel() - - for _, tc := range tcs { - t.Run(tc.Name, func(t *testing.T) { - t.Parallel() - - for filename, ds := range tc.Expected { - t.Run(filename, func(t *testing.T) { - // not parallel because we're sharing tc.Data, not - // worth it deep cloning - - rawQuery := string(testdata(t, filename)) - expectedQuery := sqltemplate.FormatSQL(rawQuery) - - for _, d := range ds { - t.Run(d.DialectName(), func(t *testing.T) { - // not parallel for the same reason - - tc.Data.SetDialect(d) - err := tc.Data.Validate() - require.NoError(t, err) - got, err := sqltemplate.Execute(tmpl, tc.Data) - require.NoError(t, err) - got = sqltemplate.FormatSQL(got) - require.Equal(t, expectedQuery, got) - }) - } - }) - } - }) - } - }) - } + }}) } diff --git a/pkg/storage/unified/sql/sqltemplate/mocks/test_snapshots.go b/pkg/storage/unified/sql/sqltemplate/mocks/test_snapshots.go new file mode 100644 index 00000000000..9fda62e1302 --- /dev/null +++ b/pkg/storage/unified/sql/sqltemplate/mocks/test_snapshots.go @@ -0,0 +1,147 @@ +package mocks + +import ( + "fmt" + "os" + "path/filepath" + reflect "reflect" + "strings" + "testing" + "text/template" + + "github.com/google/go-cmp/cmp" + sqltemplate "github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate" + "github.com/stretchr/testify/require" +) + +func NewTestingSQLTemplate() sqltemplate.SQLTemplateIface { + standard := sqltemplate.New(sqltemplate.MySQL) // dialect gets replaced at each iteration + return &testingSQLTemplate{standard} +} + +type testingSQLTemplate struct { + *sqltemplate.SQLTemplate +} + +func (t *testingSQLTemplate) Arg(x any) string { + _ = t.SQLTemplate.Arg(x) // discard the output + + switch v := reflect.ValueOf(x); { + case v.Kind() == reflect.Bool: + if v.Bool() { + return "TRUE" + } + return "FALSE" + + case v.CanInt(), v.CanUint(), v.CanFloat(): + _, ok := x.(fmt.Stringer) + if !ok { + return fmt.Sprintf("%v", x) + } + } + + return fmt.Sprintf("'%v'", x) // single quotes +} + +func (t *testingSQLTemplate) ArgList(slice reflect.Value) (string, error) { + // Copied from upstream Arg + if !slice.IsValid() || slice.Kind() != reflect.Slice { + return "", sqltemplate.ErrInvalidArgList + } + sliceLen := slice.Len() + if sliceLen == 0 { + return "", nil + } + + var b strings.Builder + b.Grow(3*sliceLen - 2) // the list will be ?, ?, ? + for i, l := 0, slice.Len(); i < l; i++ { + if i > 0 { + b.WriteString(", ") + } + b.WriteString(t.Arg(slice.Index(i).Interface())) + } + + return b.String(), nil +} + +type TemplateTestCase struct { + Name string + + // Data should be the struct passed to the template. + Data sqltemplate.SQLTemplateIface +} + +type TemplateTestSetup struct { + // Where the snapshots can be found + RootDir string + + // The template will be run through each dialect + Dialects []sqltemplate.Dialect + + // Check a set of templates against example inputs + Templates map[*template.Template][]TemplateTestCase +} + +func CheckQuerySnapshots(t *testing.T, setup TemplateTestSetup) { + t.Helper() + t.Parallel() + + if len(setup.Dialects) < 1 { + setup.Dialects = []sqltemplate.Dialect{ + sqltemplate.MySQL, + sqltemplate.SQLite, + sqltemplate.PostgreSQL, + } + } + + for tmpl, cases := range setup.Templates { + t.Run(tmpl.Name(), func(t *testing.T) { + t.Parallel() + + tname := strings.TrimSuffix(tmpl.Name(), ".sql") + for _, input := range cases { + t.Run(input.Name, func(t *testing.T) { + t.Parallel() + + require.NotPanics(t, func() { + for _, dialect := range setup.Dialects { + t.Run(dialect.DialectName(), func(t *testing.T) { + // not parallel because we're sharing tc.Data, + // but also not worth deep cloning + input.Data.SetDialect(dialect) + err := input.Data.Validate() + + require.NoError(t, err) + got, err := sqltemplate.Execute(tmpl, input.Data) + require.NoError(t, err) + + clean := sqltemplate.RemoveEmptyLines(got) + + update := false + fname := fmt.Sprintf("%s--%s-%s.sql", dialect.DialectName(), tname, input.Name) + fpath := filepath.Join(setup.RootDir, fname) + + // We can ignore the gosec G304 because this is only for tests + // nolint:gosec + expect, err := os.ReadFile(fpath) + if err != nil || len(expect) < 1 { + update = true + t.Errorf("missing " + fpath) + } else { + if diff := cmp.Diff(string(expect), clean); diff != "" { + t.Errorf("%s: %s", fname, diff) + update = true + } + } + if update { + _ = os.WriteFile(fpath, []byte(clean), 0777) + } + }) + } + }) + }) + } + }) + } +} diff --git a/pkg/storage/unified/sql/sqltemplate/sqltemplate.go b/pkg/storage/unified/sql/sqltemplate/sqltemplate.go index c88a443deec..f44df7414fb 100644 --- a/pkg/storage/unified/sql/sqltemplate/sqltemplate.go +++ b/pkg/storage/unified/sql/sqltemplate/sqltemplate.go @@ -11,21 +11,25 @@ import ( var ( ErrValidationNotImplemented = errors.New("validation not implemented") ErrSQLTemplateNoSerialize = errors.New("SQLTemplate should not be serialized") + + // Make sure SQLTemplate implements the interface + _ SQLTemplateIface = (*SQLTemplate)(nil) ) // SQLTemplate provides comprehensive support for SQL templating, handling // dialect traits, execution arguments and scanning arguments. type SQLTemplate struct { Dialect - Args - ScanDest + *Args + *ScanDest } // New returns a nee *SQLTemplate that will use the given dialect. func New(d Dialect) *SQLTemplate { ret := new(SQLTemplate) + ret.ScanDest = new(ScanDest) + ret.Args = NewArgs(d) ret.SetDialect(d) - return ret } diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_delete-simple.sql b/pkg/storage/unified/sql/testdata/mysql--resource_delete-simple.sql new file mode 100755 index 00000000000..2e1e60d1b79 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_delete-simple.sql @@ -0,0 +1,7 @@ +DELETE FROM "resource" + WHERE 1 = 1 + AND "namespace" = 'nn' + AND "group" = 'gg' + AND "resource" = 'rr' + AND "name" = 'name' +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_history_insert-insert into resource_history.sql b/pkg/storage/unified/sql/testdata/mysql--resource_history_insert-insert into resource_history.sql new file mode 100755 index 00000000000..643741bc3b1 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_history_insert-insert into resource_history.sql @@ -0,0 +1,20 @@ +INSERT INTO "resource_history" + ( + "guid", + "group", + "resource", + "namespace", + "name", + "value", + "action" + ) + VALUES ( + '', + '', + '', + '', + '', + '[]', + 'UNKNOWN' + ) +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_history_list-single path.sql b/pkg/storage/unified/sql/testdata/mysql--resource_history_list-single path.sql new file mode 100755 index 00000000000..3f6d4e7206f --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_history_list-single path.sql @@ -0,0 +1,25 @@ +SELECT + kv."resource_version", + kv."namespace", + kv."name", + kv."value" + FROM "resource_history" as kv + INNER JOIN ( + SELECT "namespace", "group", "resource", "name", max("resource_version") AS "resource_version" + FROM "resource_history" AS mkv + WHERE 1 = 1 + AND "resource_version" <= 0 + AND "namespace" = 'ns' + GROUP BY mkv."namespace", mkv."group", mkv."resource", mkv."name" + ) AS maxkv + ON + maxkv."resource_version" = kv."resource_version" + AND maxkv."namespace" = kv."namespace" + AND maxkv."group" = kv."group" + AND maxkv."resource" = kv."resource" + AND maxkv."name" = kv."name" + WHERE kv."action" != 3 + AND kv."namespace" = 'ns' + ORDER BY kv."namespace" ASC, kv."name" ASC + LIMIT 10 OFFSET 0 +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_history_read-single path.sql b/pkg/storage/unified/sql/testdata/mysql--resource_history_read-single path.sql new file mode 100755 index 00000000000..35127f6b176 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_history_read-single path.sql @@ -0,0 +1,13 @@ +SELECT + "resource_version", + "value" + FROM "resource_history" + WHERE 1 = 1 + AND "namespace" = '' + AND "group" = '' + AND "resource" = '' + AND "name" = '' + AND "resource_version" <= 123 + ORDER BY "resource_version" DESC + LIMIT 1 +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_history_update_rv-single path.sql b/pkg/storage/unified/sql/testdata/mysql--resource_history_update_rv-single path.sql new file mode 100755 index 00000000000..7cddc81c5ab --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_history_update_rv-single path.sql @@ -0,0 +1,4 @@ +UPDATE "resource_history" + SET "resource_version" = 0 + WHERE "guid" = '' +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_insert-simple.sql b/pkg/storage/unified/sql/testdata/mysql--resource_insert-simple.sql new file mode 100755 index 00000000000..9150eb59fef --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_insert-simple.sql @@ -0,0 +1,20 @@ +INSERT INTO "resource" + ( + "guid", + "group", + "resource", + "namespace", + "name", + "value", + "action" + ) + VALUES ( + '', + 'gg', + 'rr', + 'nn', + 'name', + '[]', + 'ADDED' + ) +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_list-filter_on_namespace.sql b/pkg/storage/unified/sql/testdata/mysql--resource_list-filter_on_namespace.sql new file mode 100755 index 00000000000..8a1356f62f8 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_list-filter_on_namespace.sql @@ -0,0 +1,10 @@ +SELECT + "resource_version", + "namespace", + "name", + "value" + FROM "resource" + WHERE 1 = 1 + AND "namespace" = 'ns' + ORDER BY "namespace" ASC, "name" ASC +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_read-without_resource_version.sql b/pkg/storage/unified/sql/testdata/mysql--resource_read-without_resource_version.sql new file mode 100755 index 00000000000..e010d010dbc --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_read-without_resource_version.sql @@ -0,0 +1,10 @@ +SELECT + "resource_version", + "value" + FROM "resource" + WHERE 1 = 1 + AND "namespace" = '' + AND "group" = '' + AND "resource" = '' + AND "name" = '' +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_update-single path.sql b/pkg/storage/unified/sql/testdata/mysql--resource_update-single path.sql new file mode 100755 index 00000000000..c0b2f77ce6c --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_update-single path.sql @@ -0,0 +1,11 @@ +UPDATE "resource" + SET + "guid" = '', + "value" = '[]', + "action" = 'UNKNOWN' + WHERE 1 = 1 + AND "group" = '' + AND "resource" = '' + AND "namespace" = '' + AND "name" = '' +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_update_rv-single path.sql b/pkg/storage/unified/sql/testdata/mysql--resource_update_rv-single path.sql new file mode 100755 index 00000000000..22637847a12 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_update_rv-single path.sql @@ -0,0 +1,4 @@ +UPDATE "resource" + SET "resource_version" = 0 + WHERE "guid" = '' +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_version_get-single path.sql b/pkg/storage/unified/sql/testdata/mysql--resource_version_get-single path.sql new file mode 100755 index 00000000000..d6e8f041e5a --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_version_get-single path.sql @@ -0,0 +1,8 @@ +SELECT + "resource_version" + FROM "resource_version" + WHERE 1 = 1 + AND "group" = '' + AND "resource" = '' + FOR UPDATE +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_version_inc-increment resource version.sql b/pkg/storage/unified/sql/testdata/mysql--resource_version_inc-increment resource version.sql new file mode 100755 index 00000000000..a6f7c024e25 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_version_inc-increment resource version.sql @@ -0,0 +1,7 @@ +UPDATE "resource_version" +SET + "resource_version" = 123 +WHERE 1 = 1 + AND "group" = '' + AND "resource" = '' +; diff --git a/pkg/storage/unified/sql/testdata/mysql--resource_version_insert-single path.sql b/pkg/storage/unified/sql/testdata/mysql--resource_version_insert-single path.sql new file mode 100755 index 00000000000..99003d5fefe --- /dev/null +++ b/pkg/storage/unified/sql/testdata/mysql--resource_version_insert-single path.sql @@ -0,0 +1,12 @@ +INSERT INTO "resource_version" + ( + "group", + "resource", + "resource_version" + ) + VALUES ( + '', + '', + 1 + ) +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_delete-simple.sql b/pkg/storage/unified/sql/testdata/postgres--resource_delete-simple.sql new file mode 100755 index 00000000000..2e1e60d1b79 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_delete-simple.sql @@ -0,0 +1,7 @@ +DELETE FROM "resource" + WHERE 1 = 1 + AND "namespace" = 'nn' + AND "group" = 'gg' + AND "resource" = 'rr' + AND "name" = 'name' +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_history_insert-insert into resource_history.sql b/pkg/storage/unified/sql/testdata/postgres--resource_history_insert-insert into resource_history.sql new file mode 100755 index 00000000000..643741bc3b1 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_history_insert-insert into resource_history.sql @@ -0,0 +1,20 @@ +INSERT INTO "resource_history" + ( + "guid", + "group", + "resource", + "namespace", + "name", + "value", + "action" + ) + VALUES ( + '', + '', + '', + '', + '', + '[]', + 'UNKNOWN' + ) +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_history_list-single path.sql b/pkg/storage/unified/sql/testdata/postgres--resource_history_list-single path.sql new file mode 100755 index 00000000000..3f6d4e7206f --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_history_list-single path.sql @@ -0,0 +1,25 @@ +SELECT + kv."resource_version", + kv."namespace", + kv."name", + kv."value" + FROM "resource_history" as kv + INNER JOIN ( + SELECT "namespace", "group", "resource", "name", max("resource_version") AS "resource_version" + FROM "resource_history" AS mkv + WHERE 1 = 1 + AND "resource_version" <= 0 + AND "namespace" = 'ns' + GROUP BY mkv."namespace", mkv."group", mkv."resource", mkv."name" + ) AS maxkv + ON + maxkv."resource_version" = kv."resource_version" + AND maxkv."namespace" = kv."namespace" + AND maxkv."group" = kv."group" + AND maxkv."resource" = kv."resource" + AND maxkv."name" = kv."name" + WHERE kv."action" != 3 + AND kv."namespace" = 'ns' + ORDER BY kv."namespace" ASC, kv."name" ASC + LIMIT 10 OFFSET 0 +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_history_read-single path.sql b/pkg/storage/unified/sql/testdata/postgres--resource_history_read-single path.sql new file mode 100755 index 00000000000..35127f6b176 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_history_read-single path.sql @@ -0,0 +1,13 @@ +SELECT + "resource_version", + "value" + FROM "resource_history" + WHERE 1 = 1 + AND "namespace" = '' + AND "group" = '' + AND "resource" = '' + AND "name" = '' + AND "resource_version" <= 123 + ORDER BY "resource_version" DESC + LIMIT 1 +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_history_update_rv-single path.sql b/pkg/storage/unified/sql/testdata/postgres--resource_history_update_rv-single path.sql new file mode 100755 index 00000000000..7cddc81c5ab --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_history_update_rv-single path.sql @@ -0,0 +1,4 @@ +UPDATE "resource_history" + SET "resource_version" = 0 + WHERE "guid" = '' +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_insert-simple.sql b/pkg/storage/unified/sql/testdata/postgres--resource_insert-simple.sql new file mode 100755 index 00000000000..9150eb59fef --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_insert-simple.sql @@ -0,0 +1,20 @@ +INSERT INTO "resource" + ( + "guid", + "group", + "resource", + "namespace", + "name", + "value", + "action" + ) + VALUES ( + '', + 'gg', + 'rr', + 'nn', + 'name', + '[]', + 'ADDED' + ) +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_list-filter_on_namespace.sql b/pkg/storage/unified/sql/testdata/postgres--resource_list-filter_on_namespace.sql new file mode 100755 index 00000000000..8a1356f62f8 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_list-filter_on_namespace.sql @@ -0,0 +1,10 @@ +SELECT + "resource_version", + "namespace", + "name", + "value" + FROM "resource" + WHERE 1 = 1 + AND "namespace" = 'ns' + ORDER BY "namespace" ASC, "name" ASC +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_read-without_resource_version.sql b/pkg/storage/unified/sql/testdata/postgres--resource_read-without_resource_version.sql new file mode 100755 index 00000000000..e010d010dbc --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_read-without_resource_version.sql @@ -0,0 +1,10 @@ +SELECT + "resource_version", + "value" + FROM "resource" + WHERE 1 = 1 + AND "namespace" = '' + AND "group" = '' + AND "resource" = '' + AND "name" = '' +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_update-single path.sql b/pkg/storage/unified/sql/testdata/postgres--resource_update-single path.sql new file mode 100755 index 00000000000..c0b2f77ce6c --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_update-single path.sql @@ -0,0 +1,11 @@ +UPDATE "resource" + SET + "guid" = '', + "value" = '[]', + "action" = 'UNKNOWN' + WHERE 1 = 1 + AND "group" = '' + AND "resource" = '' + AND "namespace" = '' + AND "name" = '' +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_update_rv-single path.sql b/pkg/storage/unified/sql/testdata/postgres--resource_update_rv-single path.sql new file mode 100755 index 00000000000..22637847a12 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_update_rv-single path.sql @@ -0,0 +1,4 @@ +UPDATE "resource" + SET "resource_version" = 0 + WHERE "guid" = '' +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_version_get-single path.sql b/pkg/storage/unified/sql/testdata/postgres--resource_version_get-single path.sql new file mode 100755 index 00000000000..d6e8f041e5a --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_version_get-single path.sql @@ -0,0 +1,8 @@ +SELECT + "resource_version" + FROM "resource_version" + WHERE 1 = 1 + AND "group" = '' + AND "resource" = '' + FOR UPDATE +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_version_inc-increment resource version.sql b/pkg/storage/unified/sql/testdata/postgres--resource_version_inc-increment resource version.sql new file mode 100755 index 00000000000..a6f7c024e25 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_version_inc-increment resource version.sql @@ -0,0 +1,7 @@ +UPDATE "resource_version" +SET + "resource_version" = 123 +WHERE 1 = 1 + AND "group" = '' + AND "resource" = '' +; diff --git a/pkg/storage/unified/sql/testdata/postgres--resource_version_insert-single path.sql b/pkg/storage/unified/sql/testdata/postgres--resource_version_insert-single path.sql new file mode 100755 index 00000000000..99003d5fefe --- /dev/null +++ b/pkg/storage/unified/sql/testdata/postgres--resource_version_insert-single path.sql @@ -0,0 +1,12 @@ +INSERT INTO "resource_version" + ( + "group", + "resource", + "resource_version" + ) + VALUES ( + '', + '', + 1 + ) +; diff --git a/pkg/storage/unified/sql/testdata/resource_delete_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_delete_mysql_sqlite.sql deleted file mode 100644 index 0c4e634b02c..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_delete_mysql_sqlite.sql +++ /dev/null @@ -1 +0,0 @@ -DELETE FROM "resource" WHERE 1 = 1 AND "namespace" = ? AND "group" = ? AND "resource" = ? AND "name" = ?; diff --git a/pkg/storage/unified/sql/testdata/resource_delete_postgres.sql b/pkg/storage/unified/sql/testdata/resource_delete_postgres.sql deleted file mode 100644 index 47f39a82e5d..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_delete_postgres.sql +++ /dev/null @@ -1 +0,0 @@ -DELETE FROM "resource" WHERE 1 = 1 AND "namespace" = $1 AND "group" = $2 AND "resource" = $3 AND "name" = $4; diff --git a/pkg/storage/unified/sql/testdata/resource_history_insert_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_history_insert_mysql_sqlite.sql deleted file mode 100644 index d267d8d31bd..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_history_insert_mysql_sqlite.sql +++ /dev/null @@ -1,3 +0,0 @@ -INSERT INTO "resource_history" - ("guid", "group", "resource", "namespace", "name", "value", "action") - VALUES (?, ?, ?, ?, ?, ?, ?); diff --git a/pkg/storage/unified/sql/testdata/resource_history_list_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_history_list_mysql_sqlite.sql deleted file mode 100644 index a7d2dd68e42..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_history_list_mysql_sqlite.sql +++ /dev/null @@ -1,13 +0,0 @@ -SELECT kv."resource_version", kv."namespace", kv."name", kv."value" -FROM "resource_history" as kv -INNER JOIN ( - SELECT "namespace", "group", "resource", "name", max("resource_version") AS "resource_version" - FROM "resource_history" AS mkv - WHERE 1 = 1 AND "resource_version" <= ? AND "namespace" = ? - GROUP BY mkv."namespace", mkv."group", mkv."resource", mkv."name" -) AS maxkv -ON maxkv."resource_version" = kv."resource_version" AND maxkv."namespace" = kv."namespace" AND maxkv."group" = kv."group" AND maxkv."resource" = kv."resource" AND maxkv."name" = kv."name" -WHERE kv."action" != 3 AND kv."namespace" = ? -ORDER BY kv."namespace" ASC, kv."name" ASC -LIMIT ? OFFSET ? -; diff --git a/pkg/storage/unified/sql/testdata/resource_history_read_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_history_read_mysql_sqlite.sql deleted file mode 100644 index 228b54f7e7e..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_history_read_mysql_sqlite.sql +++ /dev/null @@ -1,6 +0,0 @@ -SELECT "resource_version", "value" - FROM "resource_history" - WHERE 1 = 1 AND "namespace" = ? AND "group" = ? AND "resource" = ? AND "name" = ? AND "resource_version" <= ? - ORDER BY "resource_version" DESC - LIMIT 1 -; diff --git a/pkg/storage/unified/sql/testdata/resource_history_update_rv_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_history_update_rv_mysql_sqlite.sql deleted file mode 100644 index e11a0b5f092..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_history_update_rv_mysql_sqlite.sql +++ /dev/null @@ -1,3 +0,0 @@ -UPDATE "resource_history" SET "resource_version" = ? -WHERE "guid" = ? -; diff --git a/pkg/storage/unified/sql/testdata/resource_insert_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_insert_mysql_sqlite.sql deleted file mode 100644 index b61dff00552..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_insert_mysql_sqlite.sql +++ /dev/null @@ -1,4 +0,0 @@ -INSERT INTO "resource" - ("guid", "group", "resource", "namespace", "name", "value", "action") - VALUES (?, ?, ?, ?, ?, ?, ?) -; diff --git a/pkg/storage/unified/sql/testdata/resource_list_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_list_mysql_sqlite.sql deleted file mode 100644 index c19b0dc403b..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_list_mysql_sqlite.sql +++ /dev/null @@ -1,5 +0,0 @@ -SELECT "resource_version", "namespace", "name", "value" - FROM "resource" - WHERE 1 = 1 AND "namespace" = ? - ORDER BY "namespace" ASC, "name" ASC -; diff --git a/pkg/storage/unified/sql/testdata/resource_read_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_read_mysql_sqlite.sql deleted file mode 100644 index 80d06fca4d5..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_read_mysql_sqlite.sql +++ /dev/null @@ -1,4 +0,0 @@ -SELECT "resource_version", "value" - FROM "resource" - WHERE 1 = 1 AND "namespace" = ? AND "group" = ? AND "resource" = ? AND "name" = ? -; diff --git a/pkg/storage/unified/sql/testdata/resource_update_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_update_mysql_sqlite.sql deleted file mode 100644 index 79cb0546a0f..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_update_mysql_sqlite.sql +++ /dev/null @@ -1,4 +0,0 @@ -UPDATE "resource" SET "guid" = ?, "value" = ?, "action" = ? -WHERE 1 =1 AND "group" = ? AND "resource" = ? AND "namespace" = ? AND "name" = ? -; - \ No newline at end of file diff --git a/pkg/storage/unified/sql/testdata/resource_update_rv_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_update_rv_mysql_sqlite.sql deleted file mode 100644 index 71a1128c233..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_update_rv_mysql_sqlite.sql +++ /dev/null @@ -1,4 +0,0 @@ -UPDATE "resource" SET "resource_version" = ? -WHERE "guid" = ? -; - \ No newline at end of file diff --git a/pkg/storage/unified/sql/testdata/resource_version_get_mysql.sql b/pkg/storage/unified/sql/testdata/resource_version_get_mysql.sql deleted file mode 100644 index 0815e484f2f..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_version_get_mysql.sql +++ /dev/null @@ -1,4 +0,0 @@ -SELECT "resource_version" - FROM "resource_version" - WHERE 1 = 1 AND "group" = ? AND "resource" = ? -FOR UPDATE; diff --git a/pkg/storage/unified/sql/testdata/resource_version_get_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_version_get_sqlite.sql deleted file mode 100644 index 329fc0f6d04..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_version_get_sqlite.sql +++ /dev/null @@ -1,4 +0,0 @@ -SELECT "resource_version" - FROM "resource_version" - WHERE 1 = 1 AND "group" = ? AND "resource" = ? -; diff --git a/pkg/storage/unified/sql/testdata/resource_version_inc_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_version_inc_mysql_sqlite.sql deleted file mode 100644 index 2d4c6b37574..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_version_inc_mysql_sqlite.sql +++ /dev/null @@ -1,4 +0,0 @@ -UPDATE "resource_version" - SET "resource_version" = ? - WHERE 1 = 1 AND "group" = ? AND "resource" = ? -; diff --git a/pkg/storage/unified/sql/testdata/resource_version_insert_mysql_sqlite.sql b/pkg/storage/unified/sql/testdata/resource_version_insert_mysql_sqlite.sql deleted file mode 100644 index ca378d00b30..00000000000 --- a/pkg/storage/unified/sql/testdata/resource_version_insert_mysql_sqlite.sql +++ /dev/null @@ -1,3 +0,0 @@ -INSERT INTO "resource_version" - ("group", "resource", "resource_version") - VALUES (?, ?, 1); diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_delete-simple.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_delete-simple.sql new file mode 100755 index 00000000000..2e1e60d1b79 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_delete-simple.sql @@ -0,0 +1,7 @@ +DELETE FROM "resource" + WHERE 1 = 1 + AND "namespace" = 'nn' + AND "group" = 'gg' + AND "resource" = 'rr' + AND "name" = 'name' +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_history_insert-insert into resource_history.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_history_insert-insert into resource_history.sql new file mode 100755 index 00000000000..643741bc3b1 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_history_insert-insert into resource_history.sql @@ -0,0 +1,20 @@ +INSERT INTO "resource_history" + ( + "guid", + "group", + "resource", + "namespace", + "name", + "value", + "action" + ) + VALUES ( + '', + '', + '', + '', + '', + '[]', + 'UNKNOWN' + ) +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_history_list-single path.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_history_list-single path.sql new file mode 100755 index 00000000000..3f6d4e7206f --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_history_list-single path.sql @@ -0,0 +1,25 @@ +SELECT + kv."resource_version", + kv."namespace", + kv."name", + kv."value" + FROM "resource_history" as kv + INNER JOIN ( + SELECT "namespace", "group", "resource", "name", max("resource_version") AS "resource_version" + FROM "resource_history" AS mkv + WHERE 1 = 1 + AND "resource_version" <= 0 + AND "namespace" = 'ns' + GROUP BY mkv."namespace", mkv."group", mkv."resource", mkv."name" + ) AS maxkv + ON + maxkv."resource_version" = kv."resource_version" + AND maxkv."namespace" = kv."namespace" + AND maxkv."group" = kv."group" + AND maxkv."resource" = kv."resource" + AND maxkv."name" = kv."name" + WHERE kv."action" != 3 + AND kv."namespace" = 'ns' + ORDER BY kv."namespace" ASC, kv."name" ASC + LIMIT 10 OFFSET 0 +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_history_read-single path.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_history_read-single path.sql new file mode 100755 index 00000000000..35127f6b176 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_history_read-single path.sql @@ -0,0 +1,13 @@ +SELECT + "resource_version", + "value" + FROM "resource_history" + WHERE 1 = 1 + AND "namespace" = '' + AND "group" = '' + AND "resource" = '' + AND "name" = '' + AND "resource_version" <= 123 + ORDER BY "resource_version" DESC + LIMIT 1 +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_history_update_rv-single path.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_history_update_rv-single path.sql new file mode 100755 index 00000000000..7cddc81c5ab --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_history_update_rv-single path.sql @@ -0,0 +1,4 @@ +UPDATE "resource_history" + SET "resource_version" = 0 + WHERE "guid" = '' +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_insert-simple.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_insert-simple.sql new file mode 100755 index 00000000000..9150eb59fef --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_insert-simple.sql @@ -0,0 +1,20 @@ +INSERT INTO "resource" + ( + "guid", + "group", + "resource", + "namespace", + "name", + "value", + "action" + ) + VALUES ( + '', + 'gg', + 'rr', + 'nn', + 'name', + '[]', + 'ADDED' + ) +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_list-filter_on_namespace.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_list-filter_on_namespace.sql new file mode 100755 index 00000000000..8a1356f62f8 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_list-filter_on_namespace.sql @@ -0,0 +1,10 @@ +SELECT + "resource_version", + "namespace", + "name", + "value" + FROM "resource" + WHERE 1 = 1 + AND "namespace" = 'ns' + ORDER BY "namespace" ASC, "name" ASC +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_read-without_resource_version.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_read-without_resource_version.sql new file mode 100755 index 00000000000..e010d010dbc --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_read-without_resource_version.sql @@ -0,0 +1,10 @@ +SELECT + "resource_version", + "value" + FROM "resource" + WHERE 1 = 1 + AND "namespace" = '' + AND "group" = '' + AND "resource" = '' + AND "name" = '' +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_update-single path.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_update-single path.sql new file mode 100755 index 00000000000..c0b2f77ce6c --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_update-single path.sql @@ -0,0 +1,11 @@ +UPDATE "resource" + SET + "guid" = '', + "value" = '[]', + "action" = 'UNKNOWN' + WHERE 1 = 1 + AND "group" = '' + AND "resource" = '' + AND "namespace" = '' + AND "name" = '' +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_update_rv-single path.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_update_rv-single path.sql new file mode 100755 index 00000000000..22637847a12 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_update_rv-single path.sql @@ -0,0 +1,4 @@ +UPDATE "resource" + SET "resource_version" = 0 + WHERE "guid" = '' +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_version_get-single path.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_version_get-single path.sql new file mode 100755 index 00000000000..071a12f8677 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_version_get-single path.sql @@ -0,0 +1,7 @@ +SELECT + "resource_version" + FROM "resource_version" + WHERE 1 = 1 + AND "group" = '' + AND "resource" = '' +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_version_inc-increment resource version.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_version_inc-increment resource version.sql new file mode 100755 index 00000000000..a6f7c024e25 --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_version_inc-increment resource version.sql @@ -0,0 +1,7 @@ +UPDATE "resource_version" +SET + "resource_version" = 123 +WHERE 1 = 1 + AND "group" = '' + AND "resource" = '' +; diff --git a/pkg/storage/unified/sql/testdata/sqlite--resource_version_insert-single path.sql b/pkg/storage/unified/sql/testdata/sqlite--resource_version_insert-single path.sql new file mode 100755 index 00000000000..99003d5fefe --- /dev/null +++ b/pkg/storage/unified/sql/testdata/sqlite--resource_version_insert-single path.sql @@ -0,0 +1,12 @@ +INSERT INTO "resource_version" + ( + "group", + "resource", + "resource_version" + ) + VALUES ( + '', + '', + 1 + ) +;