Skip to content

Commit 67e12df

Browse files
authored
docs: improve database migrations guide (supabase#30914)
1 parent 731a23d commit 67e12df

File tree

6 files changed

+125
-68
lines changed

6 files changed

+125
-68
lines changed

apps/docs/content/guides/deployment/database-migrations.mdx

Lines changed: 86 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@ id: 'database-migrations'
33
title: 'Database Migrations'
44
description: 'How to manage schema migrations for your Supabase project.'
55
subtitle: 'How to manage schema migrations for your Supabase project.'
6-
video: 'https://www.youtube-nocookie.com/v/vyHyYpvjaks'
7-
tocVideo: 'vyHyYpvjaks'
6+
video: 'https://www.youtube-nocookie.com/v/Kx5nHBmIxyQ'
7+
tocVideo: 'Kx5nHBmIxyQ'
88
---
99

10-
Database schema changes are managed through "migrations". Database migrations are a common way of tracking changes to your database over time.
10+
Database migrations are SQL statements that create, update, or delete your existing database schemas. They are a common way of tracking changes to your database over time.
1111

1212
## Schema migrations
1313

1414
For this guide, we'll create a table called `employees` and see how we can make changes to it.
1515

16+
You will need to [install](/docs/guides/local-development#quickstart) the Supabase CLI and start the local development stack.
17+
1618
<StepHikeCompact>
1719

1820
<StepHikeCompact.Step step={1}>
1921
<StepHikeCompact.Details title="Create your first migration file">
20-
2122
To get started, generate a [new migration](/docs/reference/cli/supabase-migration-new) to store the SQL needed to create our `employees` table.
22-
2323
</StepHikeCompact.Details>
2424

2525
<StepHikeCompact.Code>
2626

27-
```bash
27+
```bash Terminal
2828
supabase migration new create_employees_table
2929
```
3030

@@ -37,18 +37,17 @@ supabase migration new create_employees_table
3737

3838
<StepHikeCompact.Step step={2}>
3939
<StepHikeCompact.Details title="Add the SQL to your migration file">
40-
This creates a new migration: supabase/migrations/\<timestamp\>
41-
_create_employees_table.sql.
40+
This creates a new migration file in supabase/migrations directory.
4241

43-
To that file, add the SQL to create this `employees` table
42+
To that file, add the SQL to create this `employees` table.
4443
</StepHikeCompact.Details>
4544

4645
<StepHikeCompact.Code>
4746

48-
```sql
49-
create table employees (
47+
```sql supabase/migrations/<timestamp>_create_employees_table.sql
48+
create table if not exists employees (
5049
id bigint primary key generated always as identity,
51-
name text,
50+
name text not null,
5251
email text,
5352
created_at timestamptz default now()
5453
);
@@ -62,16 +61,16 @@ create table employees (
6261
<StepHikeCompact>
6362

6463
<StepHikeCompact.Step step={3}>
65-
<StepHikeCompact.Details title="Apply your migration">
66-
Now that you have a migration file, you can run this migration and create the `employees` table.
64+
<StepHikeCompact.Details title="Apply your first migration">
65+
Run this migration to create the `employees` table.
6766

68-
Use the `reset` command here to reset the database to the current migrations
67+
Now you can visit your new `employees` table in the local Dashboard.
6968
</StepHikeCompact.Details>
7069

7170
<StepHikeCompact.Code>
7271

73-
```bash
74-
supabase db reset
72+
```bash Terminal
73+
supabase migration up
7574
```
7675

7776
</StepHikeCompact.Code>
@@ -83,15 +82,13 @@ supabase db reset
8382

8483
<StepHikeCompact.Step step={4}>
8584
<StepHikeCompact.Details title="Modify your employees table">
86-
Now you can visit your new `employees` table in the Dashboard.
87-
88-
Next, modify your `employees` table by adding a column for department. Create a new migration file for that.
85+
Next, modify your `employees` table by adding a column for `department`.
8986
</StepHikeCompact.Details>
9087

9188
<StepHikeCompact.Code>
9289

93-
```bash
94-
supabase migration new add_department_to_employees_table
90+
```bash Terminal
91+
supabase migration new add_department_column
9592
```
9693

9794
</StepHikeCompact.Code>
@@ -103,15 +100,12 @@ supabase migration new add_department_to_employees_table
103100

104101
<StepHikeCompact.Step step={5}>
105102
<StepHikeCompact.Details title="Add a new column to your table">
106-
This creates a new migration file: supabase/migrations/\<timestamp\>
107-
_add_department_to_employees_table.sql.
108-
109-
To that file, add the SQL to create a new department column
103+
To that new migration file, add the SQL to create a new `department` column.
110104
</StepHikeCompact.Details>
111105

112106
<StepHikeCompact.Code>
113107

114-
```sql
108+
```sql supabase/migrations/<timestamp>_add_department_column.sql
115109
alter table if exists public.employees
116110
add department text default 'Hooli';
117111
```
@@ -121,22 +115,44 @@ add department text default 'Hooli';
121115
</StepHikeCompact.Step>
122116
</StepHikeCompact>
123117

124-
### Add sample data
118+
<StepHikeCompact>
125119

126-
Now that you are managing your database with migrations scripts, it would be great have some seed data to use every time you reset the database.
120+
<StepHikeCompact.Step step={6}>
121+
<StepHikeCompact.Details title="Apply your second migration">
122+
Run this migration to update your existing `employees` table.
123+
</StepHikeCompact.Details>
127124

128-
For this, you can create a seed script in `supabase/seed.sql`.
125+
<StepHikeCompact.Code>
126+
127+
```bash Terminal
128+
supabase migration up
129+
```
130+
131+
</StepHikeCompact.Code>
132+
133+
</StepHikeCompact.Step>
134+
</StepHikeCompact>
135+
136+
Finally, you should see the `department` column added to your `employees` table in the local Dashboard.
137+
138+
View the [complete code](https://github.com/supabase/supabase/tree/master/examples/database/employees) for this example.
139+
140+
### Seeding data
141+
142+
Now that you are managing your database with migrations scripts, it would be great have some seed data to use every time you reset the database.
129143

130144
<StepHikeCompact>
131145

132146
<StepHikeCompact.Step step={1}>
133147
<StepHikeCompact.Details title="Populate your table">
134-
Insert data into your `employees` table with your `supabase/seed.sql` file.
148+
Create a seed script in supabase/seed.sql.
149+
150+
To that file, add the SQL to insert data into your `employees` table.
135151
</StepHikeCompact.Details>
136152

137153
<StepHikeCompact.Code>
138154

139-
```sql
155+
```sql supabase/seed.sql
140156
insert into public.employees
141157
(name)
142158
values
@@ -154,7 +170,7 @@ values
154170

155171
<StepHikeCompact.Step step={2}>
156172
<StepHikeCompact.Details title="Reset your database">
157-
Reset your database (apply current migrations), and populate with seed data
173+
Reset your database to reapply migrations and populate with seed data.
158174
</StepHikeCompact.Details>
159175

160176
<StepHikeCompact.Code>
@@ -198,60 +214,62 @@ The last step is deploying these changes to a live Supabase project.
198214

199215
## Deploy your project
200216

201-
You've been developing your project locally, making changes to your tables via migrations. It's time to deploy your project to the Supabase Platform and start scaling up to millions of users! Head over to [Supabase](https://supabase.com/dashboard) and create a new project to deploy to.
217+
You've been developing your project locally, making changes to your tables via migrations. It's time to deploy your project to the Supabase Platform and start scaling up to millions of users!
202218

203-
### Log in to the Supabase CLI
219+
Head over to [Supabase](https://supabase.com/dashboard) and create a new project to deploy to.
204220

205-
<CH.Code>
221+
<StepHikeCompact>
222+
223+
<StepHikeCompact.Step step={1}>
224+
<StepHikeCompact.Details title="Log in to the Supabase CLI">
225+
[Login](/docs/reference/cli/usage#supabase-login) to the Supabase CLI using an auto-generated Personal Access Token.
226+
</StepHikeCompact.Details>
227+
228+
<StepHikeCompact.Code>
206229

207230
```bash Terminal
208231
supabase login
209232
```
210233

211-
```bash npx
212-
npx supabase login
213-
```
214-
215-
</CH.Code>
216-
217-
### Link your project
218-
219-
Associate your project with your remote project using [`supabase link`](/docs/reference/cli/usage#supabase-link).
220-
221-
```bash
222-
supabase link --project-ref <project-id>
223-
# You can get <project-id> from your project's dashboard URL: https://supabase.com/dashboard/project/<project-id>
234+
</StepHikeCompact.Code>
224235

225-
supabase db pull
226-
# Capture any changes that you have made to your remote database before you went through the steps above
227-
# If you have not made any changes to the remote database, skip this step
228-
```
236+
</StepHikeCompact.Step>
237+
</StepHikeCompact>
229238

230-
`supabase/migrations` is now populated with a migration in `<timestamp>_remote_schema.sql`.
231-
This migration captures any changes required for your local database to match the schema of your remote Supabase project.
239+
<StepHikeCompact>
232240

233-
Review the generated migration file and once happy, apply the changes to your local instance:
241+
<StepHikeCompact.Step step={2}>
242+
<StepHikeCompact.Details title="Link your project">
243+
[Link](/docs/reference/cli/usage#supabase-link) to your remote project by selecting from the on-screen prompt.
244+
</StepHikeCompact.Details>
234245

235-
```bash
236-
# To apply the new migration to your local database:
237-
supabase migration up
246+
<StepHikeCompact.Code>
238247

239-
# To reset your local database completely:
240-
supabase db reset
248+
```bash Terminal
249+
supabase link
241250
```
242251

243-
<Admonition type="note">
252+
</StepHikeCompact.Code>
244253

245-
There are a few commands required to link your project. We are in the process of consolidating these commands into a single command. Bear with us!
254+
</StepHikeCompact.Step>
255+
</StepHikeCompact>
246256

247-
</Admonition>
257+
<StepHikeCompact>
248258

249-
### Deploy database changes
259+
<StepHikeCompact.Step step={3}>
260+
<StepHikeCompact.Details title="Deploy database changes">
261+
[Push](/docs/reference/cli/usage#supabase-db-push) your migrations to the remote database.
262+
</StepHikeCompact.Details>
250263

251-
Deploy any local database migrations using [`db push`](/docs/reference/cli/usage#supabase-db-push):
264+
<StepHikeCompact.Code>
252265

253-
```sh
266+
```bash Terminal
254267
supabase db push
255268
```
256269

257-
Visiting your live project on [Supabase](https://supabase.com/dashboard), you'll see a new `employees` table, complete with the `department` column you added in the second migration above.
270+
</StepHikeCompact.Code>
271+
272+
</StepHikeCompact.Step>
273+
</StepHikeCompact>
274+
275+
Visiting your live project on [Supabase](https://supabase.com/dashboard/project/_), you'll see a new `employees` table, complete with the `department` column you added in the second migration above.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Supabase
2+
.branches
3+
.temp
4+
.env
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# For detailed configuration reference documentation, visit:
2+
# https://supabase.com/docs/guides/local-development/cli/config
3+
# A string used to distinguish different Supabase projects on the same host. Defaults to the
4+
# working directory name when running `supabase init`.
5+
project_id = "employees"
6+
7+
[db]
8+
# Port to use for the local database URL.
9+
port = 54322
10+
# Port used by db diff command to initialize the shadow database.
11+
shadow_port = 54320
12+
# The database major version to use. This has to be the same as your remote database's. Run `SHOW
13+
# server_version;` on the remote database to check.
14+
major_version = 15
15+
16+
[db.seed]
17+
# If enabled, seeds the database after migrations during a db reset.
18+
enabled = true
19+
# Specifies an ordered list of seed files to load during db reset.
20+
# Supports glob patterns relative to supabase directory: `./seeds/*.sql'
21+
sql_paths = ['./seed.sql']
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
create table if not exists employees (
2+
id bigint primary key generated always as identity,
3+
name text not null,
4+
email text,
5+
created_at timestamptz default now()
6+
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
alter table if exists public.employees
2+
add department text default 'Hooli';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
insert into public.employees
2+
(name)
3+
values
4+
('Erlich Bachman'),
5+
('Richard Hendricks'),
6+
('Monica Hall');

0 commit comments

Comments
 (0)