016.Start Rails project from existing db.

Thach
Written by Thach on
016.Start Rails project from existing db.

Vừa rồi tôi được phân vào một dự án renew một web PHP + MySQL, và khi migrate hệ thống sang Rails, tôi muốn dùng Postgres thay vì dùng Rails với MySQL.

Sau một lúc research, thì tôi tìm thấy Pgloader.

Simple migrate data

Đầu tiên là cài đặt Pgloader, may mắn là chúng ta có thể cài bằng Homebrew trên Mac

brew update && brew install pgloader

Giờ để chuyển data và cả copy structure từ DB cũ, đơn giản nhất thì chúng ta có thể dùng lệnh như sau, tất nhiên là cần thay các giá trị user, password, host, db_name.

pgloader --debug mysql://mysql_user:mysql_pw@localhost/mysql_db_name postgresql://postgres_user:postgres_pw@localhost/postgres_db_name

Something you may want to know

Khi mà migrate từ MySQL sang Postgres, các tables sẽ nằm trong một schema không phải public như chúng ta thường có. Để có thể chuyển về public, chúng ta sẽ đặt trong một file migration_config.load như thế này

LOAD DATABASE
  FROM mysql://mysql_user:mysql_pw@localhost/mysql_db_name
  INTO postgres://postgres_user:postgres_pw@localhost/postgres_db_name
ALTER SCHEMA 'mysql_db_name' RENAME TO 'public';

Và chạy lệnh migrate như sau

pgloader --debug migration_config.load

Lỗi tràn bộ nhớ

Khi mà data trong DB quá nhiều, các bạn có thể gặp lỗi Heap exhausted during load data into ...

Để sửa lỗi này thì thêm option prefetch rows trong file migration_config.load

LOAD DATABASE
  FROM mysql://mysql_user:mysql_pw@localhost/mysql_db_name
  INTO postgres://postgres_user:postgres_pw@localhost/postgres_db_name
  WITH prefetch rows = 10000
ALTER SCHEMA 'mysql_db_name' RENAME TO 'public';

Lỗi authen của MySQL user

Khi dùng MySQL 8, có thể các bạn sẽ gặp lỗi MYSQL-UNSUPPORTED-AUTHENTICATION was signalled

Theo các bài hướng dẫn thì các bạn edit file my.cnf, thêm vào trong cụm[mysql]

default-authentication-plugin=mysql_native_password

Restart Mysql và sau đó sửa lại quyền cho user

mysql -u root -p
# nhập mysql_pw rồi sửa mode authen cho user
ALTER USER 'mysql_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mysql_pw';
# hoặc đơn giản là tạo thêm một user khác
CREATE USER 'new_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Tạo migration cho DB

Sau khi đã migration DB hoàn tất, vì chúng ta không có file migrate để tạo cấu trúc data như hiện tại, chúng ta nên tạo một cái. May mắn là Rails có hỗ trợ sẵn chúng ta.

rake db:schema:dump

Copy phần create_table và những index trong đó. Sau đó tạo một file migrate, paste vào phần change và chạy migrate

rails g migration InititalDatabase
class InitialDataStructure < ActiveRecord::Migration[7.0]
  def change
    #copy schema content here (create_table, add_foreign_key, create_enum)
  end
end

rails db:migrate

Và xong, giờ chúng ta có thể sử dụng như một DB postgres bình thường

Comments

comments powered by Disqus