1, Quản lý migration : golang-migrate
Fiber KHÔNG có migration built-in như Laravel, nhưng bạn hoàn toàn có thể dùng migration thông qua các tool bên ngoài (rất phổ biến trong Go). Và phổ biến nhất trong đó là
|
1 |
https://github.com/golang-migrate/migrate |
Cách cài đặt
|
1 |
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest |
Tạo file migration
|
1 |
migrate create -ext sql -dir db/migrations -seq create_users_table |
Khởi tạo lệnh sẽ tạo ra 2 file
000001_create_users_table.up.sql
000002_create_users_table.down.sql
Ví dụ về nội dung up
|
1 2 3 4 5 6 7 8 9 |
CREATE TABLE users ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), email VARCHAR(255) UNIQUE, phone VARCHAR(20) UNIQUE, password_hash VARCHAR(255), created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW(), last_login_at TIMESTAMPTZ ); |
Ví dụ về nội dung down
|
1 |
DROP TABLE users; |
Chạy migration
|
1 |
migrate -database YOUR_DATABASE_URL -path PATH_TO_YOUR_MIGRATIONS up |
2, QUản lý biến môi trường godotenv
Cài đặt
|
1 |
go get github.com/joho/godotenv |