Database Migrations
Diesel
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"
In case you use the provided flake.nix development environment by running the command nix develop or having direnv enabled,
the DATABASE_URL will be automatically configured for a local timescale deployment.
DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres"
Migrations
Migrations are automatically ran on trajectory-trace start.
Migration source code is stored in this directory.
Each sub-directory (i.e. migration) contains a up.sql and down.sql file, which contains the migration operations and a way to reverse them.
The diesel-cli is used to apply migations manually. Typically you wouldn't need to, but you can.
# the trajectory-trace subdirectory contains the migrations directory
cd trajectory-trace
diesel migration run
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.
