feat(db): add automatic Drizzle migration on app startup
This commit is contained in:
@@ -1,9 +1,19 @@
|
||||
import { openDatabaseSync } from "expo-sqlite";
|
||||
import { drizzle } from "drizzle-orm/expo-sqlite";
|
||||
import { migrate } from "drizzle-orm/expo-sqlite/migrator";
|
||||
import * as schema from "./schema";
|
||||
import migrations from "../../drizzle/migrations";
|
||||
|
||||
export const DATABASE_NAME = "swimbuddy.db";
|
||||
|
||||
const expoDb = openDatabaseSync(DATABASE_NAME, { enableChangeListener: true });
|
||||
|
||||
export const db = drizzle(expoDb, { schema });
|
||||
|
||||
// Run migrations on first load
|
||||
let migrated = false;
|
||||
export async function ensureMigrations() {
|
||||
if (migrated) return;
|
||||
await migrate(db, migrations);
|
||||
migrated = true;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { SQLiteProvider } from "expo-sqlite";
|
||||
import { DATABASE_NAME } from "../db/client";
|
||||
import { ensureMigrations } from "../db/client";
|
||||
|
||||
export function DatabaseProvider({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<SQLiteProvider databaseName={DATABASE_NAME} useSuspense>
|
||||
<SQLiteProvider databaseName={DATABASE_NAME} useSuspense onInit={ensureMigrations}>
|
||||
{children}
|
||||
</SQLiteProvider>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user