Skip to content

Commit

Permalink
Remove dependency on migrant
Browse files Browse the repository at this point in the history
  • Loading branch information
mrozycki committed Oct 26, 2024
1 parent 510db88 commit 3263e63
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Rustmas.toml
webui/dist

*.sqlite
*-shm
*-wal

.DS_Store
.idea
2 changes: 1 addition & 1 deletion Rustmas.example.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# path to an sqlite database, for parameter storage
database_path = "webapi/db.sqlite"
database_path = "db.sqlite"

[controller]
# path to CSV file with light positions
Expand Down
Binary file added db.sqlite.example
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE
CREATE TABLE IF NOT EXISTS
animation_parameters (
animation TEXT PRIMARY KEY,
parameters TEXT
Expand Down
9 changes: 4 additions & 5 deletions webapi/DEPLOYMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ Getting and building the code
Database setup
--------------

Similarly to local development setup, you will need to set up the database using
the [migrant CLI](https://crates.io/crates/migrant). Run the following from the `webapi` directory:
Similarly to local development setup, you will need to provide an empty SQLite
database file. One has been provided in the repository as [`db.sqlite.example`](../db.sqlite.example)
in the root of the project. You just need to make a copy named `db.sqlite`.

```
cargo install migrant --features sqlite
migrant setup
migrant apply
cp db.sqlite.example db.sqlite
```

Alternatively, you can just use the `db.sqlite` file from your local development setup.
Expand Down
11 changes: 0 additions & 11 deletions webapi/Migrant.toml

This file was deleted.

21 changes: 8 additions & 13 deletions webapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"
```

Then you will need to install `migrant` for running database migrations,
`trunk` to serve the frontend code and add a WASM target for Rust to compile
the frontend. This can be done with:
Then you will need to install `trunk` to serve the frontend code and add
a WASM target for Rust to compile the frontend. This can be done with:

```
cargo install migrant --features sqlite
cargo install trunk
rustup target add wasm32-unknown-unknown
```
Expand All @@ -46,19 +44,16 @@ sudo apt install libopencv-dev

### Database

Rustmas WebAPI uses an SQLite database to store animation parameter values. Migrations for that
database are provided in the `migrations` directory. You can run them using
the [migrant CLI](https://crates.io/crates/migrant) from the `webapi` directory:
Rustmas WebAPI uses an SQLite database to store animation parameter values.
It runs appropriate migrations at startup, so for an initial run you only need
to provide an empty SQLite database file. One has been provided in the repository
as [`db.sqlite.example`](../db.sqlite.example) in the root of the project.
You just need to make a copy named `db.sqlite`.

```
cd webapi
migrant setup
migrant apply
cd -
cp db.sqlite.example db.sqlite
```

This will produce a `db.sqlite` file with appropriate tables set up.

### Configuration file

In order to run WebAPI locally, you need to create a Rustmas.toml file.
Expand Down
3 changes: 3 additions & 0 deletions webapi/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("cargo::rerun-if-changed=migrations");
}
4 changes: 3 additions & 1 deletion webapi/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ pub struct Db {

impl Db {
pub async fn new(path: &str) -> Result<Self, Box<dyn Error>> {
let conn = SqliteConnectOptions::from_str(path)?
let mut conn = SqliteConnectOptions::from_str(path)?
.disable_statement_logging()
.connect()
.await?;

sqlx::migrate!("../migrations").run(&mut conn).await?;

Ok(Self {
conn: Arc::new(Mutex::new(conn)),
})
Expand Down
10 changes: 5 additions & 5 deletions webapi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ async fn main() -> Result<(), Box<dyn Error>> {

let (sender, receiver) = mpsc::channel::<lightfx::Frame>(1);

info!("Setting up database");
let db = web::Data::new(Db(
db::Db::new(&config.database_path.to_string_lossy()).await?
));

info!("Starting controller");
let controller = {
let mut controller = rustmas_animator::Controller::builder_from(config.controller)?
Expand All @@ -306,11 +311,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
web::Data::new(AnimationController(Mutex::new(controller)))
};

info!("Establishing database connection");
let db = web::Data::new(Db(
db::Db::new(&config.database_path.to_string_lossy()).await?
));

let frame_broadcaster = web::Data::new(FrameBroadcaster::new(receiver).start());

HttpServer::new(move || {
Expand Down

0 comments on commit 3263e63

Please sign in to comment.