Database Migration

ترحيل قاعدة البيانات

Intermediatedatabase1 min read
schema migrationdata migrationFirestore migrationbackfill

Definition

A controlled, documented change to a database's structure or data — adding a field, renaming a collection, or backfilling existing documents with new values.

تغيير مُتحكَّم فيه وموثَّق لبنية قاعدة البيانات أو بياناتها — إضافة حقل أو إعادة تسمية مجموعة أو ملء المستندات الموجودة بقيم جديدة مسبقاً.

Why It Matters

404Fault has 155+ glossary terms without `createdByUid` or `ownershipType` fields. Adding these fields requires a backfill migration that reads all glossary documents, determines which were admin-created (all of them), and writes the attribution fields. Without this migration, the profile attribution fix cannot work.

لدى 404Fault 155+ مصطلح قاموس بدون حقول `createdByUid` أو `ownershipType`. إضافة هذه الحقول يتطلب ترحيل ملء مسبق يقرأ جميع مستندات القاموس ويُحدّد أيها أنشأه المسؤول (جميعها) ويكتب حقول النسب. بدون هذا الترحيل، لا يمكن عمل إصلاح نسب الملف الشخصي.

Full Definition

A database migration is any change to the structure or content of a database that must be performed safely without data loss. In SQL databases (PostgreSQL), migrations are explicit schema files (ALTER TABLE, ADD COLUMN). In Firestore (schemaless), migrations are typically admin scripts that batch-update documents to add new required fields. A backfill migration adds a new field to existing documents: iterate all documents, write the new field value. Migrations must be: idempotent (safe to run twice), logged (what changed and when), and reversible when possible. Breaking changes require careful coordination between schema and code deployment.
ترحيل قاعدة البيانات هو أي تغيير على بنية قاعدة البيانات أو محتواها يجب تنفيذه بأمان دون فقدان البيانات. في قواعد بيانات SQL (PostgreSQL)، الترحيلات هي ملفات مخطط صريحة (ALTER TABLE وADD COLUMN). في Firestore (بدون مخطط)، الترحيلات هي عادةً سكريبتات مسؤول تُحدّث المستندات دفعةً لإضافة حقول جديدة مطلوبة. ترحيل الملء المسبق يُضيف حقلاً جديداً للمستندات الموجودة: يُكرّر جميع المستندات ويكتب قيمة الحقل الجديد. يجب أن تكون الترحيلات: متعددة (آمن تشغيلها مرتين) ومُسجَّلة (ما الذي تغيّر ومتى) وقابلة للعكس كلما أمكن. التغييرات الكاسرة تتطلب تنسيقاً دقيقاً بين نشر المخطط والكود.

Example Usage

Admin backfill migration pattern: `async function backfillAttributionFields() { const snap = await db.collection('glossary').get(); const batch = db.batch(); snap.docs.forEach(doc => { if (!doc.data().createdByUid) { batch.update(doc.ref, { createdByUid: ADMIN_UID, ownershipType: 'platform', createdVia: 'seed' }); } }); await batch.commit(); console.log('Backfilled', snap.size, 'documents'); }` — idempotent: skips docs that already have the field.

نمط ترحيل الملء المسبق للمسؤول: `async function backfillAttributionFields() { const snap = await db.collection('glossary').get(); const batch = db.batch(); snap.docs.forEach(doc => { if (!doc.data().createdByUid) { batch.update(doc.ref, { createdByUid: ADMIN_UID, ownershipType: 'platform', createdVia: 'seed' }); } }); await batch.commit(); console.log('Backfilled', snap.size, 'documents'); }` — متعدد: يتخطى المستندات التي تمتلك الحقل بالفعل.

Knowledge Graph

Avoid these mistakes when using Database Migration:

1

Not making migrations idempotent — if a migration fails halfway and must be re-run, it must be safe to run again without duplicating data

2

Running migrations during peak traffic — large Firestore batch operations consume write capacity; run migrations during low-traffic windows

3

Not testing migrations on a copy of production data first — migrations that look correct in dev can have edge cases with real production data

Sign in to unlock guided AI explanations from AI Teacher.

Generate a Prompt

Copy this prompt and use it directly with any AI model — no setup needed.

Ready-to-Use Prompt
Help me build a project using Database Migration.

Explain:
1. What is Database Migration and why it matters
2. The core architecture and required tools
3. Step-by-step implementation plan
4. Common mistakes to avoid: Not making migrations idempotent — if a migration fails halfway and must be re-run, it must be safe to run again without duplicating data, Running migrations during peak traffic — large Firestore batch operations consume write capacity; run migrations during low-traffic windows, Not testing migrations on a copy of production data first — migrations that look correct in dev can have edge cases with real production data
5. Best practices and production tips

Official Resources