Running migrate() in Sveltekit project

I have a migrate.ts file with the script for running migrate(), how would you typically execute this in a sveltekit project? I tried adding it to a npm script: "migrate": "ts-node src/lib/server/database/migrate.ts" but I get errors here (ERR_UNKNOWN_FILE_EXTENSION). Any ideas?
3 Replies
sik
sik17mo ago
i use tsx dont know if this is a ts-node issue i have a script something like this tsx ./src/lib/server/db/migrate.ts if you are using sveltekit $env variables in migrate.ts mind you that will not work
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';

import 'dotenv/config';

const connectionString = process.env.DATABASE_URL ?? '';
const client = postgres(connectionString, { max: 1 });

const db = drizzle(client);

export async function main() {
try {
console.log('Migrating database...');
await migrate(db, { migrationsFolder: 'drizzle' });
console.log('Database migrated.');
} catch (err) {
console.error(err);
}
process.exit(0);
}
main();
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';

import 'dotenv/config';

const connectionString = process.env.DATABASE_URL ?? '';
const client = postgres(connectionString, { max: 1 });

const db = drizzle(client);

export async function main() {
try {
console.log('Migrating database...');
await migrate(db, { migrationsFolder: 'drizzle' });
console.log('Database migrated.');
} catch (err) {
console.error(err);
}
process.exit(0);
}
main();
thomasmol
thomasmolOP17mo ago
ah okay thanks! will try this
megmut
megmut17mo ago
Here is my Migrate script from my sveltekit project
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';

const connectionString = process.env.POSTGRES_URL;

if (!connectionString) {
throw new Error('No connection string present');
}

const sql = postgres(connectionString, { max: 1, ssl: true });
const db = drizzle(sql);

await migrate(db, { migrationsFolder: './src/lib/database/drizzle' });

console.info('Migrations complete');

process.exit(0);
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';

const connectionString = process.env.POSTGRES_URL;

if (!connectionString) {
throw new Error('No connection string present');
}

const sql = postgres(connectionString, { max: 1, ssl: true });
const db = drizzle(sql);

await migrate(db, { migrationsFolder: './src/lib/database/drizzle' });

console.info('Migrations complete');

process.exit(0);
And I invoke it with:
"db:migrate": "node -r dotenv/config --loader ts-node/esm ./src/lib/database/migrate.ts",
"db:migrate": "node -r dotenv/config --loader ts-node/esm ./src/lib/database/migrate.ts",
From memory, the --loader ts-node/esm is why you're getting 'file extension not found'
Want results from more Discord servers?
Add your server