Skip to content

Commit

Permalink
Insert languages and word types.
Browse files Browse the repository at this point in the history
  • Loading branch information
ISibboI committed Jul 30, 2023
1 parent 5bf8eae commit d5a8144
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 32 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/rvoc-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ diesel_migrations = "2.1.0"
diesel-async = { version = "0.3.2", features = ["postgres", "deadpool"] }

# wiktionary connection
wiktionary-dump-parser = "0.4.0"
wiktionary-dump-parser = "0.5.0"

# login and secrets
password-hash = "0.5.0"
Expand Down
36 changes: 14 additions & 22 deletions backend/rvoc-backend/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use crate::{
configuration::Configuration,
error::{RVocError, RVocResult},
};
use diesel::Connection;
use diesel::PgConnection;
use diesel_async::{pooled_connection::deadpool::Pool, AsyncPgConnection};
use tracing::{debug, info, instrument};

pub mod model;

const MIGRATIONS: diesel_migrations::EmbeddedMigrations = diesel_migrations::embed_migrations!();

/// Create an async connection pool to the database.
Expand All @@ -24,9 +26,7 @@ pub async fn create_async_database_connection_pool(
/// Create a sync connection to the database.
///
/// If there are pending database migrations, this method returns an error.
pub fn create_sync_database_connection(
configuration: &Configuration,
) -> RVocResult<impl Connection> {
pub fn create_sync_database_connection(configuration: &Configuration) -> RVocResult<PgConnection> {
if has_missing_migrations(configuration)? {
Err(RVocError::PendingDatabaseMigrations)
} else {
Expand All @@ -41,15 +41,10 @@ pub fn has_missing_migrations(configuration: &Configuration) -> RVocResult<bool>
// Needs to be a sync connection, because `diesel_migrations` does not support async yet,
// and `diesel_async` does not support migrations yet.
debug!("Creating synchronous connection to database");
let mut conn = diesel::PgConnection::establish(
std::str::from_utf8(configuration.postgres_url.unsecure())
.expect("postgres_url should be utf8"),
)
.map_err(|error| RVocError::DatabaseMigration {
source: Box::new(error),
})?;
let mut connection = create_sync_connection(configuration)?;

conn.has_pending_migration(MIGRATIONS)
connection
.has_pending_migration(MIGRATIONS)
.map_err(|error| RVocError::DatabaseMigration { source: error })
}

Expand All @@ -64,15 +59,10 @@ pub fn run_migrations(configuration: &Configuration) -> RVocResult<()> {
// Needs to be a sync connection, because `diesel_migrations` does not support async yet,
// and `diesel_async` does not support migrations yet.
debug!("Creating synchronous connection to database");
let mut conn = diesel::PgConnection::establish(
std::str::from_utf8(configuration.postgres_url.unsecure())
.expect("postgres_url should be utf8"),
)
.map_err(|error| RVocError::DatabaseMigration {
source: Box::new(error),
})?;
let mut connection = create_sync_connection(configuration)?;
info!("Running pending database migrations (this may take a long time)...");
conn.run_pending_migrations(MIGRATIONS)
connection
.run_pending_migrations(MIGRATIONS)
.map_err(|error| RVocError::DatabaseMigration { source: error })?;
info!("Database migrations complete");
Ok(())
Expand All @@ -95,9 +85,11 @@ async fn create_async_connection_pool(
}

#[instrument(err, skip(configuration))]
fn create_sync_connection(configuration: &Configuration) -> RVocResult<impl Connection> {
fn create_sync_connection(configuration: &Configuration) -> RVocResult<PgConnection> {
use diesel::Connection;

// create a new connection with the default config
let connection = diesel::pg::PgConnection::establish(
let connection = PgConnection::establish(
std::str::from_utf8(configuration.postgres_url.unsecure())
.expect("postgres_url should be utf8"),
)
Expand Down
15 changes: 15 additions & 0 deletions backend/rvoc-backend/src/database/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use diesel::Insertable;

#[derive(Insertable)]
#[diesel(table_name = crate::schema::languages)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct InsertLanguage {
pub english_name: String,
}

#[derive(Insertable)]
#[diesel(table_name = crate::schema::word_types)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct InsertWordType {
pub english_name: String,
}
6 changes: 1 addition & 5 deletions backend/rvoc-backend/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,4 @@ diesel::table! {
diesel::joinable!(words -> languages (language));
diesel::joinable!(words -> word_types (word_type));

diesel::allow_tables_to_appear_in_same_query!(
languages,
word_types,
words,
);
diesel::allow_tables_to_appear_in_same_query!(languages, word_types, words,);
24 changes: 22 additions & 2 deletions backend/rvoc-backend/src/update_wiktionary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::path::PathBuf;

use diesel::{Connection, Insertable, RunQueryDsl};
use tokio::fs;
use wiktionary_dump_parser::parser::parse_dump_file;

use crate::database::create_sync_database_connection;
use crate::database::model::{InsertLanguage, InsertWordType};
use crate::error::RVocResult;
use crate::{configuration::Configuration, error::RVocError};
use tracing::{debug, error, instrument};
Expand All @@ -16,13 +18,31 @@ pub async fn run_update_wiktionary(configuration: &Configuration) -> RVocResult<
let new_dump_file = update_wiktionary_dump_files(configuration).await?;
// expect the extension to be ".tar.bz2", and replace it with ".log"
let error_log = new_dump_file.with_extension("").with_extension(".log");
let _database_connection = create_sync_database_connection(configuration)?;
let mut database_connection = create_sync_database_connection(configuration)?;

debug!("Parsing wiktionary dump file {new_dump_file:?}");
parse_dump_file(
new_dump_file,
Option::<PathBuf>::None,
|_| todo!(),
|word| {
database_connection.transaction(|connection| {
InsertLanguage {
english_name: word.language_english_name,
}
.insert_into(crate::schema::languages::table)
.on_conflict_do_nothing()
.execute(connection)?;

InsertWordType {
english_name: word.word_type,
}
.insert_into(crate::schema::word_types::table)
.on_conflict_do_nothing()
.execute(connection)?;

todo!()
})
},
error_log,
false,
)
Expand Down

0 comments on commit d5a8144

Please sign in to comment.