diff --git a/README.md b/README.md index 001b8f52e0e5f130e687fb815ef37541c46c475a..6beb801c253347b96c3810f2228224dbb32d9cc3 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,13 @@ If you are developping for lektor, you will need the [clang-format](https://releases.llvm.org/download.html) command line utility, for a beautifull and uniform C++ code style. [Here](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) is the list of -all the options to put in the `.clang-format` file. +all the options to put in the `.clang-format` file. You will also need diesel +for all the database stuff, use the following command to install diesel cli with +the correct sqlite support on your system: + +```sh +cargo install diesel_cli --no-default-features --features sqlite +``` ### Building instructions diff --git a/src/rust/liblektor-rs/Cargo.toml b/src/rust/liblektor-rs/Cargo.toml index 58bfc5710ed29173cbdb303a18c8b523f15ffb12..7e9d12c79edd258b45790c32e2433e751ad17049 100644 --- a/src/rust/liblektor-rs/Cargo.toml +++ b/src/rust/liblektor-rs/Cargo.toml @@ -8,4 +8,4 @@ crate-type = [ "staticlib" ] [dependencies] libc = "0.2.0" -diesel = { version = "2", features = [ "sqlite" ] } \ No newline at end of file +diesel = { version = "2", default-features = false, features = [ "sqlite" ] } diff --git a/src/rust/liblektor-rs/build.rs b/src/rust/liblektor-rs/build.rs new file mode 100644 index 0000000000000000000000000000000000000000..91f293dbe667d28324ada51171434898655515f9 --- /dev/null +++ b/src/rust/liblektor-rs/build.rs @@ -0,0 +1,54 @@ +use std::{ + env, + path::Path, + process::{Command, Stdio}, +}; + +macro_rules! cmd { + ($exec: literal => $args: tt) => {{ + let output = Command::new($exec) + .args($args) + .stdout(Stdio::piped()) + .spawn() + .expect(format!("failed to run {}", $exec).as_str()) + .wait_with_output() + .expect(format!("failed to wait for {}", $exec).as_str()); + assert!(output.status.success()); + }}; +} + +fn rerun_directory<T: AsRef<Path> + ?Sized>(dir: &T) { + println!("cargo:rerun-if-changed={}", dir.as_ref().to_string_lossy()); + for entry in std::fs::read_dir(dir).unwrap() { + let path = entry.expect("Couldn't access file in src directory").path(); + if path.is_dir() { + rerun_directory(&path); + } + } +} + +fn main() { + // Because the target folder was fixed, we know where the source folder is! + let out_dir = env::var_os("OUT_DIR").unwrap(); + let out_dir = Path::new(&out_dir); + let db_path = out_dir + .join("kara.db") + .canonicalize() + .expect("failed to canonicalize the sample kara database path"); + let source_dir = out_dir + .join("../../../../../liblektor-rs") + .canonicalize() + .expect("failed to canonicalize the source path"); + let migration_dir = source_dir + .join("migrations") + .canonicalize() + .expect("failed to canonicalize the migration dir"); + + let _ = std::fs::remove_file(&db_path); + env::set_var("DATABASE_URL", db_path); + env::set_current_dir(source_dir).expect("failed to cwd to source folder!"); + cmd!( "diesel" => [ "migration", "run" ] ); + + rerun_directory(&migration_dir); + println!("cargo:rerun-if-changed=build.rs"); +} diff --git a/src/rust/liblektor-rs/diesel.toml b/src/rust/liblektor-rs/diesel.toml index 35a12ff0dbc6cb89ff1d400570145f9ed5fcda54..8de80930237919ba388f7d7450085774945cac77 100644 --- a/src/rust/liblektor-rs/diesel.toml +++ b/src/rust/liblektor-rs/diesel.toml @@ -2,7 +2,7 @@ # see https://diesel.rs/guides/configuring-diesel-cli [print_schema] -file = "src/schema.rs" +file = "src/database/schema.rs" [migrations_directory] dir = "migrations" diff --git a/src/rust/liblektor-rs/migrations/.keep b/src/rust/liblektor-rs/migrations/.keep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/rust/liblektor-rs/migrations/2022-09-30-204512_create_posts/down.sql b/src/rust/liblektor-rs/migrations/2022-09-30-204512_initial/down.sql similarity index 88% rename from src/rust/liblektor-rs/migrations/2022-09-30-204512_create_posts/down.sql rename to src/rust/liblektor-rs/migrations/2022-09-30-204512_initial/down.sql index 5a2e360002d48d846f949ea2695257288c7b9033..9a50b90634bf40082be5b3a8b2c256fe1bbba947 100644 --- a/src/rust/liblektor-rs/migrations/2022-09-30-204512_create_posts/down.sql +++ b/src/rust/liblektor-rs/migrations/2022-09-30-204512_initial/down.sql @@ -5,5 +5,4 @@ DROP TABLE kara_makers; DROP TABLE kara_tag; DROP TABLE tag; DROP TABLE kara_tags; -DROP TABLE queue_1; DROP TABLE history; diff --git a/src/rust/liblektor-rs/migrations/2022-09-30-204512_create_posts/up.sql b/src/rust/liblektor-rs/migrations/2022-09-30-204512_initial/up.sql similarity index 82% rename from src/rust/liblektor-rs/migrations/2022-09-30-204512_create_posts/up.sql rename to src/rust/liblektor-rs/migrations/2022-09-30-204512_initial/up.sql index 33a7c418d772ccd4fe40c7638080ddec3cf91c8b..a4ec97be9d176b4ebcfbaa21db758f54b08c2261 100644 --- a/src/rust/liblektor-rs/migrations/2022-09-30-204512_create_posts/up.sql +++ b/src/rust/liblektor-rs/migrations/2022-09-30-204512_initial/up.sql @@ -1,6 +1,6 @@ CREATE TABLE repo - ( name TEXT NOT NULL - , id INTEGER NOT NULL + ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT + , name TEXT NOT NULL UNIQUE ); CREATE TABLE repo_kara @@ -29,7 +29,7 @@ CREATE TABLE kara_makers CREATE TABLE tag ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT - , name TEXT NOT NULL + , name TEXT NOT NULL UNIQUE ); CREATE TABLE kara_tags @@ -39,13 +39,7 @@ CREATE TABLE kara_tags , PRIMARY KEY (kara_id, tag_id, value) ); -CREATE TABLE queue_1 - ( id INTEGER NOT NULL REFERENCES kara(id) ON DELETE CASCADE - , position INTEGER NOT NULL CHECK(position > 0) - ); - CREATE TABLE history ( id INTEGER NOT NULL REFERENCES kara(id) ON DELETE CASCADE , epoch INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT - , PRIMARY KEY (id, epoch) ); diff --git a/src/rust/liblektor-rs/src/database/mod.rs b/src/rust/liblektor-rs/src/database/mod.rs index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8ea4ab3902e2f6c320f130e737e122c51cdeb9af 100644 --- a/src/rust/liblektor-rs/src/database/mod.rs +++ b/src/rust/liblektor-rs/src/database/mod.rs @@ -0,0 +1,4 @@ +pub(self) use diesel::prelude::*; + +pub mod models; +pub mod schema; diff --git a/src/rust/liblektor-rs/src/database/models.rs b/src/rust/liblektor-rs/src/database/models.rs new file mode 100644 index 0000000000000000000000000000000000000000..d6727d48d878ba1d0d8df96e475b9e7fbda2a04b --- /dev/null +++ b/src/rust/liblektor-rs/src/database/models.rs @@ -0,0 +1,11 @@ +use crate::database::{schema::*, *}; + +#[derive(Insertable)] +#[diesel(table_name = kara)] +pub struct NewKara<'a> { + pub song_title: &'a str, + pub song_type: &'a str, + pub song_origin: &'a str, + pub source_name: &'a str, + pub language: &'a str, +} diff --git a/src/rust/liblektor-rs/src/schema.rs b/src/rust/liblektor-rs/src/database/schema.rs similarity index 58% rename from src/rust/liblektor-rs/src/schema.rs rename to src/rust/liblektor-rs/src/database/schema.rs index fda66f5553d1b4bc772b42e51908fae6af3a2e00..f429bd48c796e16ffbe09d353e73f83fc19c915c 100644 --- a/src/rust/liblektor-rs/src/schema.rs +++ b/src/rust/liblektor-rs/src/database/schema.rs @@ -1,13 +1,22 @@ // @generated automatically by Diesel CLI. +diesel::table! { + history (epoch) { + id -> Integer, + epoch -> Integer, + } +} + diesel::table! { kara (id) { id -> Integer, + is_dl -> Bool, song_title -> Text, song_type -> Text, song_origin -> Text, source_name -> Text, language -> Text, + kara_hash -> Text, } } @@ -26,6 +35,21 @@ diesel::table! { } } +diesel::table! { + repo (id) { + id -> Integer, + name -> Text, + } +} + +diesel::table! { + repo_kara (repo_id, repo_kara_id, local_kara_id) { + repo_id -> Integer, + repo_kara_id -> Integer, + local_kara_id -> Integer, + } +} + diesel::table! { tag (id) { id -> Integer, @@ -33,13 +57,19 @@ diesel::table! { } } +diesel::joinable!(history -> kara (id)); diesel::joinable!(kara_makers -> kara (id)); diesel::joinable!(kara_tags -> kara (kara_id)); diesel::joinable!(kara_tags -> tag (tag_id)); +diesel::joinable!(repo_kara -> kara (local_kara_id)); +diesel::joinable!(repo_kara -> repo (repo_id)); diesel::allow_tables_to_appear_in_same_query!( + history, kara, kara_makers, kara_tags, + repo, + repo_kara, tag, ); diff --git a/src/rust/liblektor-rs/src/lib.rs b/src/rust/liblektor-rs/src/lib.rs index a691be2f8675d6d90ac1513a0fbbca57fdb5cb3a..a40f844be227dcd832ad83239bf1fea3e1f7aba3 100644 --- a/src/rust/liblektor-rs/src/lib.rs +++ b/src/rust/liblektor-rs/src/lib.rs @@ -1,8 +1,7 @@ -#![allow(dead_code)] +#![allow(dead_code, unused_imports)] mod compat; mod database; -mod mkv; mod module; pub(crate) use compat::*; diff --git a/src/rust/liblektor-rs/src/mkv.rs b/src/rust/liblektor-rs/src/mkv.rs deleted file mode 100644 index 3770013ea629dfe190aa6c0365d79c41f61885b3..0000000000000000000000000000000000000000 --- a/src/rust/liblektor-rs/src/mkv.rs +++ /dev/null @@ -1,12 +0,0 @@ -use crate::*; - -#[repr(C)] -pub(crate) struct LktKaraMetadata { - song_name: [c_char; LEKTOR_TAG_MAX], - source_name: [c_char; LEKTOR_TAG_MAX], - category: [c_char; LEKTOR_TAG_MAX], - language: [c_char; LEKTOR_TAG_MAX], - author_name: [c_char; LEKTOR_TAG_MAX], - song_type: [c_char; LEKTOR_TAG_MAX], - song_number: c_int, -}