← Back to Node.js Course | Chapter 9: Database Integration | Lesson 6 of 7

Migrations की अवधारणा

Migrations versioned scripts हैं जो आपकी database structure को step by step बदलते हैं और undo हो सकते हैं।

In this page:

  1. Migrations Concept

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

javascript
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. #}
आम गलतियां
  1. old migrations edit करना
  2. manual schema changes करना
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.