-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Seven Yang edited this page Apr 14, 2025
·
6 revisions
A comprehensive tool for managing database schema and data migrations with full control, supporting SQLite, PostgreSQL, MySQL, SQL Server, and MongoDB.
git clone https://github.com/45592858/DataV5.git --depth=1
cd DataV5
npm install
Schema changes are handled via a custom script that wraps Prisma CLI functionality.
npm run schema-migrate:dev # Development
npm run schema-migrate:test # Test
npm run schema-migrate:prod # Production
Each command uses its corresponding .env
file (e.g., .env.development
).
Update your Prisma model (e.g., add Profile
) in /schema/*.prisma
.
// module-01.prisma
model Post {
id Int @default(autoincrement()) @id
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int
}
model User {
id Int @default(autoincrement()) @id
name String?
email String @unique
posts Post[]
+ profile Profile?
}
+model Profile {
+ id Int @default(autoincrement()) @id
+ bio String?
+ userId Int @unique
+ user User @relation(fields: [userId], references: [id])
+}
npm run schema-migrate:dev dev
This will:
- Generate a migration in
prisma/migrations
- Apply it to the development DB
After committing changes:
npm run schema-migrate:prod deploy
npm run schema-migrate:dev status
Pattern: YYYYMMDD-XXX_ENV_description.sql
Example:
20240401-001_dev_seed_users.sql
-
ENV
can bedev
,test
,prod
, orall
Store SQL files in the /data
directory.
npm run data-migration dev
npm run data-migration test
npm run data-migration prod
Files are:
- Executed in filename order
- Filtered by
ENV
orall
- INSERT, UPDATE, DELETE, SELECT
- MERGE, REPLACE, CALL, EXECUTE
- All statements must end with
;
Auto-ignores:
- Duplicate keys
- Unique constraint violations
npm run schema-migrate:test deploy
npm run data-migrate:test
Repeat with prod
for production.
Update schema/root.prisma
:
datasource db {
provider = "postgresql" // or mysql, sqlserver, mongodb, sqlite
url = env("DB_URL")
}
Set DB_URL
in .env.*
:
# PostgreSQL
DB_URL="postgresql://user:pass@localhost:5432/db?schema=public"
# MySQL
DB_URL="mysql://user:pass@localhost:3306/db"
# SQL Server
DB_URL="sqlserver://localhost:1433;initial catalog=db;user=sa;password=pass;"
# MongoDB
DB_URL="mongodb://user:pass@localhost/db?authSource=admin"
- Always commit migrations
- Never manually alter the DB
- Test everything in
dev
first - Backup data before running large migrations
- Use meaningful and timestamped SQL filenames
- Coordinate schema changes within your team
- It depends on Node.js and Prisma, the next-generation ORM for Node.js.
- If you want to understand the technology behind the project, please refer to the Prisma Docs.