Skip to content
Extraits de code Groupes Projets
Vérifiée Valider e33d9dc5 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

RUST: Run the diesel run migration command on a non existing db and generate...

RUST: Run the diesel run migration command on a non existing db and generate the schema.rs file if needed

NOTE: A build.rs file should not write files in the source folder, but
      it's easier to do it that way for now. We migh want to do
      something with cmake otherwise...
parent b97b3a8c
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -60,7 +60,13 @@ If you are developping for lektor, you will need the ...@@ -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, [clang-format](https://releases.llvm.org/download.html) command line utility,
for a beautifull and uniform C++ code style. for a beautifull and uniform C++ code style.
[Here](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) is the list of [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 ### Building instructions
......
...@@ -8,4 +8,4 @@ crate-type = [ "staticlib" ] ...@@ -8,4 +8,4 @@ crate-type = [ "staticlib" ]
[dependencies] [dependencies]
libc = "0.2.0" libc = "0.2.0"
diesel = { version = "2", features = [ "sqlite" ] } diesel = { version = "2", default-features = false, features = [ "sqlite" ] }
\ No newline at end of file
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");
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# see https://diesel.rs/guides/configuring-diesel-cli # see https://diesel.rs/guides/configuring-diesel-cli
[print_schema] [print_schema]
file = "src/schema.rs" file = "src/database/schema.rs"
[migrations_directory] [migrations_directory]
dir = "migrations" dir = "migrations"
...@@ -5,5 +5,4 @@ DROP TABLE kara_makers; ...@@ -5,5 +5,4 @@ DROP TABLE kara_makers;
DROP TABLE kara_tag; DROP TABLE kara_tag;
DROP TABLE tag; DROP TABLE tag;
DROP TABLE kara_tags; DROP TABLE kara_tags;
DROP TABLE queue_1;
DROP TABLE history; DROP TABLE history;
CREATE TABLE repo CREATE TABLE repo
( name TEXT NOT NULL ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
, id INTEGER NOT NULL , name TEXT NOT NULL UNIQUE
); );
CREATE TABLE repo_kara CREATE TABLE repo_kara
...@@ -29,7 +29,7 @@ CREATE TABLE kara_makers ...@@ -29,7 +29,7 @@ CREATE TABLE kara_makers
CREATE TABLE tag CREATE TABLE tag
( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
, name TEXT NOT NULL , name TEXT NOT NULL UNIQUE
); );
CREATE TABLE kara_tags CREATE TABLE kara_tags
...@@ -39,13 +39,7 @@ CREATE TABLE kara_tags ...@@ -39,13 +39,7 @@ CREATE TABLE kara_tags
, PRIMARY KEY (kara_id, tag_id, value) , 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 CREATE TABLE history
( id INTEGER NOT NULL REFERENCES kara(id) ON DELETE CASCADE ( id INTEGER NOT NULL REFERENCES kara(id) ON DELETE CASCADE
, epoch INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT , epoch INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
, PRIMARY KEY (id, epoch)
); );
pub(self) use diesel::prelude::*;
pub mod models;
pub mod schema;
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,
}
// @generated automatically by Diesel CLI. // @generated automatically by Diesel CLI.
diesel::table! {
history (epoch) {
id -> Integer,
epoch -> Integer,
}
}
diesel::table! { diesel::table! {
kara (id) { kara (id) {
id -> Integer, id -> Integer,
is_dl -> Bool,
song_title -> Text, song_title -> Text,
song_type -> Text, song_type -> Text,
song_origin -> Text, song_origin -> Text,
source_name -> Text, source_name -> Text,
language -> Text, language -> Text,
kara_hash -> Text,
} }
} }
...@@ -26,6 +35,21 @@ diesel::table! { ...@@ -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! { diesel::table! {
tag (id) { tag (id) {
id -> Integer, id -> Integer,
...@@ -33,13 +57,19 @@ diesel::table! { ...@@ -33,13 +57,19 @@ diesel::table! {
} }
} }
diesel::joinable!(history -> kara (id));
diesel::joinable!(kara_makers -> kara (id)); diesel::joinable!(kara_makers -> kara (id));
diesel::joinable!(kara_tags -> kara (kara_id)); diesel::joinable!(kara_tags -> kara (kara_id));
diesel::joinable!(kara_tags -> tag (tag_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!( diesel::allow_tables_to_appear_in_same_query!(
history,
kara, kara,
kara_makers, kara_makers,
kara_tags, kara_tags,
repo,
repo_kara,
tag, tag,
); );
#![allow(dead_code)] #![allow(dead_code, unused_imports)]
mod compat; mod compat;
mod database; mod database;
mod mkv;
mod module; mod module;
pub(crate) use compat::*; pub(crate) use compat::*;
......
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,
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter