-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: Add migrations and seed data #20
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
apps/api/supabase/migrations/20240901155537_create_auth_users_triggers.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
create function public .handle_new_user() returns trigger language plpgsql security definer | ||
set | ||
search_path = '' as $$ begin | ||
insert into | ||
public .users (id, email, full_name, username) | ||
values | ||
( | ||
new .id, | ||
new .email, | ||
new .raw_user_meta_data ->> 'full_name', | ||
new .raw_user_meta_data ->> 'username' | ||
); | ||
|
||
return new; | ||
|
||
end; | ||
|
||
$$; | ||
|
||
-- trigger the function every time a user is created | ||
create trigger on_auth_user_created after | ||
insert | ||
on auth.users for each row execute function public .handle_new_user(); |
46 changes: 46 additions & 0 deletions
46
apps/api/supabase/migrations/20240901155538_create_users_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
-- create users table | ||
create table public .users ( | ||
id uuid primary key, | ||
username text unique not null, | ||
email text unique not null, | ||
full_name text, | ||
avatar_url text, | ||
bio text, | ||
created_at timestamp with time zone default now(), | ||
updated_at timestamp with time zone default now(), | ||
constraint fk_auth_user foreign key (id) references auth.users(id) on | ||
delete | ||
cascade | ||
); | ||
|
||
-- create index on username for faster lookups | ||
create index idx_users_username on public .users (username); | ||
|
||
-- enable row level security (rls) | ||
alter table | ||
public .users enable row level security; | ||
|
||
-- create a trigger to update the updated_at column | ||
create | ||
or replace function update_updated_at() returns trigger as $$ begin | ||
new .updated_at = now(); | ||
|
||
return new; | ||
|
||
end; | ||
|
||
$$ language plpgsql; | ||
|
||
create trigger users_updated_at before | ||
update | ||
on public .users for each row execute function update_updated_at(); | ||
|
||
-- create a policy to allow users to read all profiles | ||
create policy read_all_profiles on public .users for | ||
select | ||
using (true); | ||
|
||
-- create a policy to allow users to update their own profile | ||
create policy update_own_profile on public .users for | ||
pontusab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
update | ||
using (auth.uid() = id); |
60 changes: 60 additions & 0 deletions
60
apps/api/supabase/migrations/20240901165124_create_posts_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
-- create posts table | ||
create table posts ( | ||
id uuid primary key default gen_random_uuid(), | ||
user_id uuid not null, | ||
title text not null, | ||
content text not null, | ||
created_at timestamptz not null default now(), | ||
updated_at timestamptz not null default now() | ||
); | ||
|
||
-- add foreign key constraint | ||
alter table | ||
posts | ||
add | ||
constraint fk_posts_user foreign key (user_id) references public .users(id) on | ||
delete | ||
cascade; | ||
|
||
-- create index for faster queries | ||
create index idx_posts_user_id on posts(user_id); | ||
|
||
-- add rls policies | ||
alter table | ||
posts enable row level security; | ||
|
||
-- policy to allow anyone to read all posts | ||
create policy "allow read access for all users" on posts for | ||
select | ||
using (true); | ||
|
||
-- policy to allow users to insert their own posts | ||
create policy "allow insert for authenticated users" on posts for | ||
insert | ||
with check (auth.uid() = user_id); | ||
|
||
-- policy to allow users to update their own posts | ||
create policy "allow update for post owners" on posts for | ||
update | ||
using (auth.uid() = user_id); | ||
|
||
-- policy to allow users to delete their own posts | ||
create policy "allow delete for post owners" on posts for | ||
delete | ||
using (auth.uid() = user_id); | ||
|
||
-- function to update the updated_at timestamp | ||
create | ||
or replace function update_updated_at() returns trigger as $$ begin | ||
new .updated_at = now(); | ||
|
||
return new; | ||
|
||
end; | ||
|
||
$$ language plpgsql; | ||
|
||
-- trigger to call the update_updated_at function | ||
create trigger update_posts_updated_at before | ||
update | ||
on posts for each row execute function update_updated_at(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
INSERT INTO | ||
auth.users ( | ||
instance_id, | ||
id, | ||
aud, | ||
role, | ||
email, | ||
encrypted_password, | ||
email_confirmed_at, | ||
invited_at, | ||
confirmation_token, | ||
confirmation_sent_at, | ||
recovery_token, | ||
recovery_sent_at, | ||
email_change_token_new, | ||
email_change, | ||
email_change_sent_at, | ||
last_sign_in_at, | ||
raw_app_meta_data, | ||
raw_user_meta_data, | ||
is_super_admin, | ||
created_at, | ||
updated_at, | ||
phone, | ||
phone_confirmed_at, | ||
phone_change, | ||
phone_change_token, | ||
phone_change_sent_at, | ||
email_change_token_current, | ||
email_change_confirm_status, | ||
banned_until, | ||
reauthentication_token, | ||
reauthentication_sent_at, | ||
is_sso_user, | ||
deleted_at, | ||
is_anonymous | ||
) | ||
VALUES | ||
( | ||
'00000000-0000-0000-0000-000000000000', | ||
'aec53558-767e-4408-b4d6-1c1e6f17ffe5', | ||
'authenticated', | ||
'authenticated', | ||
'[email protected]', | ||
'$2a$10$nnqTShcTX48N6QWWjbPUee.wrGz1kGx/uq5lORviCm.fn04W1BeRe', | ||
'2024-09-01 17:21:01.462788+00', | ||
NULL, | ||
'', | ||
NULL, | ||
'', | ||
NULL, | ||
'', | ||
'', | ||
NULL, | ||
NULL, | ||
'{"provider": "email", "providers": ["email"]}', | ||
'{"username": "username", "full_name": "Test User"}', | ||
NULL, | ||
'2024-09-01 17:21:01.455486+00', | ||
'2024-09-01 17:21:01.46295+00', | ||
NULL, | ||
NULL, | ||
'', | ||
'', | ||
NULL, | ||
'', | ||
0, | ||
NULL, | ||
'', | ||
NULL, | ||
false, | ||
NULL, | ||
false | ||
); | ||
|
||
INSERT INTO | ||
auth.identities ( | ||
provider_id, | ||
user_id, | ||
identity_data, | ||
provider, | ||
last_sign_in_at, | ||
created_at, | ||
updated_at, | ||
id | ||
) | ||
VALUES | ||
( | ||
'aec53558-767e-4408-b4d6-1c1e6f17ffe5', | ||
'aec53558-767e-4408-b4d6-1c1e6f17ffe5', | ||
'{"sub": "aec53558-767e-4408-b4d6-1c1e6f17ffe5", "email": "[email protected]", "email_verified": false, "phone_verified": false}', | ||
'email', | ||
'2024-09-01 17:21:01.459821+00', | ||
'2024-09-01 17:21:01.459849+00', | ||
'2024-09-01 17:21:01.459849+00', | ||
'c5e81668-437b-47c2-83e2-84b8566b3018' | ||
); | ||
|
||
-- Seed data for posts | ||
INSERT INTO | ||
posts (user_id, title, content) | ||
VALUES | ||
( | ||
'aec53558-767e-4408-b4d6-1c1e6f17ffe5', | ||
'React Server Components: A Game Changer', | ||
'React Server Components are revolutionizing how we build React applications. They allow for better performance and smaller bundle sizes by running components on the server. This new paradigm is especially powerful when combined with frameworks like Next.js 13+.' | ||
), | ||
( | ||
'aec53558-767e-4408-b4d6-1c1e6f17ffe5', | ||
'The Rise of Bun: A New JavaScript Runtime', | ||
'Bun is gaining traction as a fast all-in-one JavaScript runtime. It aims to replace Node.js, npm, yarn, and more. With its focus on performance and developer experience, Bun is definitely worth keeping an eye on in 2024.' | ||
), | ||
( | ||
'aec53558-767e-4408-b4d6-1c1e6f17ffe5', | ||
'TypeScript 5.0: What''s New and Exciting', | ||
'TypeScript 5.0 brings several new features and improvements, including decorators, const type parameters, and more. These enhancements continue to make TypeScript an essential tool for building robust JavaScript applications.' | ||
), | ||
( | ||
'aec53558-767e-4408-b4d6-1c1e6f17ffe5', | ||
'The State of JavaScript Frameworks in 2024', | ||
'While React remains dominant, frameworks like Svelte and Solid are gaining popularity for their performance and simplicity. Meanwhile, meta-frameworks like Next.js and Remix are becoming increasingly important in the React ecosystem.' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// security: allows all users emails to be read. very bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xyzeva I agree. I think the email doesn't need to be on this table at all actually, but was following the user update schema.