Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions supabase/migrations/20240414161707_basejump-setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ CREATE TABLE IF NOT EXISTS basejump.config
enable_team_accounts boolean default true,
enable_personal_account_billing boolean default true,
enable_team_account_billing boolean default true,
enable_automatic_personal_team boolean default true, -- In some setups you might not want to automatically create a personal space. In this case set this value to false.
billing_provider text default 'stripe'
);

-- create config row
INSERT INTO basejump.config (enable_team_accounts, enable_personal_account_billing, enable_team_account_billing)
VALUES (true, true, true);
INSERT INTO basejump.config (enable_team_accounts, enable_personal_account_billing, enable_team_account_billing, enable_automatic_personal_team)
VALUES (true, true, true, false);

-- enable select on the config table
GRANT SELECT ON basejump.config TO authenticated, service_role;
Expand Down
28 changes: 16 additions & 12 deletions supabase/migrations/20240414161947_basejump-accounts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,25 @@ declare
first_account_id uuid;
generated_user_name text;
begin
-- Check the condition
if basejump.is_set('enable_automatic_personal_team') then

-- first we setup the user profile
-- TODO: see if we can get the user's name from the auth.users table once we learn how oauth works
if new.email IS NOT NULL then
generated_user_name := split_part(new.email, '@', 1);
end if;
-- create the new users's personal account
insert into basejump.accounts (name, primary_owner_user_id, personal_account, id)
values (generated_user_name, NEW.id, true, NEW.id)
returning id into first_account_id;
-- first we setup the user profile
-- TODO: see if we can get the user's name from the auth.users table once we learn how oauth works
if new.email IS NOT NULL then
generated_user_name := split_part(new.email, '@', 1);
end if;
-- create the new users's personal account
insert into basejump.accounts (name, primary_owner_user_id, personal_account, id)
values (generated_user_name, NEW.id, true, NEW.id)
returning id into first_account_id;

-- add them to the account_user table so they can act on it
insert into basejump.account_user (account_id, user_id, account_role)
values (first_account_id, NEW.id, 'owner');
-- add them to the account_user table so they can act on it
insert into basejump.account_user (account_id, user_id, account_role)
values (first_account_id, NEW.id, 'owner');

return NEW;
end if;
return NEW;
end;
$$;
Expand Down
4 changes: 2 additions & 2 deletions supabase/tests/database/01-basejump-schema-tests.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(20);

Expand All @@ -15,7 +15,7 @@ select has_table('basejump', 'billing_subscriptions', 'Basejump billing_subscrip
select tests.rls_enabled('basejump');

select columns_are('basejump', 'config',
Array ['enable_team_accounts', 'enable_personal_account_billing', 'enable_team_account_billing', 'billing_provider'],
Array ['enable_team_accounts', 'enable_personal_account_billing', 'enable_team_account_billing', 'enable_automatic_personal_team', 'billing_provider'],
'Basejump config table should have the correct columns');


Expand Down
39 changes: 39 additions & 0 deletions supabase/tests/database/02-no-personal-accounts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
BEGIN;
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(2);

-- make sure we're not setup for automatic personal team account creation
update basejump.config
set enable_automatic_personal_team = false;

--- we insert a user into auth.users and return the id into user_id to use

select tests.create_supabase_user('test1', 'test1@test.com');
select tests.create_supabase_user('test2');

select tests.create_supabase_user('test3', 'test3@test.com');
select tests.create_supabase_user('test4', 'test4@test.com');


------------
--- Primary Owner
------------
select tests.authenticate_as('test1');

-- should not create any personal account automatically
SELECT is_empty(
$$ select name from basejump.accounts limit 1 $$,
'Inserting a user should create a personal account when personal accounts are enabled'
);

-- should have not created accounts that are personal
SELECT is_empty(
$$ select name from basejump.accounts where personal_account = true limit 1 $$,
'Should not have created personal accounts'
);

SELECT *
FROM finish();

ROLLBACK;
6 changes: 5 additions & 1 deletion supabase/tests/database/02-personal-accounts.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(15);

-- make sure we're setup for automatic personal team account creation
update basejump.config
set enable_automatic_personal_team = true;

--- we insert a user into auth.users and return the id into user_id to use

select tests.create_supabase_user('test1', 'test1@test.com');
Expand Down
2 changes: 1 addition & 1 deletion supabase/tests/database/04-team-accounts.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(34);

Expand Down
2 changes: 1 addition & 1 deletion supabase/tests/database/05-team-accounts-disabled.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(1);

Expand Down
2 changes: 1 addition & 1 deletion supabase/tests/database/06-invitations.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- the main testing for invitations on accounts is in the team_accounts tests
-- this batch is to let us test the more complicated behaviors such as one_time, 24_hour, multiple use, etc...accounts
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(35);

Expand Down
7 changes: 5 additions & 2 deletions supabase/tests/database/07-inviting-team-member.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(10);

-- make sure we're setup for enabling personal accounts
update basejump.config
set enable_team_accounts = true;
set enable_team_accounts = true,
-- make sure we're setup for automatic personal team account creation
enable_automatic_personal_team = true;


--- Create the users we need for testing
select tests.create_supabase_user('test1');
Expand Down
2 changes: 1 addition & 1 deletion supabase/tests/database/08-inviting-team-owner.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(9);

Expand Down
2 changes: 1 addition & 1 deletion supabase/tests/database/09-removing-team-members.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(6);

Expand Down
2 changes: 1 addition & 1 deletion supabase/tests/database/10-account-roles.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(17);
-- make sure we're setup for enabling personal accounts
Expand Down
8 changes: 5 additions & 3 deletions supabase/tests/database/11-public-account-functions.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(29);

-- make sure we're setup for the test correctly
update basejump.config
set enable_team_accounts = true;
update basejump.config
set enable_team_accounts = true,
-- make sure we're setup for automatic personal team account creation
enable_automatic_personal_team = true;

--- we insert a user into auth.users and return the id into user_id to use
select tests.create_supabase_user('test1');
Expand Down
2 changes: 1 addition & 1 deletion supabase/tests/database/12-created-by-tracking.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

select plan(5);

Expand Down
2 changes: 1 addition & 1 deletion supabase/tests/database/14-public-billing-functions.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

update basejump.config
set enable_team_account_billing = TRUE,
Expand Down
6 changes: 4 additions & 2 deletions supabase/tests/database/15-billing-disabled-functions.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
BEGIN;
create extension "basejump-supabase_test_helpers" version '0.0.6';
create extension if not exists "basejump-supabase_test_helpers" version '0.0.6';

update basejump.config
set enable_personal_account_billing = FALSE,
enable_team_account_billing = FALSE;
enable_team_account_billing = FALSE,
-- make sure we're setup for automatic personal team account creation
enable_automatic_personal_team = true;

select plan(6);

Expand Down