Skip to content

Commit 42c4c91

Browse files
authored
Merge pull request #26 from midday-ai/bryce/migrations-seed
Migrations seed
2 parents 6ed9c03 + 4fd5e3f commit 42c4c91

File tree

21 files changed

+478
-44
lines changed

21 files changed

+478
-44
lines changed

.github/workflows/check.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Check
2+
on:
3+
push:
4+
paths:
5+
- '**'
6+
jobs:
7+
check:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
- uses: oven-sh/setup-bun@v1
12+
with:
13+
bun-version: latest
14+
- name: Install dependencies
15+
run: bun install
16+
- name: 🔦 Run linter
17+
run: bun run lint
18+
- name: 🪐 Check TypeScript
19+
run: bun run typecheck

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ bun dev:web // starts the web app in development mode
109109
bun dev:app // starts the app in development mode
110110
bun dev:api // starts the api in development mode
111111
bun dev:email // starts the email app in development mode
112+
113+
// Database
114+
bun migrate // run migrations
115+
bun seed // run seed
112116
```
113117

114118
## How to use

apps/api/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
"private": true,
44
"scripts": {
55
"dev": "supabase start",
6-
"login": "supabase login"
6+
"login": "supabase login",
7+
"migrate": "supabase migration up",
8+
"seed": "supabase db seed generate && supabase db seed run",
9+
"reset": "supabase db reset",
10+
"generate": "supabase gen types --lang=typescript --local --schema public > ../../packages/supabase/src/types/db.ts"
711
},
812
"dependencies": {
913
"supabase": "^1.191.3"

apps/api/supabase/migrations/.gitkeep

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
create function public.handle_new_user() returns trigger
2+
language plpgsql security definer set search_path = ''
3+
as $$
4+
begin
5+
insert into public.users (id, email, full_name)
6+
values (
7+
new.id,
8+
new.email,
9+
new.raw_user_meta_data ->> 'full_name'
10+
);
11+
return new;
12+
end;
13+
$$;
14+
15+
-- trigger the function every time a user is created
16+
create trigger on_auth_user_created
17+
after insert on auth.users
18+
for each row
19+
execute function public.handle_new_user();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- create users table
2+
create table public.users (
3+
id uuid primary key,
4+
email text unique not null,
5+
full_name text,
6+
avatar_url text,
7+
created_at timestamp with time zone default now(),
8+
updated_at timestamp with time zone default now(),
9+
constraint fk_auth_user foreign key (id) references auth.users(id) on delete cascade
10+
);
11+
12+
-- enable row level security (rls)
13+
alter table public.users enable row level security;
14+
15+
-- create a trigger to update the updated_at column
16+
create or replace function update_updated_at()
17+
returns trigger as $$
18+
begin
19+
new.updated_at = now();
20+
return new;
21+
end;
22+
$$ language plpgsql;
23+
24+
create trigger users_updated_at
25+
before update on public.users
26+
for each row
27+
execute function update_updated_at();
28+
29+
-- create a policy to allow users to read their own profile
30+
create policy select_own_profile on public.users
31+
for select using (auth.uid() = id);
32+
33+
-- create a policy to allow users to update their own profile
34+
create policy update_own_profile on public.users
35+
for update using (auth.uid() = id);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
-- create posts table
2+
create table posts (
3+
id uuid primary key default gen_random_uuid(),
4+
user_id uuid not null,
5+
title text not null,
6+
content text not null,
7+
created_at timestamptz not null default now(),
8+
updated_at timestamptz not null default now()
9+
);
10+
11+
-- add foreign key constraint
12+
alter table
13+
posts
14+
add
15+
constraint fk_posts_user foreign key (user_id) references public.users(id) on
16+
delete
17+
cascade;
18+
19+
-- create index for faster queries
20+
create index idx_posts_user_id on posts(user_id);
21+
22+
-- add rls policies
23+
alter table
24+
posts enable row level security;
25+
26+
-- policy to allow read access for all authenticated users
27+
create policy "allow read access for all authenticated users" on posts for
28+
select
29+
to authenticated
30+
using (true);
31+
32+
-- policy to allow users to insert their own posts
33+
create policy "allow insert for authenticated users" on posts for
34+
insert
35+
with check (auth.uid() = user_id);
36+
37+
-- policy to allow users to update their own posts
38+
create policy "allow update for post owners" on posts for
39+
update
40+
using (auth.uid() = user_id);
41+
42+
-- policy to allow users to delete their own posts
43+
create policy "allow delete for post owners" on posts for
44+
delete
45+
using (auth.uid() = user_id);
46+
47+
-- function to update the updated_at timestamp
48+
create
49+
or replace function update_updated_at() returns trigger as $$ begin
50+
new.updated_at = now();
51+
52+
return new;
53+
54+
end;
55+
56+
$$ language plpgsql;
57+
58+
-- trigger to call the update_updated_at function
59+
create trigger update_posts_updated_at before
60+
update
61+
on posts for each row execute function update_updated_at();

apps/api/supabase/seed.sql

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
INSERT INTO
2+
auth.users (
3+
instance_id,
4+
id,
5+
aud,
6+
role,
7+
email,
8+
encrypted_password,
9+
email_confirmed_at,
10+
invited_at,
11+
confirmation_token,
12+
confirmation_sent_at,
13+
recovery_token,
14+
recovery_sent_at,
15+
email_change_token_new,
16+
email_change,
17+
email_change_sent_at,
18+
last_sign_in_at,
19+
raw_app_meta_data,
20+
raw_user_meta_data,
21+
is_super_admin,
22+
created_at,
23+
updated_at,
24+
phone,
25+
phone_confirmed_at,
26+
phone_change,
27+
phone_change_token,
28+
phone_change_sent_at,
29+
email_change_token_current,
30+
email_change_confirm_status,
31+
banned_until,
32+
reauthentication_token,
33+
reauthentication_sent_at,
34+
is_sso_user,
35+
deleted_at,
36+
is_anonymous
37+
)
38+
VALUES
39+
(
40+
'00000000-0000-0000-0000-000000000000',
41+
'aec53558-767e-4408-b4d6-1c1e6f17ffe5',
42+
'authenticated',
43+
'authenticated',
44+
45+
'$2a$10$nnqTShcTX48N6QWWjbPUee.wrGz1kGx/uq5lORviCm.fn04W1BeRe',
46+
'2024-09-01 17:21:01.462788+00',
47+
NULL,
48+
'',
49+
NULL,
50+
'',
51+
NULL,
52+
'',
53+
'',
54+
NULL,
55+
NULL,
56+
'{"provider": "email", "providers": ["email"]}',
57+
'{"username": "username", "full_name": "Test User"}',
58+
NULL,
59+
'2024-09-01 17:21:01.455486+00',
60+
'2024-09-01 17:21:01.46295+00',
61+
NULL,
62+
NULL,
63+
'',
64+
'',
65+
NULL,
66+
'',
67+
0,
68+
NULL,
69+
'',
70+
NULL,
71+
false,
72+
NULL,
73+
false
74+
);
75+
76+
INSERT INTO
77+
auth.identities (
78+
provider_id,
79+
user_id,
80+
identity_data,
81+
provider,
82+
last_sign_in_at,
83+
created_at,
84+
updated_at,
85+
id
86+
)
87+
VALUES
88+
(
89+
'aec53558-767e-4408-b4d6-1c1e6f17ffe5',
90+
'aec53558-767e-4408-b4d6-1c1e6f17ffe5',
91+
'{"sub": "aec53558-767e-4408-b4d6-1c1e6f17ffe5", "email": "[email protected]", "email_verified": false, "phone_verified": false}',
92+
'email',
93+
'2024-09-01 17:21:01.459821+00',
94+
'2024-09-01 17:21:01.459849+00',
95+
'2024-09-01 17:21:01.459849+00',
96+
'c5e81668-437b-47c2-83e2-84b8566b3018'
97+
);
98+
99+
-- Seed data for posts
100+
INSERT INTO
101+
posts (user_id, title, content)
102+
VALUES
103+
(
104+
'aec53558-767e-4408-b4d6-1c1e6f17ffe5',
105+
'React Server Components: A Game Changer',
106+
'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+.'
107+
),
108+
(
109+
'aec53558-767e-4408-b4d6-1c1e6f17ffe5',
110+
'The Rise of Bun: A New JavaScript Runtime',
111+
'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.'
112+
),
113+
(
114+
'aec53558-767e-4408-b4d6-1c1e6f17ffe5',
115+
'TypeScript 5.0: What''s New and Exciting',
116+
'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.'
117+
),
118+
(
119+
'aec53558-767e-4408-b4d6-1c1e6f17ffe5',
120+
'The State of JavaScript Frameworks in 2024',
121+
'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.'
122+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PostsLoading } from "@/components/posts/posts.loading";
2+
import { PostsServer } from "@/components/posts/posts.server";
3+
import { Suspense } from "react";
4+
5+
export const metadata = {
6+
title: "Posts",
7+
};
8+
9+
export default function Page() {
10+
return (
11+
<Suspense fallback={<PostsLoading />}>
12+
<PostsServer />
13+
</Suspense>
14+
);
15+
}

apps/app/src/app/[locale]/(dashboard)/settings/page.tsx

Lines changed: 0 additions & 19 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export function UsersLoading() {
1+
export function PostsLoading() {
22
return <div>Loading...</div>;
33
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getPosts } from "@v1/supabase/queries";
2+
3+
export async function PostsServer() {
4+
const { data } = await getPosts();
5+
6+
return (
7+
<div>
8+
{data?.map((post) => (
9+
<div key={post.id}>{post.title}</div>
10+
))}
11+
</div>
12+
);
13+
}

apps/app/src/components/users.server.tsx

Lines changed: 0 additions & 13 deletions
This file was deleted.

bun.lockb

-1.82 KB
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"lint:repo:fix": "bunx sherif@latest --fix",
2020
"typecheck": "turbo typecheck"
2121
},
22-
"devDependencies": {
22+
"dependencies": {
2323
"@biomejs/biome": "1.8.3",
2424
"@manypkg/cli": "^0.21.4",
2525
"@t3-oss/env-nextjs": "^0.11.1",

packages/supabase/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"clean": "rm -rf .turbo node_modules",
77
"lint": "biome check .",
88
"format": "biome format --write .",
9-
"typecheck": "tsc --noEmit",
10-
"db:generate": "supabase gen types --lang=typescript --project-id $PROJECT_ID --schema public > src/types/db.ts"
9+
"typecheck": "tsc --noEmit"
1110
},
1211
"dependencies": {
1312
"@supabase/ssr": "^0.5.1",

packages/supabase/src/mutations/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { logger } from "@v1/logger";
22
import { createClient } from "@v1/supabase/server";
3+
import type { Database, Tables, TablesUpdate } from "../types";
34

4-
export async function updateUser(userId: string, data: unknown) {
5+
export async function updateUser(userId: string, data: TablesUpdate<"users">) {
56
const supabase = createClient();
67

78
try {

0 commit comments

Comments
 (0)