/prisma-data-migration

Migrate data from one database to another with prisma

Primary LanguageTypeScript

Prisma data migration

Migrate database from one to another.

Usage

Copy prisma/.env.sample to prisma/.env.

cp prisma/.env.sample prisma/.env

Add DATABASE_URL_OLD, and DATABASE_URL correct values as provided in Prisma datasource.

Finally, install packages and generate Prisma client.

bun install && bun generate

Supported databases

Since the repo uses Prisma, it supports database connectors as provided.

  • PostgreSQL
  • MySQL
  • SQLite
  • MongoDB
  • CockroachDB
  • Microsoft SQL Server

Write migration code

Open src/index.ts and start writing migration script. The initial template looks like below.

import {PrismaClient} from './generated/client';
import {PrismaClient as PrismaClientOld} from './generated/client-old';
import fs from 'fs';

const prismaOld = new PrismaClientOld();
const prisma = new PrismaClient();

const logFileName = 'logFile.txt';

async function main(): Promise<void> {
  
}


main()
  .catch((e) => {
    fs.appendFile(logFileName, `main catch::${e}\n`, (err) => {
      if (err) console.log(`File logging error: ${err}\n`);
    });

    process.exit(1);
  })
  .finally(async () => {
    await Promise.all([
      prismaOld.$disconnect(),
      prisma.$disconnect()
    ]);
  });

After you wrote the script, run bun start.