Skip to content

Commit fb2f040

Browse files
authored
Optimize username resolution with durable identities (#1320)
List users by quering the users table. The previous method of querying lots of different tables that have a "users" field in them was very inefficent and slow
1 parent 3fa39a6 commit fb2f040

17 files changed

Lines changed: 1062 additions & 53 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"operations": [
3+
{
4+
"sql": {
5+
"up": "LOCK TABLE public.users, public.workflows, public.apps, public.app_versions, public.credential IN SHARE ROW EXCLUSIVE MODE; DO $$ BEGIN IF EXISTS (SELECT 1 FROM public.users WHERE id = '') OR EXISTS (SELECT 1 FROM public.workflows WHERE submitted_by = '') OR EXISTS (SELECT 1 FROM public.apps WHERE owner = '') OR EXISTS (SELECT 1 FROM public.app_versions WHERE created_by = '') THEN RAISE EXCEPTION 'empty persistent user identity'; END IF; INSERT INTO public.users (id, created_by) SELECT historical.id, 'migration' FROM (SELECT submitted_by AS id FROM public.workflows WHERE submitted_by IS NOT NULL UNION SELECT owner AS id FROM public.apps WHERE owner IS NOT NULL UNION SELECT created_by AS id FROM public.app_versions WHERE created_by IS NOT NULL) AS historical ON CONFLICT (id) DO NOTHING; DELETE FROM public.credential AS credential WHERE NOT EXISTS (SELECT 1 FROM public.users WHERE users.id = credential.user_name); IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conrelid = 'public.credential'::regclass AND conname = 'credential_user_name_fkey') THEN ALTER TABLE public.credential ADD CONSTRAINT credential_user_name_fkey FOREIGN KEY (user_name) REFERENCES public.users(id) ON DELETE CASCADE NOT VALID; END IF; ALTER TABLE public.credential VALIDATE CONSTRAINT credential_user_name_fkey; END $$; CREATE INDEX IF NOT EXISTS users_base_username_id_idx ON public.users ((split_part(id, '@', 1)), id)"
6+
}
7+
}
8+
]
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
filegroup(
18+
name = "migration_files",
19+
srcs = glob(["*.json"]),
20+
visibility = ["//deployments/charts/service/migrations/tests:__pkg__"],
21+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
load("//bzl:py.bzl", "osmo_py_test")
18+
load("@osmo_python_deps//:requirements.bzl", "requirement")
19+
20+
osmo_py_test(
21+
name = "test_users_backfill_migration",
22+
srcs = ["test_users_backfill_migration.py"],
23+
data = ["//deployments/charts/service/migrations:migration_files"],
24+
deps = [
25+
"//src/tests/common",
26+
"//src/utils/connectors",
27+
"@rules_python//python/runfiles",
28+
requirement("psycopg2-binary"),
29+
],
30+
size = "medium",
31+
tags = ["requires-network"],
32+
)

0 commit comments

Comments
 (0)