Skip to content

Commit f8b3ff7

Browse files
committed
docs: simplify migrations using drizzle
1 parent ed57a6e commit f8b3ff7

File tree

1 file changed

+15
-37
lines changed

1 file changed

+15
-37
lines changed

docs/content/docs/3.recipes/2.drizzle.md

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -91,57 +91,35 @@ When running the `npm run db:generate` command, `drizzle-kit` will generate the
9191

9292
### Migrations
9393

94-
::important
95-
We are planning to update this section to leverage [Nitro Tasks](https://nitro.unjs.io/guide/tasks) instead of a server plugin very soon.
96-
::
97-
9894
We can create a server plugin to run the migrations in development automatically:
9995

10096
```ts [server/plugins/migrations.ts]
10197
import { consola } from 'consola'
98+
import { migrate } from 'drizzle-orm/d1/migrator'
10299

103100
export default defineNitroPlugin(async () => {
104101
if (!import.meta.dev) return
105102

106103
onHubReady(async () => {
107-
const migrationsStorage = useStorage('root/server/database/migrations')
108-
let migrationFiles = await migrationsStorage.getKeys()
109-
migrationFiles = migrationFiles.filter(key => key.endsWith('.sql'))
110-
111-
const database = hubDatabase()
112-
113-
// Make sure to create the _hub_migrations table if it doesn't exist
114-
await database.prepare('CREATE TABLE IF NOT EXISTS _hub_migrations (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, created_at INTEGER NOT NULL)').run()
115-
116-
// Get applied migrations from database
117-
const hubMigrations = await database.prepare('SELECT * FROM _hub_migrations').all()
118-
const appliedMigrations = (hubMigrations.results || []).map((row) => row.name)
119-
const missingMigrations = migrationFiles.filter(file => !appliedMigrations.includes(file))
120-
if (!missingMigrations.length) {
121-
consola.success('Database up-to-date and ready')
122-
return
123-
}
124-
125-
// Apply missing migrations
126-
const appliedMigrationsStmts = []
127-
for (const file of missingMigrations) {
128-
consola.info(`Applying database migrations from ${file}...`)
129-
const migration = (await migrationsStorage.getItem<string>(file)) || ''
130-
const statements = migration.split('--> statement-breakpoint')
131-
for (let stmt of statements) {
132-
await database.prepare(stmt.trim()).run()
133-
}
134-
appliedMigrationsStmts.push(database.prepare('INSERT INTO _hub_migrations (name, created_at) VALUES (?, ?)').bind(file, Date.now()))
135-
}
136-
await database.batch(appliedMigrationsStmts)
137-
consola.success('Database migrations done')
104+
await migrate(useDB(), { migrationsFolder: 'server/database/migrations' })
105+
.then(() => {
106+
consola.success('Database migrations done')
107+
})
108+
.catch((err) => {
109+
consola.error('Database migrations failed', err)
110+
})
138111
})
139112
})
140-
141113
```
142114

143115
::callout
144-
This solution is will create a `_hub_migrations` table in your database to keep track of the applied migrations. It will also run the migrations automatically in development mode.
116+
Drizzle will create a `__drizzle_migrations` table in your database to keep track of the applied migrations. It will also run the migrations automatically in development mode.
117+
::
118+
119+
To apply the migrations in staging or production, you can run the server using `npx nuxi dev --remote` command to connect your local server to the remote database, learn more about [remote storage](/docs/getting-started/remote-storage).
120+
121+
::note
122+
We are planning to update this section to leverage [Nitro Tasks](https://nitro.unjs.io/guide/tasks) instead of a server plugin in the future.
145123
::
146124

147125
### `useDrizzle()`

0 commit comments

Comments
 (0)