Go/migrate: Difference between revisions

From Fundamental Ramen
< Go
Jump to navigation Jump to search
(Created page with "== Best practice == === up === === down === === create === == DSN Problem ==")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Best practice ==
== Best practice ==


=== up ===
{| class="wikitable"
! Purpose || Command
|-
| create ||
<syntaxhighlight lang="bash">
migrate create -ext sql -dir assets/migrations -seq {{.CLI_ARGS}}
</syntaxhighlight>
|-
| up ||
<syntaxhighlight lang="bash">
migrate -path assets/migrations -database "postgres://sysop:mypass@localhost:45432/mydb" up
</syntaxhighlight>
|-
| down ||
<syntaxhighlight lang="bash">
migrate -path assets/migrations -database "postgres://sysop:mypass@localhost:45432/mydb" down
</syntaxhighlight>
|}


== Taskfile.yaml ==


=== down ===
<syntaxhighlight lang="yaml">
  mig-up:
    desc: "Use golang-migrate to upgrade db"
    cmds:
      - migrate -path assets/migrations -database "postgres://sysop:mypass@localhost:45432/mydb" up


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


=== create ===
  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 "20060102" -tz "Asia/Taipei" {{.CLI_ARGS}}
      # by sequence number
      - migrate create -ext sql -dir assets/migrations -seq {{.CLI_ARGS}}
</syntaxhighlight>


== DSN Problem ==
== DSN Problem ==

Latest revision as of 10:30, 28 July 2026

Best practice

Purpose Command
create
migrate create -ext sql -dir assets/migrations -seq {{.CLI_ARGS}}
up
migrate -path assets/migrations -database "postgres://sysop:mypass@localhost:45432/mydb" up
down
migrate -path assets/migrations -database "postgres://sysop:mypass@localhost:45432/mydb" down

Taskfile.yaml

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

  mig-down:
    desc: "Use golang-migrate to downgrade db"
    cmds:
      - migrate -path assets/migrations -database "postgres://sysop:mypass@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 "20060102" -tz "Asia/Taipei" {{.CLI_ARGS}}
      # by sequence number
      - migrate create -ext sql -dir assets/migrations -seq {{.CLI_ARGS}}

DSN Problem