Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
advotracker - database handling
advotracker will make use of Diesel to handle SQL access functions.
Diesel will requires Rust 1.24 or later.
Diesel Setup
First of all, add the required dependencies to your projects Cargo.toml
[dependencies]
diesel = { version = "1.4.4", features = ["sqlite" "chrono"] }
dotenv = "0.15.0"
CLI helper
Diesel provides a separete CLI tool to help manage the project. This CLI should be installed on your system
cargo install diesel_cli
cargo install diesel_cli --no-default-features --features sqlite3
Database location
We do uses sqlite for now. Use your distro package for sqlite3.
Now direct diesel to the correct database instance. We can create an
environment variable DATABASE_URL, that will point to the instance.
For the test setup, we take advantage of an .env file:
DATABASE_URL=sqlite://username:password@localhost/advotracker
#DATABASE_URL=postgres://username:password@localhost/advotracker
Setup
When managing the database schema, it will evolve over the time.
Diesel takes that into account and supports a bundles called Migrations.
They allow us define states that can be applied (up.sql) or reverted (down.sql).
Applying and immediately reverting a migration should leave your database schema unchanged.
diesel setup
diesel migration generate advotracker
The call will create the needed structure with as a subfolder migrations,
holding a timstamp based subfolder with the migration name and to empty files.
You'll see output that looks something like this:
Creating migrations/20200625133000_advotracker/up.sql
Creating migrations/20200625133000_advotracker/down.sql
We can apply our new migration:
diesel migration run
To test out, that our sql scripts are working out as expected try the redo
function. This will drop the given schema and create a new one using the
adopted files in the migration path.
diesel migration run
Further infomation can be optained from [Diesel's project page]1 .
This work is licensed under a Creative Common License 4.0
© 2020 Ralf Zerres, Networkx GmbH
Foodnotes
-
Diesel Guides: https://diesel.rs/guides/getting-started/ ↩︎
