Tutorial¶
In this tutorial, we'll guide you through the basics of using Bolt by running through how you can create a migration, apply it, revert it, and see the status of your migrations.
Prerequisites¶
Before starting this tutorial, make sure you have:
- Bolt installed (see Installation)
- Docker installed (for running the database container)
Creating your first migration¶
Run the following command to create your first migration:
Output:
Upon completion, a migrations directory will be created with the following structure:
Bolt automatically creates the migrations directory and your migration scripts inside it.
Writing your migration scripts¶
Bolt utilizes plain SQL for migration scripts. Each migration file contains a
comment for the upgrade script portion with -- migrate:up and a comment for
the downgrade script with -- migrate:down.
- Open up
20240316145038_my_first_migration.sql
You'll see the following template in the file for your migration:
- Write your migration script
-- migrate:up
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);
-- migrate:down
DROP TABLE users;
Starting up a Database Container¶
We'll use Docker to run a PostgreSQL database container for this tutorial:
- Install Docker
If you don't have docker installed, view the installation instructions here.
- Startup the PostgreSQL container
docker run \
--name bolt-postgres \
--env POSTGRES_USER=bolt_user \
--env POSTGRES_PASSWORD=bolt_password \
--env POSTGRES_DB=bolt_tutorial_db \
--publish 5432:5432 \
--detach \
postgres:latest
Configuring Bolt¶
Bolt can be configured with a bolt.toml file and environment variables. For
simplicity, we'll use a bolt.toml file in this tutorial.
- Create a
bolt.tomlfile:
- Add the following contents to the file:
[database]
dsn = "postgresql://bolt_user:bolt_password@localhost:5432/bolt_tutorial_db?sslmode=disable"
driver = "postgresql"
Applying your migration¶
Apply your migration, which will execute the upgrade script:
Output:
Applying migration 20240316145038_my_first_migration..
Successfully applied migration 20240316145038_my_first_migration in 3.217875ms!
Checking Migration Status¶
To view the status of your migration:
Output:
This command displays the migration's version, name, and whether the migration was applied or not.
Verifying the Migration¶
Connect to the database container to confirm the users table was created:
bolt_tutorial_db=# \dt
List of relations
Schema | Name | Type | Owner
--------+-----------------+-------+-----------
public | bolt_migrations | table | bolt_user
public | users | table | bolt_user
(2 rows)
We can see that our users table was created successfully. We also see another
table that we didn't explicitly create called bolt_migrations. This table is
used by Bolt to keep track of which migrations have been applied to your
database. Generally, you should not modify this table manually.
Downgrading Your Migration¶
To revert your migration:
Output:
Reverting migration 20240316145038_my_first_migration..
Successfully reverted migration 20240316145038_my_first_migration in 6.283667ms!
Verifying Migration Reversion¶
Reconnect to the database container and check that the users table has been removed:
bolt_tutorial_db=# \dt
List of relations
Schema | Name | Type | Owner
--------+-----------------+-------+-----------
public | bolt_migrations | table | bolt_user
(1 row)
Creating a migration with dependencies¶
Let's create another migration that depends on the first one:
Open the new migration file. You'll see that Bolt automatically added a dependency on the previous migration:
Add the migration SQL:
-- depends: 20240316145038
-- migrate:up
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id),
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
-- migrate:down
DROP TABLE posts;
Apply the new migration:
The dependency ensures that the users table exists before creating the
posts table with its foreign key reference.
Next Steps¶
Congratulations! You've learned the core features of Bolt.
To learn more:
- Check out How-To Guides for specific tasks
- Browse the Reference for detailed command and configuration documentation
- Read the Explanation section to understand how Bolt works internally