From 6908f7a7a240e8c6c4a0397e5fce53b3084899c5 Mon Sep 17 00:00:00 2001 From: Michael Gaffney Date: Mon, 22 Jun 2020 13:54:55 -0400 Subject: [PATCH] Use standard column names (#138) --- internal/auth/db_test.go | 22 +++++++-------- internal/db/migrations/postgres.gen.go | 27 ++++++++++--------- .../db/migrations/postgres/07_auth.up.sql | 27 ++++++++++--------- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/internal/auth/db_test.go b/internal/auth/db_test.go index 98c69584b..082c4a749 100644 --- a/internal/auth/db_test.go +++ b/internal/auth/db_test.go @@ -13,13 +13,13 @@ func TestDB_AuthMethodIDTrigger(t *testing.T) { const ( createTable = ` create table if not exists test_auth_method ( - auth_method_id wt_public_id primary key, - iam_scope_id wt_public_id not null references iam_scope(public_id) + public_id wt_public_id primary key, + scope_id wt_public_id not null references iam_scope(public_id) ); ` insert = ` insert into test_auth_method - (auth_method_id, iam_scope_id) + (public_id, scope_id) values ($1, $2); ` @@ -31,10 +31,10 @@ insert on test_auth_method for each row execute procedure insert_auth_method_subtype(); ` baseTableQuery = ` -select count(*) from auth_method where auth_method_id = $1; +select count(*) from auth_method where public_id = $1; ` testTableQuery = ` -select count(*) from test_auth_method where auth_method_id = $1; +select count(*) from test_auth_method where public_id = $1; ` ) @@ -81,14 +81,14 @@ func TestDB_AuthAccountIDTrigger(t *testing.T) { const ( createTable = ` create table if not exists test_auth_account ( - auth_account_id wt_public_id primary key, + public_id wt_public_id primary key, auth_method_id wt_public_id not null, - iam_scope_id wt_public_id not null + scope_id wt_public_id not null ); ` insert = ` insert into test_auth_account - (auth_account_id, auth_method_id, iam_scope_id) + (public_id, auth_method_id, scope_id) values ($1, $2, $3); ` @@ -100,14 +100,14 @@ insert on test_auth_account for each row execute procedure insert_auth_account_subtype(); ` baseTableQuery = ` -select count(*) from auth_account where auth_account_id = $1; +select count(*) from auth_account where public_id = $1; ` testTableQuery = ` -select count(*) from test_auth_account where auth_account_id = $1; +select count(*) from test_auth_account where public_id = $1; ` insertAuthMethod = ` insert into auth_method - (auth_method_id, iam_scope_id) + (public_id, scope_id) values ($1, $2); ` diff --git a/internal/db/migrations/postgres.gen.go b/internal/db/migrations/postgres.gen.go index e28232fb4..fbe0630b5 100644 --- a/internal/db/migrations/postgres.gen.go +++ b/internal/db/migrations/postgres.gen.go @@ -622,8 +622,8 @@ begin; -- base table for auth methods create table auth_method ( - auth_method_id wt_public_id primary key, - iam_scope_id wt_public_id not null + public_id wt_public_id primary key, + scope_id wt_public_id not null references iam_scope(public_id) on delete cascade on update cascade, @@ -631,24 +631,25 @@ begin; -- The order of columns is important for performance. See: -- https://dba.stackexchange.com/questions/58970/enforcing-constraints-two-tables-away/58972#58972 -- https://dba.stackexchange.com/questions/27481/is-a-composite-index-also-good-for-queries-on-the-first-field - unique(iam_scope_id, auth_method_id) + unique(scope_id, public_id) ); -- base table for auth accounts create table auth_account ( - auth_account_id wt_public_id primary key, + public_id wt_public_id primary key, auth_method_id wt_public_id not null, - iam_scope_id wt_public_id not null, + scope_id wt_public_id not null, iam_user_id wt_public_id, - foreign key (iam_scope_id, auth_method_id) - references auth_method (iam_scope_id, auth_method_id) + foreign key (scope_id, auth_method_id) + references auth_method (scope_id, public_id) on delete cascade on update cascade, - foreign key (iam_scope_id, iam_user_id) + foreign key (scope_id, iam_user_id) references iam_user (scope_id, public_id) on delete set null - on update cascade + on update cascade, + unique(scope_id, auth_method_id, public_id) ); @@ -658,9 +659,9 @@ begin; as $$ begin insert into auth_method - (auth_method_id, iam_scope_id) + (public_id, scope_id) values - (new.auth_method_id, new.iam_scope_id); + (new.public_id, new.scope_id); return new; end; $$ language plpgsql; @@ -671,9 +672,9 @@ begin; as $$ begin insert into auth_account - (auth_account_id, auth_method_id, iam_scope_id) + (public_id, auth_method_id, scope_id) values - (new.auth_account_id, new.auth_method_id, new.iam_scope_id); + (new.public_id, new.auth_method_id, new.scope_id); return new; end; $$ language plpgsql; diff --git a/internal/db/migrations/postgres/07_auth.up.sql b/internal/db/migrations/postgres/07_auth.up.sql index bd2655726..d0bc15dd8 100644 --- a/internal/db/migrations/postgres/07_auth.up.sql +++ b/internal/db/migrations/postgres/07_auth.up.sql @@ -9,8 +9,8 @@ begin; -- base table for auth methods create table auth_method ( - auth_method_id wt_public_id primary key, - iam_scope_id wt_public_id not null + public_id wt_public_id primary key, + scope_id wt_public_id not null references iam_scope(public_id) on delete cascade on update cascade, @@ -18,24 +18,25 @@ begin; -- The order of columns is important for performance. See: -- https://dba.stackexchange.com/questions/58970/enforcing-constraints-two-tables-away/58972#58972 -- https://dba.stackexchange.com/questions/27481/is-a-composite-index-also-good-for-queries-on-the-first-field - unique(iam_scope_id, auth_method_id) + unique(scope_id, public_id) ); -- base table for auth accounts create table auth_account ( - auth_account_id wt_public_id primary key, + public_id wt_public_id primary key, auth_method_id wt_public_id not null, - iam_scope_id wt_public_id not null, + scope_id wt_public_id not null, iam_user_id wt_public_id, - foreign key (iam_scope_id, auth_method_id) - references auth_method (iam_scope_id, auth_method_id) + foreign key (scope_id, auth_method_id) + references auth_method (scope_id, public_id) on delete cascade on update cascade, - foreign key (iam_scope_id, iam_user_id) + foreign key (scope_id, iam_user_id) references iam_user (scope_id, public_id) on delete set null - on update cascade + on update cascade, + unique(scope_id, auth_method_id, public_id) ); @@ -45,9 +46,9 @@ begin; as $$ begin insert into auth_method - (auth_method_id, iam_scope_id) + (public_id, scope_id) values - (new.auth_method_id, new.iam_scope_id); + (new.public_id, new.scope_id); return new; end; $$ language plpgsql; @@ -58,9 +59,9 @@ begin; as $$ begin insert into auth_account - (auth_account_id, auth_method_id, iam_scope_id) + (public_id, auth_method_id, scope_id) values - (new.auth_account_id, new.auth_method_id, new.iam_scope_id); + (new.public_id, new.auth_method_id, new.scope_id); return new; end; $$ language plpgsql;