การทดสอบ database migration ด้วย Schema จากระบบ production แทนการ download ฐานข้อมูลทั้งหมด - MUMT-IT/mis2018 GitHub Wiki

เนื่องจากฐานข้อมูลในระบบ production อาจมีขนาดใหญ่เกินไปในการ download มาเพื่อการทดสอบระบบหรือการปรับปรุงฐานข้อมูล เพื่อแก้ไขปัญหานี้ เราสามารถ download เฉพาะ Schema ซึ่งคือโครงสร้างของฐานข้อมูลได้ ซึ่งขนาดไฟล์จะไม่ใหญ่ โดยดำเนินการตามขั้นตอนต่อไปนี้

หากยังไม่มีไฟล์ schema ให้รันคำสั่งต่อไปนี้ โดยเปลี่ยนชื่อ YOUR_APP_NAME ให้เหมาะสม

pg_dump \
  --schema-only \
  --no-owner \
  --no-privileges \
  $(heroku config:get DATABASE_URL --app YOUR_APP_NAME) \
  > schema.sql
  1. สร้าง database ใหม่ตั้งชื่อที่เหมาะสม โดยรันคำสั่งต่อไปนี้
created my_new_db
  1. โหลด Schema เข้าฐานข้อมูลด้วยคำสั่งต่อไปนี้
psql -d my_new_db -f schema.sql
  1. เพิ่ม alembic_version number ด้วยคำสั่งต่อไปนี้ใน PSQL
INSERT INTO alembic_version (version_num) VALUES ('xxxxxxxxxx');

หรือรัน

flask db stamp xxxxxxxxx

โดย xxxxxxxxx หมายถึง alembic version number ล่าสุด หากไม่ทราบให้ใช้คำสั่งต่อไปนี้

heroku run flask db heads --app mummtmis
  1. จากนั้นรันคำสั่ง
flask db upgrade

หากต้องการ download seeding data เพื่อทดสอบระบบสามารถทำได้ดังนี้ (หมายเหตุ อาจจำเป็นต้อง download ตารางที่มี references มาประกอบด้วย)

pg_dump --data-only --column-inserts --no-owner --no-privileges \
-t public.staff_account \
-t public.staff_personal_info \
-t public.roles \
"$(heroku config:get DATABASE_URL --app mumtmis)" > seed_data.sql

ตามด้วย

psql -d my_new_db -f seed_data.sql