DEV Community

Cover image for Why Alembic is Basically Git for Your Database (And Why You Need It) 🗄️
wymdev
wymdev

Posted on

Why Alembic is Basically Git for Your Database (And Why You Need It) 🗄️

We have all been there. It is a late Friday afternoon, you finally hit deploy on a feature you have been working on all week, and the app goes live. Everything looks great for about two minutes until the error logs start lighting up.

The culprit? Someone forgot to run a crucial ALTER TABLE SQL script on the production database.

As developers, we are meticulous about our code. We use Git to track every single character change, review pull requests, and manage branches. But when it comes to the database schema, things often get surprisingly messy. We end up relying on memory, copy-pasting queries in Slack, or sharing terrifyingly named files like db_update_final_v3.sql.

If you are working in the Python ecosystem (especially with SQLAlchemy), there is a much better way. Enter Alembic.

To put it simply: Alembic is version control for your database.

How it Actually Works in Practice

Instead of manually logging into your database manager and tweaking tables by hand, Alembic lets you define your database changes in Python scripts.

Imagine you are building a user authentication system. Originally, your users table just had an id, email, and password. A few months later, the product team decides they want to add a last_login_date column.

Without Alembic, you would manually run the SQL command and just hope the rest of your team remembers to do the same on their local machines.

With Alembic, you simply head to your terminal and tell it to generate a new migration script:

alembic revision -m "add last login date to users"

Enter fullscreen mode Exit fullscreen mode

Alembic automatically creates a new file in your project with two essential functions: upgrade() and downgrade().

You write the code to add the column in upgrade(), and—this is the magic part—you write the code to remove it in downgrade(). You then commit this file to your Git repository just like any other piece of code.

When your teammates pull your latest branch, all they have to do is run alembic upgrade head, and their local databases will perfectly update to match yours. No guessing, no broken local environments.

The Good Stuff

Adopting Alembic changes the way your team handles data.

  • Peace of Mind: You get a crystal-clear, step-by-step history of how your database has evolved over time.
  • The "Undo" Button: If a deployment goes wrong and the new schema is causing issues, you can instantly roll back the database to the previous safe version using a simple downgrade command.
  • No More Sync Issues: It completely eliminates the classic "well, the database works on my machine" argument.

The Catch

Of course, it is not without a few growing pains.

  • There is a slight learning curve. You have to learn Alembic's specific way of writing operations, which can feel like overkill if you just want to quickly drop a column.
  • If two developers create different migration scripts on different branches at the exact same time, you will run into "multiple head" errors when you merge. It is just like a Git merge conflict, and you have to resolve it manually.

Despite those minor hurdles, once you start version-controlling your database, you will honestly wonder how you ever survived without it.


(Myanmar Version)

Alembic: သင့် Database အတွက် Git (ဘာလို့ သုံးသင့်တာလဲ) 🗄️

သောကြာနေ့ ညနေစောင်းလေးမှာ တစ်ပတ်လုံး ပင်ပင်ပန်းပန်း ရေးထားတဲ့ Feature အသစ်ကို Deploy လုပ်လိုက်တယ်ဆိုပါစို့။ App လေး အသက်ဝင်သွားပြီး နှစ်မိနစ်လောက်အကြာမှာပဲ Error Log တွေ တက်လာပါတော့တယ်။

ပြဿနာက ဘာဖြစ်မလဲ? Production Database မှာ ALTER TABLE ဆိုတဲ့ SQL script လေး run ဖို့ တစ်ယောက်ယောက် မေ့သွားလို့ပါ။ ကျွန်တော်တို့ Developer တော်တော်များများ ဒီလို အခြေအနေမျိုး ကြုံဖူးကြမှာပါ။

Code တွေနဲ့ ပတ်သက်လာရင် ကျွန်တော်တို့က အရမ်း သေချာကြတယ်။ Git သုံးပြီး Code အပြောင်းအလဲ မှန်သမျှကို သေချာ Track လုပ်တယ်၊ PR တွေ စစ်တယ်၊ Branch တွေ ခွဲပြီး စနစ်တကျ လုပ်ကြတယ်။ ဒါပေမယ့် Database Schema (ဖွဲ့စည်းပုံ) နဲ့ ပတ်သက်လာရင်တော့ အံ့သြစရာကောင်းလောက်အောင် ရှုပ်ထွေးနေတတ်တယ်။ မှတ်ဉာဏ်ကိုပဲ အားကိုးတာ၊ Slack မှာ Query တွေ Copy-paste လုပ်ပြီး ပို့တာ၊ ဒါမှမဟုတ် db_update_final_v3.sql လိုမျိုး နာမည်တွေပေးထားတဲ့ File တွေနဲ့ပဲ အလုပ်လုပ်နေတတ်ကြတယ်။

တကယ်လို့ သင်က Python (အထူးသဖြင့် SQLAlchemy) ကို အသုံးပြုနေတယ်ဆိုရင်တော့ ဒီထက် အများကြီး ပိုကောင်းတဲ့ နည်းလမ်းတစ်ခု ရှိပါတယ်။ အဲ့ဒါကတော့ Alembic ပါပဲ။

လွယ်လွယ်ပြောရရင်တော့ Alembic ဆိုတာ သင့် Database အတွက် Version Control ပါပဲ။

လက်တွေ့မှာ ဘယ်လို အလုပ်လုပ်လဲ?

