Replies: 2 comments 6 replies
-
If you have a schema file in sync with your db, you can just update your schema to whatever you need and then create the migration file using drizzle-kit by running in you terminal: $ drizzle-kit generate:{you db type} That will create a new .sql file with your migration. Of course all of this is available in the docs |
Beta Was this translation helpful? Give feedback.
4 replies
-
I'm using postgres and using this utility (just in development) for when I need to reset all the tables without dealing with permissions and other stuffs. package.json reset.ts import { sql } from "drizzle-orm";
import { db } from "./client";
if (!("POSTGRES_URL" in process.env)) throw new Error("POSTGRES_URL not found on .env.development");
async function reset() {
console.log("⏳ Resetting database...");
const start = Date.now();
const query = sql`
-- Delete all tables
DO $$ DECLARE
r RECORD;
BEGIN
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
END $$;
-- Delete enums
DO $$ DECLARE
r RECORD;
BEGIN
FOR r IN (select t.typname as enum_name
from pg_type t
join pg_enum e on t.oid = e.enumtypid
join pg_catalog.pg_namespace n ON n.oid = t.typnamespace
where n.nspname = current_schema()) LOOP
EXECUTE 'DROP TYPE IF EXISTS ' || quote_ident(r.enum_name);
END LOOP;
END $$;
`;
await db.execute(query);
const end = Date.now();
console.log(`✅ Reset end & took ${end - start}ms`);
console.log("");
process.exit(0);
}
reset().catch((err) => {
console.error("❌ Reset failed");
console.error(err);
process.exit(1);
}); |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How do I create a schema/migration to delete a table?
Beta Was this translation helpful? Give feedback.
All reactions