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
// Make sure to create the _hub_migrations table if it doesn't exist
114
+
awaitdatabase.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 =awaitdatabase.prepare('SELECT * FROM _hub_migrations').all()
.replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS')
121
-
.replace('CREATE INDEX', 'CREATE INDEX IF NOT EXISTS')
122
-
.replace('CREATE UNIQUE INDEX', 'CREATE UNIQUE INDEX IF NOT EXISTS')
123
-
awaitdatabase.prepare(stmt).run()
132
+
awaitdatabase.prepare(stmt.trim()).run()
124
133
}
134
+
appliedMigrationsStmts.push(database.prepare('INSERT INTO _hub_migrations (name, created_at) VALUES (?, ?)').bind(file, Date.now()))
125
135
}
136
+
awaitdatabase.batch(appliedMigrationsStmts)
126
137
consola.success('Database migrations done')
127
138
})
128
139
})
140
+
129
141
```
130
142
131
143
::callout
132
-
This solution is not perfect as it will run the migrations every time the server starts. You can improve it by keeping track of the migrations that have been applied and only run the new ones.
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.
0 commit comments