D1: `drizzle-kit push` says changes applied but database unchanged
Here are the logs:
here's my config:
The env vars are all set (when they're not set I get an auth error). it says changes applied, but the they're actually not. this is on a fresh and empty database
~/c/u/p/pin (main)> drizzle-kit push
drizzle-kit: v0.23.0
drizzle-orm: v0.31.2
No config path provided, using default path
Reading config file '/home/tom/code/unravel/packages/pin/drizzle.config.ts'
[✓] Pulling schema from database...
[✓] Changes applied
~/c/u/p/pin (main)> drizzle-kit push
drizzle-kit: v0.23.0
drizzle-orm: v0.31.2
No config path provided, using default path
Reading config file '/home/tom/code/unravel/packages/pin/drizzle.config.ts'
[✓] Pulling schema from database...
[✓] Changes applied
import { defineConfig } from "drizzle-kit";
import { config } from "dotenv";
const projectDir = process.cwd();
config({
path: `${projectDir}/.env`,
});
export default defineConfig({
schema: "./src/schema.ts",
out: "./migrations",
dialect: "sqlite",
driver: "d1-http",
dbCredentials: {
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
databaseId: process.env.CLOUDFLARE_DATABASE_ID!,
token: process.env.CLOUDFLARE_D1_TOKEN!,
},
});
import { defineConfig } from "drizzle-kit";
import { config } from "dotenv";
const projectDir = process.cwd();
config({
path: `${projectDir}/.env`,
});
export default defineConfig({
schema: "./src/schema.ts",
out: "./migrations",
dialect: "sqlite",
driver: "d1-http",
dbCredentials: {
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
databaseId: process.env.CLOUDFLARE_DATABASE_ID!,
token: process.env.CLOUDFLARE_D1_TOKEN!,
},
});
1 Reply
Hi Tom,
did you ever find a solution to this?
I'm having the same issues right now:
Result: DB is still empty. 😦
But generating a migration and migrating DOES work:
I would love to have
Then you can specify which DB to use with the CLI by setting the
// schema.ts
import { createId } from "@paralleldrive/cuid2";
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
export const users = sqliteTable("users", {
id: text().primaryKey().unique().$default(createId),
username: text(),
email: text().notNull().unique(),
password: text().notNull(),
forgotPasswordToken: text(),
createdAt: integer(),
updatedAt: integer(),
});
// schema.ts
import { createId } from "@paralleldrive/cuid2";
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
export const users = sqliteTable("users", {
id: text().primaryKey().unique().$default(createId),
username: text(),
email: text().notNull().unique(),
password: text().notNull(),
forgotPasswordToken: text(),
createdAt: integer(),
updatedAt: integer(),
});
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
import env from "./env";
export default defineConfig({
dialect: "sqlite",
schema: "./db/schema/index.ts",
out: "./db/migrations",
driver: "d1-http",
dbCredentials: {
databaseId: env.CLOUDFLARE_D1_ID,
accountId: env.CLOUDFLARE_ACCOUNT_ID,
token: env.CLOUDFLARE_API_TOKEN,
},
tablesFilter: ["/^(?!.*_cf_KV).*$/"],
});
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
import env from "./env";
export default defineConfig({
dialect: "sqlite",
schema: "./db/schema/index.ts",
out: "./db/migrations",
driver: "d1-http",
dbCredentials: {
databaseId: env.CLOUDFLARE_D1_ID,
accountId: env.CLOUDFLARE_ACCOUNT_ID,
token: env.CLOUDFLARE_API_TOKEN,
},
tablesFilter: ["/^(?!.*_cf_KV).*$/"],
});
➜ remix-template-cf-workers git:(main) ✗ bun drizzle-kit push
No config path provided, using default 'drizzle.config.ts'
Reading config file [redacted]
[✓] Pulling schema from database...
[✓] Changes applied
➜ remix-template-cf-workers git:(main) ✗ bun drizzle-kit push
No config path provided, using default 'drizzle.config.ts'
Reading config file [redacted]
[✓] Pulling schema from database...
[✓] Changes applied
➜ remix-template-cf-workers git:(main) ✗ bun drizzle-kit generate
No config path provided, using default 'drizzle.config.ts'
Reading config file [redacted]
1 tables
users 7 columns 2 indexes 0 fks
[✓] Your SQL migration file ➜ db/migrations/0000_green_toad_men.sql 🚀
➜ remix-template-cf-workers git:(main) ✗ bun drizzle-kit migrate
No config path provided, using default 'drizzle.config.ts'
Reading config file [redacted]
[✓] migrations applied successfully!%
➜ remix-template-cf-workers git:(main) ✗ bun drizzle-kit generate
No config path provided, using default 'drizzle.config.ts'
Reading config file [redacted]
1 tables
users 7 columns 2 indexes 0 fks
[✓] Your SQL migration file ➜ db/migrations/0000_green_toad_men.sql 🚀
➜ remix-template-cf-workers git:(main) ✗ bun drizzle-kit migrate
No config path provided, using default 'drizzle.config.ts'
Reading config file [redacted]
[✓] migrations applied successfully!%
push
working though as migrations are a bit tedious while developing
Update: I found a solution yesterday
The problem was, that push
used the local DB while Drizzle ORM and Studio used the remote DB. That was super confusing.
I found a nice config snippet that I modified slightly:
// drizzle.config.ts
// original snippet by noelukwa: https://github.com/drizzle-team/drizzle-orm/discussions/1545#discussioncomment-11115682
import { defineConfig, type Config } from "drizzle-kit";
import fs from "fs";
import path from "path";
import env from "./env";
const getLocalD1 = () => {
try {
const basePath = path.resolve(".wrangler");
const dbFile = fs
.readdirSync(basePath, { encoding: "utf-8", recursive: true })
.find((f) => f.endsWith(".sqlite"));
if (!dbFile) {
throw new Error(`.sqlite file not found in ${basePath}`);
}
const url = path.resolve(basePath, dbFile);
return url;
} catch (err) {
console.log(`Error ${err}`);
}
};
const isProd = () => process.env.NODE_ENV === "production";
const getCredentials = () => {
const prod = {
driver: "d1-http",
dbCredentials: {
databaseId: env.CLOUDFLARE_D1_ID,
accountId: env.CLOUDFLARE_ACCOUNT_ID,
token: env.CLOUDFLARE_API_TOKEN,
},
};
const dev = {
dbCredentials: {
url: getLocalD1(),
},
};
return isProd() ? prod : dev;
};
export default defineConfig({
dialect: "sqlite",
schema: "./db/schema/index.ts",
out: "./db/migrations",
tablesFilter: ["/^(?!.*_cf_KV).*$/"],
...getCredentials(),
}) satisfies Config;
// drizzle.config.ts
// original snippet by noelukwa: https://github.com/drizzle-team/drizzle-orm/discussions/1545#discussioncomment-11115682
import { defineConfig, type Config } from "drizzle-kit";
import fs from "fs";
import path from "path";
import env from "./env";
const getLocalD1 = () => {
try {
const basePath = path.resolve(".wrangler");
const dbFile = fs
.readdirSync(basePath, { encoding: "utf-8", recursive: true })
.find((f) => f.endsWith(".sqlite"));
if (!dbFile) {
throw new Error(`.sqlite file not found in ${basePath}`);
}
const url = path.resolve(basePath, dbFile);
return url;
} catch (err) {
console.log(`Error ${err}`);
}
};
const isProd = () => process.env.NODE_ENV === "production";
const getCredentials = () => {
const prod = {
driver: "d1-http",
dbCredentials: {
databaseId: env.CLOUDFLARE_D1_ID,
accountId: env.CLOUDFLARE_ACCOUNT_ID,
token: env.CLOUDFLARE_API_TOKEN,
},
};
const dev = {
dbCredentials: {
url: getLocalD1(),
},
};
return isProd() ? prod : dev;
};
export default defineConfig({
dialect: "sqlite",
schema: "./db/schema/index.ts",
out: "./db/migrations",
tablesFilter: ["/^(?!.*_cf_KV).*$/"],
...getCredentials(),
}) satisfies Config;
NODE_ENV
environment variable:
Dev (local, default): bun drizzle-kit push
Production (remote): NODE_ENV=production bun drizzle-kit push
(you might need to use crossenv if you're on Windows)