ترحيلات المخطط (Schema migrations)
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- Flyway أو Liquibase
- ترحيلات مُرقّمة بالإصدارات
- بيانات البذر (Seed data)
تعلّم أولًا
2 ساعة تدريب (تقدير)
مخرجات التعلم
- Version the schema with Flyway migrations that run on startup.
- Write forward-only migrations and explain why applied migrations are never edited.
- Separate reference/seed data from test data.
- Validate entity mappings against the migrated schema (
ddl-auto=validate).
الدروس
المختبر والتمارين
Lab
JNR-033-LAB1 · Migrate Shop
Outcomes: 1–4 · Time: ~2 h
- Add Flyway and convert the manual schema to
V1__baseline.sql. - Set
ddl-auto=validate; fix any mapping/schema mismatch it reports. - Add
V2__add_sku.sql: addsku, backfill existing rows, then setnot nulland a unique index. - Put reference data (order statuses) in
V3; keep demo data in test-only scripts. - 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)
| ID | Outcome | Task |
|---|---|---|
| JNR-033-EX01 | 1 | Name three migration files correctly and explain how Flyway orders them. |
| JNR-033-EX02 | 2 | An applied V4 has a typo. Explain what happens if you edit it and what to do instead. |
| JNR-033-EX03 | 2 | Write the safe three-step migration to add a not null column to a table with rows. |
| JNR-033-EX04 | 3 | Classify as reference or test data: order statuses, 50 fake customers, country list, a demo admin password. |
| JNR-033-EX05 | 4 | Show what ddl-auto=validate reports when an entity has a field that the table lacks. |
| JNR-033-EX06 | 1, 2 | Write 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.sqlinstead. - EX03
alter table ... add column x text;thenupdate ... set x = ...;thenalter 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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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).