انتقل إلى المحتوى
أمين أبوزيد
JNR-033

ترحيلات المخطط (Schema migrations)

  • 6 درس
  • 2 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • Flyway أو Liquibase
  • ترحيلات مُرقّمة بالإصدارات
  • بيانات البذر (Seed data)

تعلّم أولًا

2 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Version the schema with Flyway migrations that run on startup.
  2. Write forward-only migrations and explain why applied migrations are never edited.
  3. Separate reference/seed data from test data.
  4. Validate entity mappings against the migrated schema (ddl-auto=validate).

الدروس

  1. 01لماذا يحتاج المخطط إلى التحكم بالإصدارات
  2. 02أساسيات Flyway
  3. 03كتابة ترحيلات آمنة
  4. 04بيانات البذر (Seed data)
  5. 05Liquibase باختصار
  6. 06عرض عملي: تأسيس خط أساس لـ Shop باستخدام Flyway وإضافة V2

المختبر والتمارين

Lab

JNR-033-LAB1 · Migrate Shop

Outcomes: 1–4 · Time: ~2 h

  1. Add Flyway and convert the manual schema to V1__baseline.sql.
  2. Set ddl-auto=validate; fix any mapping/schema mismatch it reports.
  3. Add V2__add_sku.sql: add sku, backfill existing rows, then set not null and a unique index.
  4. Put reference data (order statuses) in V3; keep demo data in test-only scripts.
  5. Reach the same schema on a fresh database and on an existing one (baseline).

Acceptance: fresh database and existing database both reach the same state.

Exercises (assessment: write a migration for a new requirement)

IDOutcomeTask
JNR-033-EX011Name three migration files correctly and explain how Flyway orders them.
JNR-033-EX022An applied V4 has a typo. Explain what happens if you edit it and what to do instead.
JNR-033-EX032Write the safe three-step migration to add a not null column to a table with rows.
JNR-033-EX043Classify as reference or test data: order statuses, 50 fake customers, country list, a demo admin password.
JNR-033-EX054Show what ddl-auto=validate reports when an entity has a field that the table lacks.
JNR-033-EX061, 2Write a migration for a new requirement: "products have an optional weight_grams that must be positive when present".
أظهر الحلول
  • EX01 V1__baseline.sql, V2__add_sku.sql, V3__seed_status.sql; ordered by version number, not alphabetically as text.
  • EX02 The checksum differs and Flyway refuses to start; add V5__fix_typo.sql instead.
  • EX03 alter table ... add column x text; then update ... set x = ...; then alter table ... alter column x set not null;.
  • EX04 Reference: statuses, countries. Test: fake customers, demo admin password (never in production).
  • EX05 Startup fails with a schema-validation error naming the missing column.
  • EX06 alter table product add column weight_grams integer; alter table product add constraint ck_product_weight check (weight_grams is null or weight_grams > 0);

الاختبار

Outcomes: O1 Flyway on startup · O2 forward-only migrations · O3 seed vs test data · O4 schema validation.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Flyway records applied migrations in:O1
  2. 2A correct migration file name is:O1
  3. 3Migrations run:O1
  4. 4An already applied migration has an error. You should:O2
  5. 5Editing an applied migration causes:O2
  6. 6Safe way to add a required column to a populated table:O2
  7. 7Order status codes needed everywhere are:O3
  8. 8Fake demo customers belong in:O3
  9. 9ddl-auto=validate does:O4
  10. 10ddl-auto=update in production is:O4

الجزء العملي

Six exercises JNR-033-EX01 to EX06 plus the lab; the assessment is writing a migration for a new requirement (EX06). Pass: EX06 correct and safe, at least five of six exercises correct, and fresh and existing databases reaching the same state.

أخطاء شائعة

Editing an applied migration; ddl-auto=update in production.

التقييم

Write a migration for a new requirement.

الصلة بالمسار الوظيفي

Zero-downtime migrations (MID-020).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.