Database Manager ထဲဝင်ပြီး Table တွေကို ကိုယ်တိုင် Manual လိုက်ပြင်နေမယ့်အစား၊ Database မှာ ဘာတွေပြောင်းလဲချင်လဲ ဆိုတာကို Alembic သုံးပြီး Python Script တွေနဲ့ ရေးလို့ရပါတယ်။

ဥပမာ - User Authentication System တစ်ခု ရေးနေတယ် ဆိုပါစို့။ အစတုန်းက users table မှာ id, email, နဲ့ password ပဲ ပါမယ်။ လအနည်းငယ် ကြာတော့ Product Team က User တွေ နောက်ဆုံး ဝင်ခဲ့တဲ့ အချိန်ကို သိချင်လို့ last_login_date column လေး ထပ်ထည့်ချင်တယ်လို့ ပြောလာတယ်။

Alembic သာ မရှိရင် သင်က SQL command ကို Manual run ပြီး၊ ကျန်တဲ့ Team Member တွေလည်း သူတို့စက်ထဲမှာ အဲ့ဒီလို လိုက် run ဖို့ မမေ့ကြပါစေနဲ့လို့ ဆုတောင်းနေရမှာပါ။

Alembic ကို သုံးမယ်ဆိုရင်တော့ Terminal မှာ Migration Script အသစ်တစ်ခု ထုတ်ဖို့ ဒီလိုလေး run လိုက်ရုံပါပဲ-

alembic revision -m "add last login date to users"

Enter fullscreen mode Exit fullscreen mode

Alembic က သင့် Project ထဲမှာ upgrade() နဲ့ downgrade() ဆိုတဲ့ အရေးကြီးတဲ့ function နှစ်ခုပါတဲ့ File အသစ်တစ်ခုကို အလိုအလျောက် ဖန်တီးပေးပါလိမ့်မယ်။

upgrade() ထဲမှာ Column အသစ်ထည့်မယ့် Code ကို ရေးပြီး၊ downgrade() ထဲမှာတော့ အဲ့ဒီ Column ကို ပြန်ဖျက်မယ့် Code ကို ရေးရပါမယ် (ဒီအချက်က အရမ်းမိုက်ပါတယ်)။ ပြီးရင်တော့ ဒီ File လေးကို တခြား Code တွေလိုပဲ Git မှာ Commit လုပ်လိုက်လို့ ရပါပြီ။

ကိုယ့် Team က လူတွေ Code အသစ်ကို Pull လုပ်လိုက်တဲ့အခါ alembic upgrade head လို့ run လိုက်တာနဲ့ သူတို့ရဲ့ Local Database တွေက သင့် Database နဲ့ အတိအကျ တူညီသွားပါလိမ့်မယ်။ ခန့်မှန်းပြီး လုပ်စရာမလိုသလို၊ Local Environment တွေ အလုပ်မလုပ်တော့တဲ့ ပြဿနာလည်း မရှိတော့ပါဘူး။

ကောင်းကျိုးများ

Alembic ကို စသုံးလိုက်တာနဲ့ သင့် Team ရဲ့ Data ကိုင်တွယ်ပုံက လုံးဝ ပြောင်းလဲသွားပါလိမ့်မယ်။

  • စိတ်အေးရခြင်း: Database စတင် တည်ထောင်တဲ့ နေ့ကနေ အခုချိန်ထိ ဘယ်လို အဆင့်ဆင့် ပြောင်းလဲလာလဲ ဆိုတဲ့ သမိုင်းကြောင်းကို ရှင်းရှင်းလင်းလင်း သိနိုင်ပါတယ်။
  • "Undo" လုပ်နိုင်ခြင်း: Deploy လုပ်တာ တစ်ခုခု မှားယွင်းသွားလို့ Database Schema အသစ်ကြောင့် ပြဿနာ တက်နေပြီဆိုရင်၊ Downgrade Command လေး တစ်ကြောင်းတည်းနဲ့ အရင် လုံခြုံတဲ့ အနေအထားကို ချက်ချင်း နောက်ပြန်ဆုတ် (Rollback) လုပ်နိုင်ပါတယ်။
  • Sync ပြဿနာများ ကင်းဝေးခြင်း: "ငါ့စက်ထဲက Database မှာတော့ အလုပ်လုပ်သားပဲ" ဆိုတဲ့ ငြင်းခုံမှုတွေ လုံးဝ ပျောက်ကွယ်သွားပါလိမ့်မယ်။

သတိထားစရာလေးများ

ဟုတ်ပါတယ်၊ အရာအားလုံးကတော့ အခက်အခဲလေးတွေ ရှိတတ်ပါတယ်။

  • အစပိုင်းမှာ လေ့လာဖို့ အချိန်နည်းနည်း ပေးရပါမယ်။ Column တစ်ခုလောက် အလွယ်တကူ ဖျက်ချင်ရုံလေးအတွက် Alembic ရဲ့ သီးသန့် ရေးထုံးတွေကို လေ့လာနေရတာ အလုပ်ပိုတယ်လို့ ခံစားရနိုင်ပါတယ်။
  • Developer နှစ်ယောက်က အချိန် တစ်ပြိုင်နက်တည်းမှာ မတူညီတဲ့ Branch တွေကနေ Migration Script အသစ်တွေ ထုတ်လိုက်မိရင်၊ Merge လုပ်တဲ့အခါ "multiple head" error တက်တတ်ပါတယ်။ ဒါက Git မှာ Merge Conflict ဖြစ်သလိုမျိုးပဲမို့လို့ Manual ပြန်ဖြေရှင်းပေးဖို့ လိုပါတယ်။

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


Top comments (0)