You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
11
11
12
12
## Schema migrations
13
13
14
14
For this guide, we'll create a table called `employees` and see how we can make changes to it.
15
15
16
+
You will need to [install](/docs/guides/local-development#quickstart) the Supabase CLI and start the local development stack.
17
+
16
18
<StepHikeCompact>
17
19
18
20
<StepHikeCompact.Stepstep={1}>
19
21
<StepHikeCompact.Detailstitle="Create your first migration file">
20
-
21
22
To get started, generate a [new migration](/docs/reference/cli/supabase-migration-new) to store the SQL needed to create our `employees` table.
22
-
23
23
</StepHikeCompact.Details>
24
24
25
25
<StepHikeCompact.Code>
26
26
27
-
```bash
27
+
```bash Terminal
28
28
supabase migration new create_employees_table
29
29
```
30
30
@@ -37,18 +37,17 @@ supabase migration new create_employees_table
37
37
38
38
<StepHikeCompact.Stepstep={2}>
39
39
<StepHikeCompact.Detailstitle="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.
42
41
43
-
To that file, add the SQL to create this `employees` table
42
+
To that file, add the SQL to create this `employees` table.
@@ -121,22 +115,44 @@ add department text default 'Hooli';
121
115
</StepHikeCompact.Step>
122
116
</StepHikeCompact>
123
117
124
-
### Add sample data
118
+
<StepHikeCompact>
125
119
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.Stepstep={6}>
121
+
<StepHikeCompact.Detailstitle="Apply your second migration">
122
+
Run this migration to update your existing `employees` table.
123
+
</StepHikeCompact.Details>
127
124
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.
129
143
130
144
<StepHikeCompact>
131
145
132
146
<StepHikeCompact.Stepstep={1}>
133
147
<StepHikeCompact.Detailstitle="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.
135
151
</StepHikeCompact.Details>
136
152
137
153
<StepHikeCompact.Code>
138
154
139
-
```sql
155
+
```sql supabase/seed.sql
140
156
insert intopublic.employees
141
157
(name)
142
158
values
@@ -154,7 +170,7 @@ values
154
170
155
171
<StepHikeCompact.Stepstep={2}>
156
172
<StepHikeCompact.Detailstitle="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.
158
174
</StepHikeCompact.Details>
159
175
160
176
<StepHikeCompact.Code>
@@ -198,60 +214,62 @@ The last step is deploying these changes to a live Supabase project.
198
214
199
215
## Deploy your project
200
216
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!
202
218
203
-
### Log in to the Supabase CLI
219
+
Head over to [Supabase](https://supabase.com/dashboard) and create a new project to deploy to.
204
220
205
-
<CH.Code>
221
+
<StepHikeCompact>
222
+
223
+
<StepHikeCompact.Stepstep={1}>
224
+
<StepHikeCompact.Detailstitle="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>
206
229
207
230
```bash Terminal
208
231
supabase login
209
232
```
210
233
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>
224
235
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>
229
238
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>
232
240
233
-
Review the generated migration file and once happy, apply the changes to your local instance:
241
+
<StepHikeCompact.Stepstep={2}>
242
+
<StepHikeCompact.Detailstitle="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>
234
245
235
-
```bash
236
-
# To apply the new migration to your local database:
237
-
supabase migration up
246
+
<StepHikeCompact.Code>
238
247
239
-
# To reset your local database completely:
240
-
supabase db reset
248
+
```bash Terminal
249
+
supabase link
241
250
```
242
251
243
-
<Admonitiontype="note">
252
+
</StepHikeCompact.Code>
244
253
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!
[Push](/docs/reference/cli/usage#supabase-db-push) your migrations to the remote database.
262
+
</StepHikeCompact.Details>
250
263
251
-
Deploy any local database migrations using [`db push`](/docs/reference/cli/usage#supabase-db-push):
264
+
<StepHikeCompact.Code>
252
265
253
-
```sh
266
+
```bash Terminal
254
267
supabase db push
255
268
```
256
269
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.
0 commit comments