arch golang-migrate
Install the golang-migrate tool by running the following command in your terminal:
go install -tags 'sqlite3' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
Initialize a new migration files directory by executing the following command:
migrate create -ext sql -dir db/migration -seq <migration_name>
Write your migration SQL in the newly created migration file.
Apply the migrations to your database using the following command:
migrate -database 'postgres://user:password@localhost:5432/database_name?sslmode=disable' -path ./db/migration up
Roll back the last migration with the following command:
migrate -database 'postgres://user:password@localhost:5432/database_name?sslmode=disable' -path ./db/migration down
Check the status of your migrations with the command:
migrate -database 'postgres://user:password@localhost:5432/database_name?sslmode=disable' -path ./db/migration version