Update migrations
This commit is contained in:
28
Dockerfile
28
Dockerfile
@@ -1,28 +0,0 @@
|
|||||||
FROM oven/bun:1-alpine AS base
|
|
||||||
|
|
||||||
FROM base AS deps
|
|
||||||
WORKDIR /app
|
|
||||||
COPY package.json bun.lock* ./
|
|
||||||
RUN bun install --frozen-lockfile
|
|
||||||
|
|
||||||
FROM base AS builder
|
|
||||||
WORKDIR /app
|
|
||||||
COPY --from=deps /app/node_modules ./node_modules
|
|
||||||
COPY . .
|
|
||||||
RUN bun run build
|
|
||||||
|
|
||||||
FROM base AS runner
|
|
||||||
WORKDIR /app
|
|
||||||
ENV NODE_ENV=production
|
|
||||||
|
|
||||||
RUN addgroup --system --gid 1001 nodejs
|
|
||||||
RUN adduser --system --uid 1001 nuxt
|
|
||||||
|
|
||||||
COPY --from=builder /app/.output ./.output
|
|
||||||
COPY --from=builder /app/package.json ./
|
|
||||||
|
|
||||||
USER nuxt
|
|
||||||
|
|
||||||
EXPOSE 3000
|
|
||||||
|
|
||||||
CMD ["bun", "x", "nuxt", "start"]
|
|
||||||
@@ -50,4 +50,9 @@ export default defineNuxtConfig({
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
nitro: {
|
||||||
|
experimental: {
|
||||||
|
tasks: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,52 +1,28 @@
|
|||||||
import { drizzle } from "drizzle-orm/bun-sqlite";
|
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
|
||||||
import { existsSync } from "node:fs";
|
import path from "node:path";
|
||||||
import { readFileSync } from "node:fs";
|
|
||||||
import * as schema from "../database/schema";
|
|
||||||
|
|
||||||
interface FeedItem {
|
export default defineNitroPlugin(async (nitroApp) => {
|
||||||
user: {
|
const db = useDrizzle();
|
||||||
id: number;
|
const migrationsFolder = path.resolve("server/database/migrations");
|
||||||
name: string;
|
|
||||||
avatar: string | null;
|
|
||||||
city: string | null;
|
|
||||||
country: string | null;
|
|
||||||
sex: string | null;
|
|
||||||
weight: number | null;
|
|
||||||
created_at: string;
|
|
||||||
};
|
|
||||||
token: {
|
|
||||||
id: number;
|
|
||||||
user_id: number;
|
|
||||||
refresh_token: string;
|
|
||||||
access_token: string;
|
|
||||||
expires_at: string;
|
|
||||||
};
|
|
||||||
preferences: {
|
|
||||||
tone: string[];
|
|
||||||
units: "Imperial" | "Metric";
|
|
||||||
enabled: boolean;
|
|
||||||
language: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export default defineNitroPlugin(async () => {
|
console.log("Running database migrations...");
|
||||||
const config = useRuntimeConfig();
|
console.log(`Migrations folder: ${migrationsFolder}`);
|
||||||
const feedPath = "tmp/feed.json";
|
migrate(db, { migrationsFolder });
|
||||||
|
|
||||||
if (!existsSync(feedPath)) {
|
console.log("Database migrations complete");
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Processing feed.json...");
|
const feedFile = Bun.file(path.resolve("tmp/feed.json"));
|
||||||
|
const feedData = await feedFile.json().catch((err) => {
|
||||||
|
console.log(err.message);
|
||||||
|
|
||||||
const db = drizzle(config.databaseUrl, { schema });
|
return [];
|
||||||
const feed: FeedItem[] = JSON.parse(readFileSync(feedPath, "utf-8"));
|
});
|
||||||
|
|
||||||
for (const item of feed) {
|
for (const item of feedData) {
|
||||||
const userId = String(item.user.id);
|
const userId = String(item.user.id);
|
||||||
|
|
||||||
await db
|
await db
|
||||||
.insert(schema.users)
|
.insert(tables.users)
|
||||||
.values({
|
.values({
|
||||||
id: userId,
|
id: userId,
|
||||||
name: item.user.name,
|
name: item.user.name,
|
||||||
@@ -58,7 +34,7 @@ export default defineNitroPlugin(async () => {
|
|||||||
createdAt: new Date(item.user.created_at),
|
createdAt: new Date(item.user.created_at),
|
||||||
})
|
})
|
||||||
.onConflictDoUpdate({
|
.onConflictDoUpdate({
|
||||||
target: schema.users.id,
|
target: tables.users.id,
|
||||||
set: {
|
set: {
|
||||||
name: item.user.name,
|
name: item.user.name,
|
||||||
avatar: item.user.avatar,
|
avatar: item.user.avatar,
|
||||||
@@ -70,7 +46,7 @@ export default defineNitroPlugin(async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await db
|
await db
|
||||||
.insert(schema.tokens)
|
.insert(tables.tokens)
|
||||||
.values({
|
.values({
|
||||||
userId: userId,
|
userId: userId,
|
||||||
refreshToken: item.token.refresh_token,
|
refreshToken: item.token.refresh_token,
|
||||||
@@ -78,7 +54,7 @@ export default defineNitroPlugin(async () => {
|
|||||||
expiresAt: new Date(item.token.expires_at),
|
expiresAt: new Date(item.token.expires_at),
|
||||||
})
|
})
|
||||||
.onConflictDoUpdate({
|
.onConflictDoUpdate({
|
||||||
target: schema.tokens.userId,
|
target: tables.tokens.userId,
|
||||||
set: {
|
set: {
|
||||||
refreshToken: item.token.refresh_token,
|
refreshToken: item.token.refresh_token,
|
||||||
accessToken: item.token.access_token,
|
accessToken: item.token.access_token,
|
||||||
@@ -87,7 +63,7 @@ export default defineNitroPlugin(async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await db
|
await db
|
||||||
.insert(schema.preferences)
|
.insert(tables.preferences)
|
||||||
.values({
|
.values({
|
||||||
userId: userId,
|
userId: userId,
|
||||||
data: {
|
data: {
|
||||||
@@ -99,7 +75,7 @@ export default defineNitroPlugin(async () => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
.onConflictDoUpdate({
|
.onConflictDoUpdate({
|
||||||
target: schema.preferences.userId,
|
target: tables.preferences.userId,
|
||||||
set: {
|
set: {
|
||||||
data: {
|
data: {
|
||||||
enabled: item.preferences.enabled,
|
enabled: item.preferences.enabled,
|
||||||
@@ -112,5 +88,5 @@ export default defineNitroPlugin(async () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Processed ${feed.length} feed items`);
|
console.log(`Processed ${feedData.length} feed items`);
|
||||||
});
|
});
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
|
|
||||||
import { drizzle } from "drizzle-orm/bun-sqlite";
|
|
||||||
import * as schema from "../database/schema";
|
|
||||||
|
|
||||||
export default defineNitroPlugin(async () => {
|
|
||||||
const config = useRuntimeConfig();
|
|
||||||
const db = drizzle(config.databaseUrl, { schema });
|
|
||||||
|
|
||||||
console.log("Running database migrations...");
|
|
||||||
await migrate(db, { migrationsFolder: "./server/database/migrations" });
|
|
||||||
console.log("Database migrations complete");
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user