aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/mihi/src/lib.rs26
-rw-r--r--lib/mihi/src/migrate.rs102
2 files changed, 5 insertions, 123 deletions
diff --git a/lib/mihi/src/lib.rs b/lib/mihi/src/lib.rs
index b6abf2f..e965fdf 100644
--- a/lib/mihi/src/lib.rs
+++ b/lib/mihi/src/lib.rs
@@ -10,8 +10,6 @@ use rusqlite::types::{FromSql, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
use rusqlite::Result;
use rusqlite::{params, Connection};
-mod migrate;
-
/// Returns the configuration path for the application, and it even creates it
/// if it doesn't exist already.
pub fn get_config_path() -> Result<PathBuf, String> {
@@ -337,21 +335,6 @@ pub fn add_language(language: String) -> Result<(), String> {
}
}
-/// Ensure that in the config path there is a fully initialized database.
-pub fn init_database() -> Result<(), String> {
- let name = &std::env::var("MIHI_DATABASE").unwrap_or("database.sqlite3".to_string());
- let path = get_config_path()?.join(name);
- let conn = match Connection::open(path) {
- Ok(handle) => handle,
- Err(e) => return Err(format!("could not initialize the database: {e}")),
- };
-
- match migrate::init(conn) {
- Ok(_) => Ok(()),
- Err(e) => Err(format!("bad database schema file: {e}")),
- }
-}
-
/// Defines in which way two words are related.
#[derive(Clone, Debug)]
pub enum RelationKind {
@@ -1245,11 +1228,12 @@ fn get_connection() -> Result<rusqlite::Connection, String> {
let name = &std::env::var("MIHI_DATABASE").unwrap_or("database.sqlite3".to_string());
let path = get_config_path()?.join(name);
- match Connection::open(path) {
+ match Connection::open(&path) {
Ok(handle) => Ok(handle),
- Err(_) => Err(
- "could not fetch the database. Ensure that you have called 'init' first".to_string(),
- ),
+ Err(_) => Err(format!(
+ "could not fetch the database in '{}'",
+ path.display()
+ )),
}
}
diff --git a/lib/mihi/src/migrate.rs b/lib/mihi/src/migrate.rs
deleted file mode 100644
index b1ff0c3..0000000
--- a/lib/mihi/src/migrate.rs
+++ /dev/null
@@ -1,102 +0,0 @@
-use rusqlite::{Connection, Result};
-
-/// Use the given `connection` in order to initialize the database.
-pub fn init(connection: Connection) -> Result<usize> {
- connection.execute(
- r#"
-CREATE TABLE IF NOT EXISTS "words" (
- "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
- "particle" varchar,
- "enunciated" varchar,
- "declension_id" integer,
- "conjugation_id" integer,
- "kind" varchar,
- "category" integer,
- "regular" boolean DEFAULT 1,
- "locative" boolean DEFAULT 0,
- "gender" integer,
- "created_at" datetime(6) NOT NULL,
- "updated_at" datetime(6) NOT NULL,
- "suffix" varchar,
- "language_id" integer NOT NULL,
- "succeeded" integer DEFAULT 0 NOT NULL,
- "steps" integer DEFAULT 0 NOT NULL,
- "translation" jsonb DEFAULT '{}',
- "pending" boolean DEFAULT 0,
- "flags" jsonb DEFAULT '{}',
- "weight" integer DEFAULT 0 NOT NULL,
-
- CHECK (weight >= 0 AND weight <= 10),
-
- FOREIGN KEY ("conjugation_id") REFERENCES "conjugations" ("id"),
- FOREIGN KEY ("declension_id") REFERENCES "declensions" ("id")
-);
-"#,
- (),
- )?;
-
- connection.execute(
- r#"
-CREATE UNIQUE INDEX IF NOT EXISTS "index_words_on_enunciated" ON "words" ("enunciated");
-"#,
- (),
- )?;
-
- connection.execute(
- r#"
-CREATE TABLE IF NOT EXISTS "exercises" (
- "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
- "title" varchar NOT NULL,
- "enunciate" text NOT NULL,
- "solution" text NOT NULL,
- "lessons" text NOT NULL,
- "kind" integer DEFAULT 0,
- "created_at" datetime(6) NOT NULL,
- "updated_at" datetime(6) NOT NULL
-);
-"#,
- (),
- )?;
-
- connection.execute(
- r#"
-CREATE UNIQUE INDEX IF NOT EXISTS "index_exercises_on_title" ON "exercises" ("title");
-"#,
- (),
- )?;
-
- connection.execute(
- r#"
-CREATE TABLE IF NOT EXISTS "tags" (
- "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
- "name" varchar NOT NULL,
- "created_at" datetime(6) NOT NULL,
- "updated_at" datetime(6) NOT NULL
-);
-"#,
- (),
- )?;
-
- connection.execute(
- r#"
-CREATE TABLE IF NOT EXISTS "tag_associations" (
- "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
- "word_id" integer NOT NULL,
- "tag_id" integer NOT NULL,
- "created_at" datetime(6) NOT NULL,
- "updated_at" datetime(6) NOT NULL
-);
-"#,
- (),
- )?;
-
- connection.execute(
- r#"
-CREATE UNIQUE INDEX IF NOT EXISTS "index_tags_on_name" ON "tags" ("name");
-CREATE UNIQUE INDEX IF NOT EXISTS "word_tag_unique" ON tag_associations (word_id, tag_id);
-"#,
- (),
- )?;
-
- Ok(0)
-}