Skip to main content

Database Migrations

Trajectory Trace uses diesel.rs for applying database migrations. You'll need to install the diesel-cli, a guide on how to is here. The diesel-cli reads the DATABASE_URL environment variable to connect to a database. To connect to the local database instance use:

export DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres"

Running the command diesel migration run in the trajectory-trace directory will apply all pending migrations to the database.

info

Running the trajectory-trace binary will also auto-apply all pending migrations.

Creating a new Migration

Creating a new migrations requires you to run the diesel migration generate <migration_name> command. This will generate two scripts in the ./migrations/<migration_name>/ directory. One is called up.sql and allows you to invoke one or more SQL instructions for building your migration. The second file called down.sql needs to operations applied in the up.sql script.

To confirm that the migration works as expected, you can use the diesel migration up and diesel migration down command to apply and revert the migration.