push keeps wanting to truncate my existing table data

As i develop locally i often want to push a db change with push:pg and instead of just updating the change it wants to recreate thew whole db and delete all content! how can i get it to just do an incremental migration?
3 Replies
arush
arushOP9mo ago
import { pgTable, pgTableCreator, unique, uuid, varchar, foreignKey, serial, integer, jsonb, boolean, index, timestamp, text } from "drizzle-orm/pg-core"
import { sql } from "drizzle-orm"

/**
* BEGIN remember to comment out before running db:push else it will DELETE ALL DATA IN THESE TABLES
*
* EXISTING TABLES
* These tables were in the db before setting up this project and we need them.
* Drizzle is not responsible for handling their creation but we need to define them as models here
* for type inference in tRPC and the api.
*
* However, defining them here causes drizzle to want to delete all content on db:push
*
* */

export const digitalAssetPrices = pgTable("digital_asset_prices", {
id: uuid("id").default(sql`uuid_generate_v4()`).primaryKey().notNull(),
name: varchar("name", { length: 255 }),
petitionPrice: varchar("petition_price", { length: 255 }),
type: varchar("type", { length: 255 }),
},
(table) => {
return {
digitalAssetPricesNameKey: unique("digital_asset_prices_name_key").on(table.name),
}
});

...

/**
* END remember to comment out before running db:push else it will DELETE ALL DATA IN THESE TABLES
* */


export const users = pgTable("users", {
id: serial("id").primaryKey().notNull(),
uuid: uuid("uuid").default(sql`uuid_generate_v4()`),
fullName: text("full_name"),
phone: varchar("phone", { length: 256 }),
email: varchar("email", { length: 256 }),
createdAt: timestamp("created_at", { mode: 'string' }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { mode: 'string' }),
});
import { pgTable, pgTableCreator, unique, uuid, varchar, foreignKey, serial, integer, jsonb, boolean, index, timestamp, text } from "drizzle-orm/pg-core"
import { sql } from "drizzle-orm"

/**
* BEGIN remember to comment out before running db:push else it will DELETE ALL DATA IN THESE TABLES
*
* EXISTING TABLES
* These tables were in the db before setting up this project and we need them.
* Drizzle is not responsible for handling their creation but we need to define them as models here
* for type inference in tRPC and the api.
*
* However, defining them here causes drizzle to want to delete all content on db:push
*
* */

export const digitalAssetPrices = pgTable("digital_asset_prices", {
id: uuid("id").default(sql`uuid_generate_v4()`).primaryKey().notNull(),
name: varchar("name", { length: 255 }),
petitionPrice: varchar("petition_price", { length: 255 }),
type: varchar("type", { length: 255 }),
},
(table) => {
return {
digitalAssetPricesNameKey: unique("digital_asset_prices_name_key").on(table.name),
}
});

...

/**
* END remember to comment out before running db:push else it will DELETE ALL DATA IN THESE TABLES
* */


export const users = pgTable("users", {
id: serial("id").primaryKey().notNull(),
uuid: uuid("uuid").default(sql`uuid_generate_v4()`),
fullName: text("full_name"),
phone: varchar("phone", { length: 256 }),
email: varchar("email", { length: 256 }),
createdAt: timestamp("created_at", { mode: 'string' }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { mode: 'string' }),
});
above is my code in schema.ts, the comment block explains the issue
Wendelstein
Wendelstein9mo ago
Hi. If you have existing tables, there is a drizzle-kit introspect:pg (https://orm.drizzle.team/kit-docs/commands#introspect--pull), that lets you pull DDL from existing database and generate schema.ts from it. I think only then you can move on and apply your changes to your schema.ts. I'm pretty sure that drizzle would first need to "get to know" your current database schema to be able to perform changes to it.
Drizzle ORM - List of commands
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
arush
arushOP9mo ago
ive done the introspect, and that's where the above code comes from after the introspect i do a push and it wants to delete the contents of the existing data, it makes no sense got it. i have to keep the schema.ts in the drizzle folder. i was deleting it after the introspect. thanks!
Want results from more Discord servers?
Add your server