forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [torrust#56] transfer categories from db v1.0.0 to v2.0.0
First action for the command to upgrade data. It transfers the categories from the current DB schema (v1.0.0) to the new DB schema.
- Loading branch information
1 parent
7513df0
commit b92fb08
Showing
6 changed files
with
200 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod database; | ||
pub mod mysql; | ||
pub mod sqlite; | ||
pub mod sqlite_v1_0_0; | ||
pub mod sqlite_v2_0_0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use super::database::DatabaseError; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::sqlite::SqlitePoolOptions; | ||
use sqlx::{query_as, SqlitePool}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)] | ||
pub struct Category { | ||
pub category_id: i64, | ||
pub name: String, | ||
} | ||
pub struct SqliteDatabaseV1_0_0 { | ||
pub pool: SqlitePool, | ||
} | ||
|
||
impl SqliteDatabaseV1_0_0 { | ||
pub async fn new(database_url: &str) -> Self { | ||
let db = SqlitePoolOptions::new() | ||
.connect(database_url) | ||
.await | ||
.expect("Unable to create database pool."); | ||
Self { pool: db } | ||
} | ||
|
||
pub async fn get_categories_order_by_id(&self) -> Result<Vec<Category>, DatabaseError> { | ||
query_as::<_, Category>("SELECT category_id, name FROM torrust_categories ORDER BY category_id ASC") | ||
.fetch_all(&self.pool) | ||
.await | ||
.map_err(|_| DatabaseError::Error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use super::database::DatabaseError; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::sqlite::{SqlitePoolOptions, SqliteQueryResult}; | ||
use sqlx::{query, query_as, SqlitePool}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)] | ||
pub struct Category { | ||
pub category_id: i64, | ||
pub name: String, | ||
} | ||
pub struct SqliteDatabaseV2_0_0 { | ||
pub pool: SqlitePool, | ||
} | ||
|
||
impl SqliteDatabaseV2_0_0 { | ||
pub async fn new(database_url: &str) -> Self { | ||
let db = SqlitePoolOptions::new() | ||
.connect(database_url) | ||
.await | ||
.expect("Unable to create database pool."); | ||
Self { pool: db } | ||
} | ||
|
||
pub async fn reset_categories_sequence(&self) -> Result<SqliteQueryResult, DatabaseError> { | ||
query("DELETE FROM `sqlite_sequence` WHERE `name` = 'torrust_categories'") | ||
.execute(&self.pool) | ||
.await | ||
.map_err(|_| DatabaseError::Error) | ||
} | ||
|
||
pub async fn get_categories(&self) -> Result<Vec<Category>, DatabaseError> { | ||
query_as::<_, Category>("SELECT tc.category_id, tc.name, COUNT(tt.category_id) as num_torrents FROM torrust_categories tc LEFT JOIN torrust_torrents tt on tc.category_id = tt.category_id GROUP BY tc.name") | ||
.fetch_all(&self.pool) | ||
.await | ||
.map_err(|_| DatabaseError::Error) | ||
} | ||
|
||
pub async fn insert_category_and_get_id(&self, category_name: &str) -> Result<i64, DatabaseError> { | ||
query("INSERT INTO torrust_categories (name) VALUES (?)") | ||
.bind(category_name) | ||
.execute(&self.pool) | ||
.await | ||
.map(|v| v.last_insert_rowid()) | ||
.map_err(|e| match e { | ||
sqlx::Error::Database(err) => { | ||
if err.message().contains("UNIQUE") { | ||
DatabaseError::CategoryAlreadyExists | ||
} else { | ||
DatabaseError::Error | ||
} | ||
} | ||
_ => DatabaseError::Error, | ||
}) | ||
} | ||
|
||
pub async fn delete_all_database_rows(&self) -> Result<(), DatabaseError> { | ||
query("DELETE FROM torrust_categories;") | ||
.execute(&self.pool) | ||
.await | ||
.unwrap(); | ||
|
||
query("DELETE FROM torrust_torrents;") | ||
.execute(&self.pool) | ||
.await | ||
.unwrap(); | ||
|
||
query("DELETE FROM torrust_tracker_keys;") | ||
.execute(&self.pool) | ||
.await | ||
.unwrap(); | ||
|
||
query("DELETE FROM torrust_users;") | ||
.execute(&self.pool) | ||
.await | ||
.unwrap(); | ||
|
||
query("DELETE FROM torrust_user_authentication;") | ||
.execute(&self.pool) | ||
.await | ||
.unwrap(); | ||
|
||
query("DELETE FROM torrust_user_bans;") | ||
.execute(&self.pool) | ||
.await | ||
.unwrap(); | ||
|
||
query("DELETE FROM torrust_user_invitations;") | ||
.execute(&self.pool) | ||
.await | ||
.unwrap(); | ||
|
||
query("DELETE FROM torrust_user_profiles;") | ||
.execute(&self.pool) | ||
.await | ||
.unwrap(); | ||
|
||
query("DELETE FROM torrust_torrents;") | ||
.execute(&self.pool) | ||
.await | ||
.unwrap(); | ||
|
||
query("DELETE FROM torrust_user_public_keys;") | ||
.execute(&self.pool) | ||
.await | ||
.unwrap(); | ||
|
||
Ok(()) | ||
} | ||
} |