Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: drizzle-kit: push runs already applied migration #3844

Open
1 task done
IndikaUdagedara opened this issue Dec 26, 2024 · 0 comments
Open
1 task done

[BUG]: drizzle-kit: push runs already applied migration #3844

IndikaUdagedara opened this issue Dec 26, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@IndikaUdagedara
Copy link

IndikaUdagedara commented Dec 26, 2024

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.36.4

What version of drizzle-kit are you using?

0.27.2

Other packages

No response

Describe the Bug

drizzle-kit push is executing migrations even without schema changes

Please see the following example.

  • schema.ts
export const organisations = sqliteTable("organisation", {
  id: int().primaryKey({ autoIncrement: true }),
  name: text().unique().notNull(),
  status: text().notNull(),
  email: text(),
  createdAt: int({ mode: "timestamp_ms" })
    .$type<Date>()
    .$defaultFn(() => new Date()),
  updatedAt: int({ mode: "timestamp_ms" })
    .$type<Date>()
    .$defaultFn(() => new Date()),
});

export const users = sqliteTable("user", {
  id: int().primaryKey({ autoIncrement: true }),
  sub: text(),
  firstName: text(),
  lastName: text(),
  email: text().unique(),
  emailVerified: int({ mode: "boolean" }).default(false),
  status: text().notNull(),
  roles: text({ mode: "json" }).$type<string[]>().default([]),
  createdAt: int({ mode: "timestamp_ms" })
    .$type<Date>()
    .$defaultFn(() => new Date()),
  updatedAt: int({ mode: "timestamp_ms" })
    .$type<Date>()
    .$defaultFn(() => new Date()),
});

export const organisationUsers = sqliteTable(
  "organisationUser",
  {
    organisationId: int()
      .notNull()
      .references(() => organisations.id),
    userId: int()
      .notNull()
      .references(() => users.id),
    roles: text({ mode: "json" }).$type<string[]>().default([]),
  },
  (t) => ({
    pk: primaryKey({ columns: [t.userId, t.organisationId] }),
  })
);
  • drizzle.config.ts
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  schema: "./db/schema.ts",
  out: "./drizzle",
  dialect: "sqlite",
  dbCredentials: {
    url: "app.db"
  },
  verbose: true,
});

When I run pnpm drizzle-kit push, it outputs following although it is already applied (i.e., running pnpm drizzle-kit push again without schema changes still attemps to make the following change). It should run a migration only if there are schema changes.

$ pnpm drizzle-kit push
No config path provided, using default 'drizzle.config.ts'
Reading config file '.../src/drizzle.config.ts'
[✓] Pulling schema from database...

 Warning  You are about to execute current statements:

CREATE TABLE `__new_organisationUser` (
        `organisationId` integer NOT NULL,
        `userId` integer NOT NULL,
        `roles` text DEFAULT '[]',
        PRIMARY KEY(`userId`, `organisationId`),
        FOREIGN KEY (`organisationId`) REFERENCES `organisation`(`id`) ON UPDATE no action ON DELETE no action,
        FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE no action
);

INSERT INTO `__new_organisationUser`("organisationId", "userId", "roles") SELECT "organisationId", "userId", "roles" FROM `organisationUser`;
DROP TABLE `organisationUser`;
ALTER TABLE `__new_organisationUser` RENAME TO `organisationUser`;

[✓] Changes applied
@IndikaUdagedara IndikaUdagedara added the bug Something isn't working label Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant