CAFECA-IO / iSunFA

Artificial Intelligence in Financial
https://isunfa.com
GNU General Public License v3.0
0 stars 0 forks source link

[DOCS] - Experiment how to do db migration involving changed schema and existing data - on existing database #3296

Closed arealclimber closed 1 day ago

arealclimber commented 2 days ago

Summary

[DOCS] - Experiment how to do db migration involved changed schema and existing data - on existing database

Tasks

實驗現有資料庫遷移流程

  1. 了解 sql 語法

  2. 備份現有資料庫

    1. pg_dump -U <your_username> -h <old_host_ip> -p <old_port> -F c -b -v -f old_db_backup.dump <old_database_name>
  3. 在新的空白資料庫上復原舊資料

    1. pg_restore -U your_username -h new_host -p new_port -d new_database_name -v --no-owner old_db_backup.dump
  4. 寫 sql 腳本去修改 db data and schema,實驗 sql 腳本是否有寫錯

    1. psql -h <HOST_IP> -U <USER_NAME> -d <DATABASE_NAME> -f <SQL_FILE_NAME>
  5. [測試在 schema 跟 migration.sql 不同的情況]手動更新 schema.prisma、將 sql 腳本放到 prisma/migration 裡,並且執行以下指令,將 migration.sql 應用到 db 後,查看 db 跟 schema.prisma 是否將資料庫結構改成需要的樣子

    1. npx prisma migrate deploy
  6. [測試 KM 的遷移步驟]更新 schema.prisma ,然後執行npx prisma migrate dev --create-only --name custom-sql-migration指令,產生 migration.sql 之後,根據需求編輯 migration.sql ;修改好 migration.sql 之後在備份好的資料庫執行 npx prisma migrate deploynpx prisma generate

Dependencies

No response

Other Dependencies

No response

Additional Notes

No response

arealclimber commented 1 day ago

背景知識

arealclimber commented 1 day ago

相關流程圖

arealclimber commented 1 day ago

備份 staging db 之後復原在 new db 測試遷移成功

arealclimber commented 1 day ago

took 6 hrs done