Go/migrate: Difference between revisions

From Fundamental Ramen
< Go
Jump to navigation Jump to search
Line 11: Line 11:
| up ||
| up ||
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
migrate -path assets/migrations -database "postgres://sysop:mypass@localhost:45432/mydb" up
migrate -path assets/migrations -database {{.DSN}} up
</syntaxhighlight>
</syntaxhighlight>
|-
|-
| down ||
| down ||
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
migrate -path assets/migrations -database "postgres://sysop:mypass@localhost:45432/mydb" down
migrate -path assets/migrations -database {{.DSN}} down
</syntaxhighlight>
</syntaxhighlight>
|}
|}

Revision as of 01:18, 31 July 2026

Best practice

Purpose Command
create
migrate create -ext sql -dir assets/migrations -seq {{.CLI_ARGS}}
up
migrate -path assets/migrations -database {{.DSN}} up
down
migrate -path assets/migrations -database {{.DSN}} down

Taskfile.yaml

  mig-up:
    desc: "Use golang-migrate to upgrade db"
    cmds:
      - migrate -path assets/migrations -database "postgres://sysop@localhost:45432/mydb" up

  mig-down:
    desc: "Use golang-migrate to downgrade db"
    cmds:
      - migrate -path assets/migrations -database "postgres://sysop@localhost:45432/mydb" down

  mig-create:
    desc: "Use golang-migrate to create up/down file (Usage: task mig-create -- NAME)"
    CLI_ARGS: true
    cmds:
      # by date
      # - migrate create -ext sql -dir assets/migrations -format "060102" -tz "Asia/Taipei" {{.CLI_ARGS}}
      # by sequence number
      - migrate create -ext sql -dir assets/migrations -seq {{.CLI_ARGS}}