#Commit small change to schema to production
1 messages · Page 1 of 1 (latest)
To commit a small change to your schema in production, you would typically follow these steps:
- Make the changes in your development environment first. This could be adding a new table or column, or modifying an existing one. For instance, if you're adding a new optional field to an existing table, your code might look something like this:
// schema.ts
rank: v.optional(v.number()),
-
Test your changes thoroughly in the development environment to ensure they work as expected.
-
Once you're ready, deploy your changes to production. Convex will handle the process of updating the database schema. If there's a problem, such as trying to add a new, empty column and making it required, the deploy will fail.
-
If your changes involve adding a new required field, you might need to do two deploys. The first deploy would add the field as optional. Then, you would run a migration function to populate the field. Finally, you would deploy a second time to make the field required.
Here's an example of a migration function:
export const addRankToImages = migration({
table: "images",
migrateOne: async (ctx, image) => {
if (!image.rank) {
await ctx.db.patch(image._id, { rank: 0 })
}
},
})
Remember, Convex enforces schema constraints, so your schema must always match existing data. You cannot push a schema to a deployment with existing data that doesn't match it, unless you turn off schema enforcement.
For more information, you can refer to the Convex documentation on schemas (https://docs.convex.dev/database/schemas) and the discussion on making safe changes (https://docs.convex.dev/production#making-safe-changes).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@peak shuttle kapa.ai is still learning and improving, please let us know how it did by reacting below