Next.js Discord

Discord Forum

empty db migration - drizzle

Answered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
Its my first time using drizzle and im facing an issue where the db schema isnt being applied to my turso database. Here's the [repo](https://github.com/aryxst/onlyprogrammers)

database file
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';
import * as schema from './schema';

const client = createClient({
 url: process.env.TURSO_CONNECTION_URL!,
 authToken: process.env.TURSO_AUTH_TOKEN!,
});

export const db = drizzle(client, { schema });

export default db;
export { schema, client };
export * from './schema';

migration script(src/db/migrate.ts)
import { resolve } from 'node:path';
import { migrate } from 'drizzle-orm/libsql/migrator';
import { db } from '@/db';
(async () => {
 try {
  await migrate(db, { migrationsFolder: resolve(__dirname, '../../migrations') });
  console.log('Migration successful');
  process.exit(0);
 } catch (e) {
  console.error(e);
  process.exit(1);
 }
})();

drizzle.config.ts
import { type Config, defineConfig } from 'drizzle-kit';

export default defineConfig({
 schema: './src/db/schema.ts',
 out: './migrations',
 dialect: 'sqlite',
 driver: 'turso',
 dbCredentials: {
  url: process.env.TURSO_CONNECTION_URL!,
  authToken: process.env.TURSO_AUTH_TOKEN!,
 },
 migrations: {
  schema: './src/db/schema.ts',
 },
 verbose: true,
 // satisfies Config
}) as Config;

package.json scripts
 "scripts": {
 //...
  "db:migrate": "drizzle-kit generate && bun ./src/db/migrate.ts",
  "db:push": "drizzle-kit push"
}
Answered by Barbary Lion
this is postgres
View full answer

21 Replies

Barbary LionOP
I have no clue what im doing wrong
Barbary LionOP
I don't get any error 🫤
I think its not even reading the schemas
@Barbary Lion I think its not even reading the schemas
Barbary LionOP
For some reason
Toyger
did you try to run drizzle-kit generate directly?
Barbary LionOP
I will try hold on
Barbary LionOP
drizzle-kit: v0.21.1
drizzle-orm: v0.30.10

No config path provided, using default 'drizzle.config.ts'
Reading config file '/home/aryxst/Docs/onlyprogrammers/drizzle.config.ts'
0 tables


No schema changes, nothing to migrate 😴
@Toyger did you try to run `drizzle-kit generate` directly?
Barbary LionOP
I ran bunx drizzle-kit generate
Toyger
why do you need this
 migrations: {
  schema: './src/db/schema.ts',
 },

in drizzle.config, it needed only if you want to change migrations table config
are you sure your schema path and schema itself is ok?
did you check your migrations folder? maybe migrations already generated?
you can try run it like drizzle-kit generate:sqlite
also why you don't use drizzle-kit migrate instead.
$ drizzle-kit generate
drizzle-kit: v0.21.1
drizzle-orm: v0.30.10

No config path provided, using default 'drizzle.config.ts'
Reading config file '/home/aryxst/Docs/onlyprogrammers/drizzle.config.ts'
You have to provide 'schema' param
error: script "db:generate" exited with code 1
Thrianta
Why not ask this on the drizzle discord?
Barbary LionOP
user.ts
import { timestamp, pgTable, text, primaryKey, integer } from 'drizzle-orm/pg-core';
import type { AdapterAccountType } from 'next-auth/adapters';
import { createId } from '@paralleldrive/cuid2';

export const users = pgTable('user', {
 id: text('id')
  .primaryKey()
  .$defaultFn(() => createId()),
 name: text('name'),
 email: text('email').notNull(),
 emailVerified: timestamp('emailVerified', { mode: 'date' }),
 image: text('image'),
});

export const accounts = pgTable(
 'account',
 {
  userId: text('userId')
   .notNull()
   .references(() => users.id, { onDelete: 'cascade' }),
  type: text('type').$type<AdapterAccountType>().notNull(),
  provider: text('provider').notNull(),
  providerAccountId: text('providerAccountId').notNull(),
  refresh_token: text('refresh_token'),
  access_token: text('access_token'),
  expires_at: integer('expires_at'),
  token_type: text('token_type'),
  scope: text('scope'),
  id_token: text('id_token'),
  session_state: text('session_state'),
 },
 account => ({
  compoundKey: primaryKey({
   columns: [account.provider, account.providerAccountId],
  }),
 }),
);

export const sessions = pgTable('session', {
 sessionToken: text('sessionToken').primaryKey(),
 userId: text('userId')
  .notNull()
  .references(() => users.id, { onDelete: 'cascade' }),
 expires: timestamp('expires', { mode: 'date' }).notNull(),
});

export const verificationTokens = pgTable(
 'verificationToken',
 {
  identifier: text('identifier').notNull(),
  token: text('token').notNull(),
  expires: timestamp('expires', { mode: 'date' }).notNull(),
 },
 vt => ({
  compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
 }),
);

Then exported everything in schema.ts
export * from './user';
@Thrianta Why not ask this on the drizzle discord?
Barbary LionOP
didn't find it, you got the link?
@Barbary Lion user.ts tsx import { timestamp, pgTable, text, primaryKey, integer } from 'drizzle-orm/pg-core'; import type { AdapterAccountType } from 'next-auth/adapters'; import { createId } from '@paralleldrive/cuid2'; export const users = pgTable('user', { id: text('id') .primaryKey() .$defaultFn(() => createId()), name: text('name'), email: text('email').notNull(), emailVerified: timestamp('emailVerified', { mode: 'date' }), image: text('image'), }); export const accounts = pgTable( 'account', { userId: text('userId') .notNull() .references(() => users.id, { onDelete: 'cascade' }), type: text('type').$type<AdapterAccountType>().notNull(), provider: text('provider').notNull(), providerAccountId: text('providerAccountId').notNull(), refresh_token: text('refresh_token'), access_token: text('access_token'), expires_at: integer('expires_at'), token_type: text('token_type'), scope: text('scope'), id_token: text('id_token'), session_state: text('session_state'), }, account => ({ compoundKey: primaryKey({ columns: [account.provider, account.providerAccountId], }), }), ); export const sessions = pgTable('session', { sessionToken: text('sessionToken').primaryKey(), userId: text('userId') .notNull() .references(() => users.id, { onDelete: 'cascade' }), expires: timestamp('expires', { mode: 'date' }).notNull(), }); export const verificationTokens = pgTable( 'verificationToken', { identifier: text('identifier').notNull(), token: text('token').notNull(), expires: timestamp('expires', { mode: 'date' }).notNull(), }, vt => ({ compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }), }), ); Then exported everything in schema.ts tsx export * from './user';
Barbary LionOP
i copied this from the docs
Barbary LionOP
oh i might've found the issue
i've configured a sqlite dialect and use
import { timestamp, pgTable, text, primaryKey, integer } from 'drizzle-orm/pg-core';
Barbary LionOP
this is postgres
Answer
Barbary LionOP
yep that was the issue