CyberCipher
DTDrizzle Team
•Created by CyberCipher on 3/14/2025 in #help
How to run migrations in production environment?
I'm using
drizzle
with sveltekit
and ATM I run migrations locally using the following scripts I have in my package.json
,
"db:generate": "drizzle-kit generate --name=drizzle",
"db:migrate": "tsx src/lib/server/database/migrate.ts",
"db:drop": "drizzle-kit drop",
"db:generate": "drizzle-kit generate --name=drizzle",
"db:migrate": "tsx src/lib/server/database/migrate.ts",
"db:drop": "drizzle-kit drop",
migrate.ts
file has this,
import { logger } from "$lib/logger";
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';
if (!process.env.DATABASE_URL) {
logger.error("DATABASE_URL is not set");
process.exit(1);
}
async function main() {
const migrationClient = postgres(process.env.DATABASE_URL as string, { max: 1 });
await migrate(drizzle(migrationClient), {
migrationsFolder: "./src/lib/server/database/migrations",
})
await migrationClient.end()
}
main()
import { logger } from "$lib/logger";
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';
if (!process.env.DATABASE_URL) {
logger.error("DATABASE_URL is not set");
process.exit(1);
}
async function main() {
const migrationClient = postgres(process.env.DATABASE_URL as string, { max: 1 });
await migrate(drizzle(migrationClient), {
migrationsFolder: "./src/lib/server/database/migrations",
})
await migrationClient.end()
}
main()
12 replies
DTDrizzle Team
•Created by CyberCipher on 11/22/2024 in #help
How to query join table with db.query syntax?
Here is my schema for User, Library and LibraryUser tables..
import { relations } from "drizzle-orm";
import { uniqueIndex, uuid, varchar } from "drizzle-orm/pg-core";
import { LibraryAlbum } from "./library-album";
import { LibraryArtist } from "./library-artist";
import { LibraryPath } from "./library-path";
import { LibraryTrack } from "./library-track";
import { LibraryUser } from "./library-user";
import { schema } from "./schema";
export const Library = schema.table("library", {
id: uuid("id").primaryKey().notNull().unique().defaultRandom(),
name: varchar("name", { length: 255 }).notNull(),
}, (table) => {
return {
library_index: uniqueIndex("library_index").on(table.name),
};
});
export const LibraryRelation = relations(Library, ({ many }) => ({
users: many(LibraryUser),
paths: many(LibraryPath),
tracks: many(LibraryTrack),
albums: many(LibraryAlbum),
artists: many(LibraryArtist),
}));
import { relations } from "drizzle-orm";
import { uniqueIndex, uuid, varchar } from "drizzle-orm/pg-core";
import { LibraryAlbum } from "./library-album";
import { LibraryArtist } from "./library-artist";
import { LibraryPath } from "./library-path";
import { LibraryTrack } from "./library-track";
import { LibraryUser } from "./library-user";
import { schema } from "./schema";
export const Library = schema.table("library", {
id: uuid("id").primaryKey().notNull().unique().defaultRandom(),
name: varchar("name", { length: 255 }).notNull(),
}, (table) => {
return {
library_index: uniqueIndex("library_index").on(table.name),
};
});
export const LibraryRelation = relations(Library, ({ many }) => ({
users: many(LibraryUser),
paths: many(LibraryPath),
tracks: many(LibraryTrack),
albums: many(LibraryAlbum),
artists: many(LibraryArtist),
}));
import { relations } from "drizzle-orm";
import { primaryKey, uuid } from "drizzle-orm/pg-core";
import { Library } from "./library";
import { schema } from "./schema";
import { User } from "./users";
export const LibraryUser = schema.table("library_user", {
library_id: uuid("library_id").references(() => Library.id, { onDelete: "cascade" }).notNull(),
user_id: uuid("user_id").references(() => User.id, { onDelete: "cascade" }).notNull(),
}, (table) => ({
pk: primaryKey({ columns: [table.user_id, table.library_id] }),
}));
export const LibraryUserRelation = relations(LibraryUser, ({ one }) => ({
library: one(Library, {
fields: [LibraryUser.library_id],
references: [Library.id],
}),
user: one(User, {
fields: [LibraryUser.user_id],
references: [User.id],
}),
}));
import { relations } from "drizzle-orm";
import { primaryKey, uuid } from "drizzle-orm/pg-core";
import { Library } from "./library";
import { schema } from "./schema";
import { User } from "./users";
export const LibraryUser = schema.table("library_user", {
library_id: uuid("library_id").references(() => Library.id, { onDelete: "cascade" }).notNull(),
user_id: uuid("user_id").references(() => User.id, { onDelete: "cascade" }).notNull(),
}, (table) => ({
pk: primaryKey({ columns: [table.user_id, table.library_id] }),
}));
export const LibraryUserRelation = relations(LibraryUser, ({ one }) => ({
library: one(Library, {
fields: [LibraryUser.library_id],
references: [Library.id],
}),
user: one(User, {
fields: [LibraryUser.user_id],
references: [User.id],
}),
}));
20 replies
DTDrizzle Team
•Created by CyberCipher on 9/30/2024 in #help
PostgresError: password authentication failed for user.
I am getting error while trying to insert a new user to the DB.
57 replies
DTDrizzle Team
•Created by CyberCipher on 8/31/2024 in #help
PostgresError: relation "public.artist" does not exist
Hi, I'm new to drizzle. I am getting this error while trying to apply migrations,
npm run db:migrate
> [email protected] db:migrate
> tsx src/lib/database/migrate.ts
{
severity_local: 'NOTICE',
severity: 'NOTICE',
code: '42P06',
message: 'schema "drizzle" already exists, skipping',
file: 'schemacmds.c',
line: '131',
routine: 'CreateSchemaCommand'
}
{
severity_local: 'NOTICE',
severity: 'NOTICE',
code: '42P07',
message: 'relation "__drizzle_migrations" already exists, skipping',
file: 'parse_utilcmd.c',
line: '217',
routine: 'transformCreateStmt'
}
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
PostgresError: relation "public.artist" does not exist
at ErrorResponse (file:///home/apoorv/repos/app/node_modules/postgres/src/connection.js:788:26)
at handle (file:///home/apoorv/repos/app/node_modules/postgres/src/connection.js:474:6)
at Socket.data (file:///home/apoorv/repos/app/node_modules/postgres/src/connection.js:315:9)
at Socket.emit (node:events:517:28)
at addChunk (node:internal/streams/readable:368:12)
at readableAddChunk (node:internal/streams/readable:341:9)
at Readable.push (node:internal/streams/readable:278:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
severity_local: 'ERROR',
severity: 'ERROR',
code: '42P01',
where: 'SQL statement "ALTER TABLE "album" ADD CONSTRAINT "album_artist_id_artist_id_fk" FOREIGN KEY ("artist_id") REFERENCES "public"."artist"("id") ON DELETE cascade ON UPDATE no action"\n' +
'PL/pgSQL function inline_code_block line 2 at SQL statement',
file: 'namespace.c',
line: '427',
routine: 'RangeVarGetRelidExtended'
}
Node.js v18.19.0
npm run db:migrate
> [email protected] db:migrate
> tsx src/lib/database/migrate.ts
{
severity_local: 'NOTICE',
severity: 'NOTICE',
code: '42P06',
message: 'schema "drizzle" already exists, skipping',
file: 'schemacmds.c',
line: '131',
routine: 'CreateSchemaCommand'
}
{
severity_local: 'NOTICE',
severity: 'NOTICE',
code: '42P07',
message: 'relation "__drizzle_migrations" already exists, skipping',
file: 'parse_utilcmd.c',
line: '217',
routine: 'transformCreateStmt'
}
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
PostgresError: relation "public.artist" does not exist
at ErrorResponse (file:///home/apoorv/repos/app/node_modules/postgres/src/connection.js:788:26)
at handle (file:///home/apoorv/repos/app/node_modules/postgres/src/connection.js:474:6)
at Socket.data (file:///home/apoorv/repos/app/node_modules/postgres/src/connection.js:315:9)
at Socket.emit (node:events:517:28)
at addChunk (node:internal/streams/readable:368:12)
at readableAddChunk (node:internal/streams/readable:341:9)
at Readable.push (node:internal/streams/readable:278:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
severity_local: 'ERROR',
severity: 'ERROR',
code: '42P01',
where: 'SQL statement "ALTER TABLE "album" ADD CONSTRAINT "album_artist_id_artist_id_fk" FOREIGN KEY ("artist_id") REFERENCES "public"."artist"("id") ON DELETE cascade ON UPDATE no action"\n' +
'PL/pgSQL function inline_code_block line 2 at SQL statement',
file: 'namespace.c',
line: '427',
routine: 'RangeVarGetRelidExtended'
}
Node.js v18.19.0
119 replies