Migrations की अवधारणा
Migrations versioned scripts हैं जो आपकी database structure को step by step बदलते हैं और undo हो सकते हैं।
In this page:
Migrations Concept
हर migration का एक change apply करने के लिए एक up step और इसे revert करने के लिए एक down step है, और एक table record करता है कौन-से चले हैं। यह हर environment का schema sync में रखता है और git में reviewable रखता है। Tools में Knex, Prisma Migrate, और Sequelize CLI शामिल हैं।
Note:
production में पहले से चली एक migration कभी edit न करें; एक नई add करें।
उदाहरण: Migrations concept
const applied = new Set();
const migrations = [
{ id: 1, up: () => "CREATE TABLE users (id INT)", down: () => "DROP TABLE users" },
{ id: 2, up: () => "ALTER TABLE users ADD name TEXT", down: () => "ALTER TABLE users DROP name" },
];
for (const m of migrations) if (!applied.has(m.id)) { console.log("up ", m.id, m.up()); applied.add(m.id); }
const last = migrations[migrations.length - 1];
console.log("down", last.id, last.down());
// Output:
// up 1 CREATE TABLE users (id INT)
// up 2 ALTER TABLE users ADD name TEXT
// down 2 ALTER TABLE users DROP name
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- old migrations edit करना
- manual schema changes करना
- down step भूल जाना
चैप्टर सारांश
- Migrations schema changes version करते हैं
- up apply करता है, down revert करता है
- एक table applied वाले track करता है
- इन्हें git में commit करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: