How to create data migrations with Drizzle ORM? – Orm

by
Alexei Petrov
angular-forms database-migration drizzle react-typescript

Quick Fix: Use the drizzle-kit generate:pg --custom command to create data migrations with Drizzle ORM as detailed in the documentation.

The Problem:

I need to develop a method to efficiently migrate data into my database using Drizzle ORM. I want to create and run data migrations that insert data into the database, similar to Django’s migration system. Specifically, I’m looking to utilize Drizzle kit for this purpose. I’ve successfully used Drizzle kit for schema changes, but I’m unsure how to perform data migrations with it. I need a solution that ensures data migration is executed only once and allows for custom SQL or code to be run during the migration process.

The Solutions:

Solution 1: Using `drizzle-kit generate:pg –custom` command

To perform data migrations using Drizzle ORM, you can utilize the `drizzle-kit generate:pg –custom` command. This command allows you to create custom migration files where you can write SQL queries or custom Python code to modify your database data.

Here’s an example of how you can use this command:

drizzle-kit generate:pg --custom add_users

This command will generate a migration file named `add_users.sql` in the `migrations` directory of your Drizzle project. You can then open this file and add the necessary SQL queries or Python code to perform your data migration.

For example, if you want to insert some data into a table called `users`, you can add the following SQL query to the migration file:

INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]');

Once you have completed writing the migration file, you can run it using the `drizzle-kit migrate` command:

drizzle-kit migrate

This command will apply all the pending migrations to your database, including the data migration you just created.

By using this approach, you can easily perform data migrations with Drizzle ORM, ensuring that your database data is updated as needed.

Solution 2: Using JavaScript and Drizzle ORM

To perform data migrations using Drizzle ORM, you can use JavaScript and follow these steps:

  1. Install Drizzle ORM:

    • Install Drizzle ORM using npm:
    npm install drizzle-orm/postgres-js
    
  2. Create a JavaScript File:

    • Create a JavaScript file, such as index.js, and import the necessary modules from Drizzle ORM.
    import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js';
    import { migrate } from 'drizzle-orm/postgres-js/migrator';
    import postgres from 'postgres';
    
  3. Configure Database Connection:

    • Set up a PostgreSQL connection using the postgres module and store the connection string in an environment variable named DATABASE_URL.
  4. Initialize Drizzle ORM:

    • Initialize Drizzle ORM using the drizzle() function and pass in the PostgreSQL connection client.
  5. Specify Migrations Folder:

    • Specify the folder containing your data migrations using the migrationsFolder option when calling the migrate() function.
  6. Run Data Migrations:

    • Run the data migrations by calling the migrate() function, which will create the necessary tables and insert data if needed.
  7. Execute the Script:

    • Run the JavaScript file (node index.js) to execute the data migrations.
  8. Ensure Migrations Are Run Only Once:

    • Drizzle ORM does not have a built-in mechanism to ensure that data migrations are run only once. You can implement your own mechanism, such as using a migration history table, to track the status of migrations and prevent them from being run multiple times.

Q&A

How do I make data migrations with Drizzle?

Use the ‘–custom’ flag with the ‘drizzle-kit generate:pg’ command.

How can I run data migrations with Drizzle?

Run the script ‘node -r esbuild-register src/db/index.ts’.

Video Explanation:

The following video, titled "Drizzle ORM #1- Setup - Schema - Migrations - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

=1 In this first episode of our Drizzle ORM tutorial series, we dive into the world of database management with a focus on PostgreSQL. Join ...