-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: automatic database migrations (#333)
Co-authored-by: Sébastien Chopin <seb@nuxt.com>
- Loading branch information
1 parent
bd47e98
commit dc33086
Showing
17 changed files
with
442 additions
and
79 deletions.
There are no files selected for viewing
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
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
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,88 @@ | ||
--- | ||
title: Automatic Database Migrations | ||
description: "Database migrations now automatically apply during development and deployment." | ||
date: 2024-10-25 | ||
image: '/images/changelog/database-migrations.png' | ||
category: Core | ||
authors: | ||
- name: Rihan Arfan | ||
avatar: | ||
src: https://avatars.githubusercontent.com/u/20425781?v=4 | ||
to: https://x.com/RihanArfan | ||
username: RihanArfan | ||
- name: Sebastien Chopin | ||
avatar: | ||
src: https://avatars.githubusercontent.com/u/904724?v=4 | ||
to: https://x.com/atinux | ||
username: atinux | ||
--- | ||
|
||
::tip | ||
This feature is available on both [free and pro plans](/pricing) starting with [`@nuxthub/core >= v0.8.0`](https://github.com/nuxt-hub/core/releases). | ||
:: | ||
|
||
We're excited to introduce automatic [database migrations](/docs/features/database#migrations) in NuxtHub. | ||
|
||
### Automatic Migration Application | ||
|
||
SQL migrations in `server/database/migrations/*.sql` now automatically apply when you: | ||
- Start the development server (`npx nuxt dev` or [`npx nuxt dev --remote`](/docs/getting-started/remote-storage)) | ||
- Preview builds locally ([`npx nuxthub preview`](/changelog/nuxthub-preview)) | ||
- Deploy via [`npx nuxthub deploy`](/docs/getting-started/deploy#nuxthub-cli) or [Cloudflare Pages CI](/docs/getting-started/deploy#cloudflare-pages-ci) | ||
|
||
Starting now, when you clone any of [our templates](/templates) with a database, all migrations apply automatically! | ||
|
||
::note{to="/docs/features/database#migrations"} | ||
Learn more about database migrations in our **full documentation**. | ||
:: | ||
|
||
## New CLI Commands | ||
|
||
[`nuxthub@0.7.0`](https://github.com/nuxt-hub/cli) introduces these database migration commands: | ||
|
||
```bash [Terminal] | ||
# Create a new migration | ||
npx nuxthub database migrations create <name> | ||
|
||
# View migration status | ||
npx nuxthub database migrations list | ||
|
||
# Mark all migrations as applied | ||
npx nuxthub database migrations mark-all-applied | ||
``` | ||
|
||
Learn more about: | ||
- [Creating migrations](/docs/features/database#creating-migrations) | ||
- [Checking migration status](/docs/features/database#checking-migration-status) | ||
- [Marking migrations as applied](/docs/features/database#marking-migrations-as-applied) | ||
|
||
## Migrating from Existing ORMs | ||
|
||
::important | ||
**Current Drizzle ORM users:** Follow these specific migration steps. | ||
:: | ||
|
||
Since NuxtHub doesn't recognize previously applied Drizzle ORM migrations (stored in `__drizzle_migrations`), it will attempt to rerun all migrations in `server/database/migrations/*.sql`. To prevent this: | ||
|
||
1. Mark existing migrations as applied in each environment: | ||
|
||
```bash [Terminal] | ||
# Local environment | ||
npx nuxthub database migrations mark-all-applied | ||
|
||
# Preview environment | ||
npx nuxthub database migrations mark-all-applied --preview | ||
|
||
# Production environment | ||
npx nuxthub database migrations mark-all-applied --production | ||
``` | ||
|
||
2. Remove `server/plugins/database.ts` as it's no longer needed. | ||
## Understanding Database Migrations | ||
Database migrations provide version control for your database schema. They track changes and ensure consistent schema evolution across all environments through incremental updates. | ||
::note | ||
Implemented in [nuxt-hub/core#333](https://github.com/nuxt-hub/core/pull/333) and [nuxt-hub/cli#31](https://github.com/nuxt-hub/cli/pull/31). | ||
:: |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,7 @@ | ||
-- Migration number: 0001 2024-10-24T00:25:12.371Z | ||
CREATE TABLE todos ( | ||
id integer PRIMARY KEY NOT NULL, | ||
title text NOT NULL, | ||
completed integer DEFAULT 0 NOT NULL, | ||
created_at integer NOT NULL | ||
); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.