From e2b787c056b9cf235a3831532ba877cc9580b9db Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:22:00 -0600 Subject: [PATCH 1/7] chore: add database test fixture to insert non-unique linked_ids --- .../fixtures/000189_workspace_app_order.up.sql | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql new file mode 100644 index 0000000000000..3e41155957fe7 --- /dev/null +++ b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql @@ -0,0 +1,14 @@ +-- This is a deleted user that shares the same username and linked_id as the existing user below. +-- Any future migrations need to handle this case. +INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) + VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', true) ON CONFLICT DO NOTHING; +INSERT INTO public.organization_members VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; +INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) + VALUES('a0061a8e-7db7-4585-838c-3116a003dd21', 'github', '100', ''); + + +INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) + VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', false) ON CONFLICT DO NOTHING; +INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; +INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) + VALUES('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'github', '100', ''); From 43356eda79773526a86cf7db261f8a386c08a653 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:23:18 -0600 Subject: [PATCH 2/7] mend --- .../testdata/fixtures/000189_workspace_app_order.up.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql index 3e41155957fe7..d3fd503b4b79d 100644 --- a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql +++ b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql @@ -1,13 +1,13 @@ -- This is a deleted user that shares the same username and linked_id as the existing user below. -- Any future migrations need to handle this case. -INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', true) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) VALUES('a0061a8e-7db7-4585-838c-3116a003dd21', 'github', '100', ''); -INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', false) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) From 2791347d65301f1929b574450da0e132e9721c6a Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:25:48 -0600 Subject: [PATCH 3/7] mend --- coderd/database/migrations/migrate_test.go | 2 +- .../testdata/fixtures/000189_workspace_app_order.up.sql | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/coderd/database/migrations/migrate_test.go b/coderd/database/migrations/migrate_test.go index c475c1fa5f026..a01ee8e7f3ef3 100644 --- a/coderd/database/migrations/migrate_test.go +++ b/coderd/database/migrations/migrate_test.go @@ -352,7 +352,7 @@ func TestMigrateUpWithFixtures(t *testing.T) { for _, table := range tables { var count int - err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM "+table).Scan(&count) + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM"+table).Scan(&count) require.NoError(t, err) if tt.useStats { diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql index d3fd503b4b79d..5424c294bd221 100644 --- a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql +++ b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql @@ -1,13 +1,13 @@ -- This is a deleted user that shares the same username and linked_id as the existing user below. -- Any future migrations need to handle this case. -INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, deleted) VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', true) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) VALUES('a0061a8e-7db7-4585-838c-3116a003dd21', 'github', '100', ''); -INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, deleted) VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', false) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) From 549fee96a5ef68186356fffafdad8fe6fcac7c7c Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:28:41 -0600 Subject: [PATCH 4/7] rename test fixute --- ...kspace_app_order.up.sql => 000189_duplicate_user_links.up.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename coderd/database/migrations/testdata/fixtures/{000189_workspace_app_order.up.sql => 000189_duplicate_user_links.up.sql} (100%) diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql similarity index 100% rename from coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql rename to coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql From 7b082eff683d48b0fa6c77eef7a748223f40fd45 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:30:01 -0600 Subject: [PATCH 5/7] add extra case --- .../testdata/fixtures/000189_duplicate_user_links.up.sql | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql b/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql index 5424c294bd221..0fb1d0efd4aca 100644 --- a/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql +++ b/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql @@ -12,3 +12,8 @@ INSERT INTO public.users(id, email, username, hashed_password, created_at, updat INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) VALUES('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'github', '100', ''); + +-- Additionally, there is no unique constraint on user_id. So also add another user_link for the same user. +-- This has happened on a production database. +INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) +VALUES('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'oidc', 'foo', ''); From 90e7164c2f0d630081b4404a5bbaaac66a4bcb52 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:33:01 -0600 Subject: [PATCH 6/7] accidental file change --- coderd/database/migrations/migrate_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/database/migrations/migrate_test.go b/coderd/database/migrations/migrate_test.go index a01ee8e7f3ef3..c475c1fa5f026 100644 --- a/coderd/database/migrations/migrate_test.go +++ b/coderd/database/migrations/migrate_test.go @@ -352,7 +352,7 @@ func TestMigrateUpWithFixtures(t *testing.T) { for _, table := range tables { var count int - err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM"+table).Scan(&count) + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM "+table).Scan(&count) require.NoError(t, err) if tt.useStats { From fdf04ed29008e7ce8042b931e8f20db1b7b05d9b Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 13 Feb 2024 11:06:10 -0600 Subject: [PATCH 7/7] move migration closer to source Added to the migration that added the delete field to users --- ...00189_duplicate_user_links.up.sql => 000048_userdelete.up.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename coderd/database/migrations/testdata/fixtures/{000189_duplicate_user_links.up.sql => 000048_userdelete.up.sql} (100%) diff --git a/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql b/coderd/database/migrations/testdata/fixtures/000048_userdelete.up.sql similarity index 100% rename from coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql rename to coderd/database/migrations/testdata/fixtures/000048_userdelete.up.sql