Migrations - green-ecolution/backend GitHub Wiki

Migrations are structured changes to a database that can be tracked and managed over time. Migrations also make it easy to roll back changes if needed, helping to maintain data integrity and keep the database in sync across different environments. This project uses goose, a database migration tool, to manage the migrations.

Migrations always need up and down statements:

  • up: Defines the changes to apply to the database (e.g., adding a table or altering a schema).
  • down: Provides the rollback logic to undo the changes made by the up statement, ensuring you can revert to a previous state if necessary.

All migrations can be found in the /internal/storage/postgres/migrations directory.

Commands

# create a new migration
make migrate/new name="name_of_the_new_migration"

# apply latest database changes
make migrate/up

# reverse last applied migration
make migrate/down

#resets all migrations by rolling back and reapplying them
make migrate/reset

# displays the current migration status
make migrate/status