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 { existsSync } from "node:fs";
|
||||
import { readFileSync } from "node:fs";
|
||||
import * as schema from "../database/schema";
|
||||
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
|
||||
import path from "node:path";
|
||||
|
||||
interface FeedItem {
|
||||
user: {
|
||||
id: number;
|
||||
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 (nitroApp) => {
|
||||
const db = useDrizzle();
|
||||
const migrationsFolder = path.resolve("server/database/migrations");
|
||||
|
||||
export default defineNitroPlugin(async () => {
|
||||
const config = useRuntimeConfig();
|
||||
const feedPath = "tmp/feed.json";
|
||||
console.log("Running database migrations...");
|
||||
console.log(`Migrations folder: ${migrationsFolder}`);
|
||||
migrate(db, { migrationsFolder });
|
||||
|
||||
if (!existsSync(feedPath)) {
|
||||
return;
|
||||
}
|
||||
console.log("Database migrations complete");
|
||||
|
||||
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 });
|
||||
const feed: FeedItem[] = JSON.parse(readFileSync(feedPath, "utf-8"));
|
||||
return [];
|
||||
});
|
||||
|
||||
for (const item of feed) {
|
||||
for (const item of feedData) {
|
||||
const userId = String(item.user.id);
|
||||
|
||||
await db
|
||||
.insert(schema.users)
|
||||
.insert(tables.users)
|
||||
.values({
|
||||
id: userId,
|
||||
name: item.user.name,
|
||||
@@ -58,7 +34,7 @@ export default defineNitroPlugin(async () => {
|
||||
createdAt: new Date(item.user.created_at),
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: schema.users.id,
|
||||
target: tables.users.id,
|
||||
set: {
|
||||
name: item.user.name,
|
||||
avatar: item.user.avatar,
|
||||
@@ -70,7 +46,7 @@ export default defineNitroPlugin(async () => {
|
||||
});
|
||||
|
||||
await db
|
||||
.insert(schema.tokens)
|
||||
.insert(tables.tokens)
|
||||
.values({
|
||||
userId: userId,
|
||||
refreshToken: item.token.refresh_token,
|
||||
@@ -78,7 +54,7 @@ export default defineNitroPlugin(async () => {
|
||||
expiresAt: new Date(item.token.expires_at),
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: schema.tokens.userId,
|
||||
target: tables.tokens.userId,
|
||||
set: {
|
||||
refreshToken: item.token.refresh_token,
|
||||
accessToken: item.token.access_token,
|
||||
@@ -87,7 +63,7 @@ export default defineNitroPlugin(async () => {
|
||||
});
|
||||
|
||||
await db
|
||||
.insert(schema.preferences)
|
||||
.insert(tables.preferences)
|
||||
.values({
|
||||
userId: userId,
|
||||
data: {
|
||||
@@ -99,7 +75,7 @@ export default defineNitroPlugin(async () => {
|
||||
},
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: schema.preferences.userId,
|
||||
target: tables.preferences.userId,
|
||||
set: {
|
||||
data: {
|
||||
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