Getting Began #
On this tutorial, you’ll discover ways to use the migrate instrument (written in Go and fairly fashionable in Golang neighborhood) to execute database migrations. As a second half, you’ll write some Go code to learn the info from the database.
You’ll use PostgreSQL because the database of alternative on this article, however migrate is suitable with many extra databases take a look at the listing right here.
Setup
Set up
migrate
is a cli instrument that’s used to run migrations, it may also be used programmatically, however on this tutorial we’re going to use it by way of cli. There are a number of methods to put in the migrate
cli, similar to brew, scoop, and so on. Check out the set up doc to see all of the out there choices.
We’re going to set up it utilizing go set up
.
go set up -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/[email protected]
Database
To run the pattern Postgres database, I’ve supplied you with a docker-compose.yaml file. Clone the github challenge and run docker compose up. It will run a Postgres database on port 5454.
You’re free to setup a Postgres DB as you want. There is nothing particular within the above setup.
Creating Golang Migrations
You’ll create a posts
desk which must 2 columns, title and physique.
To create a brand new migration run the migrate create
command with acceptable choices.
migrate create -ext sql -dir db/migrations create_posts_table
- ext specifies the file extension to make use of when creating migrations file.
- dir specifies which listing to create the migrations in.
It will create two migrations in db/migrations
folder matching the sample: <timestamp>_create_posts_table.down.sql
and <timestamp>_create_posts_table.up.sql
.
- UP migration will comprise the sql to create the put up desk.
- DOWN migration will comprise the sql to revert what has been finished within the up migration.
Writing SQL
Within the <timestamp>_create_posts_table.up.sql
migration file write the sql to create posts desk.
CREATE TABLE IF NOT EXISTS posts (title varchar, physique varchar);
Within the <timestamp>_create_posts_table.down.sql
migration file write the sql to drop posts desk.
DROP TABLE IF EXISTS posts;
Operating migrations
migrate wants a solution to join the database to execute the sql statements. For this you have to a legitimate postgres connection string following the format:
postgres://<username>:<password>@localhost:<port>/<db_name>?sslmode=disable
In case you are utilizing the database utilizing the docker-compose.yaml
technique specified above, the connection string will seem like this:
postgres://postgres:[email protected]:5454/postgres?sslmode=disable
To run migrations use migrate up
command with the suitable choices.
export DB_URL='postgres://postgres:[email protected]:5454/postgres?sslmode=disable'
# Run migrations
migrate -database ${DB_URL} -path db/migrations up
Rolling again migrations
To roll again all of the migrations i.e. execte all of the *.down.sql
recordsdata you employ the migration down
command.