Thuta Learning
ရှာဖွေရန်
AdvancedData & Databasesbeginner

Safe Schema Migrations

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Safe Schema Migrations concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ SQL/command ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် အသုံးချနိုင်ရန်

နားလည်ထားရမယ့် အချက်

Migration file ကို version control ထဲထားပြီး environment အားလုံးအတူ run ရပါတယ်။ Column rename/drop လို breaking change ကို deploy တစ်ခါတည်းမလုပ်ဘဲ new column add → app dual-read/write → data backfill → constraints validate → old column remove အဆင့်များခွဲပါတယ်။ DDL အချို့က lock ယူလို့ timeout နဲ့ table size ကိုကြိုစစ်ရပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Required `summary` column ထည့်ရာမှာ default+NOT NULL ကို blind run မလုပ်ဘဲ nullable add၊ batches နဲ့ backfill၊ `CHECK ... NOT VALID` + `VALIDATE CONSTRAINT`၊ ပြီးမှ NOT NULL သို့ပြောင်းမယ်။ Rollback က data loss ရှိမရှိအလိုက် forward fix ကိုလည်းပြင်ဆင်ပါ။

အတူတူ စမ်းရေးကြည့်မယ်

sql
SET lock_timeout = '2s';
ALTER TABLE app.tutorials ADD COLUMN summary text;

-- Backfill in controlled batches from the application/job.
UPDATE app.tutorials
SET summary = title
WHERE summary IS NULL AND tutorial_id BETWEEN 1 AND 1000;

ALTER TABLE app.tutorials
  ADD CONSTRAINT tutorials_summary_present
  CHECK (summary IS NOT NULL) NOT VALID;
ALTER TABLE app.tutorials VALIDATE CONSTRAINT tutorials_summary_present;
ALTER TABLE app.tutorials ALTER COLUMN summary SET NOT NULL;
You should see
Existing rows ကိုထိန်းထားပြီး required column ကိုအဆင့်လိုက်ထည့်နိုင်မည်။

၅ မိနစ် စမ်းကြည့်

`display_name` ကို `full_name` သို့ safe rename လုပ်မည့် multi-deploy plan ရေးပါ။

သတိလေးတစ်ချက်

Migration အောင်မြင်/မအောင်မြင်ကို SQL command exit တစ်ခုတည်းနဲ့မဆုံးဖြတ်ပါနှင့်—lock wait၊ replica lag နှင့် app errors ကိုစောင့်ကြည့်ပါ။

PostgreSQL — ALTER TABLEPostgreSQL Global Development Group

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Migration အောင်မြင်/မအောင်မြင်ကို SQL command exit တစ်ခုတည်းနဲ့မဆုံးဖြတ်ပါနှင့်—lock wait၊ replica lag နှင့် app errors ကိုစောင့်ကြည့်ပါ။
  • နမူနာ code ကို production data ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test database နှင့် recoverable backup ပေါ်တွင် အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

`display_name` ကို `full_name` သို့ safe rename လုပ်မည့် multi-deploy plan ရေးပါ။

You'll know it worked when: Existing rows ကိုထိန်းထားပြီး required column ကိုအဆင့်လိုက်ထည့်နိုင်မည်။

Safe Schema Migrations | Thuta Learning