-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(backend): create correct index * fix(backend): change import * fix(backend): bubble up migration errors * feat(backend): migration for deleting columns * fix(backend): make message more obvious * refactor(backend): change name of migration * fix(backend): make migration msg better * fix(backend): change envar for migration check * fix(backend): fixes #370 * build(backend): bump version
- Loading branch information
Showing
9 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
31 changes: 31 additions & 0 deletions
31
apps/backend/src/migrator/m20230927_change_faulty_index_person_table.rs
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,31 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
use super::m20230413_create_person::{Person, PERSON_IDENTIFIER_UNIQUE_KEY}; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.drop_index(Index::drop().name(PERSON_IDENTIFIER_UNIQUE_KEY).to_owned()) | ||
.await?; | ||
manager | ||
.create_index( | ||
Index::create() | ||
.unique() | ||
.name(PERSON_IDENTIFIER_UNIQUE_KEY) | ||
.table(Person::Table) | ||
.col(Person::Identifier) | ||
.col(Person::Source) | ||
.to_owned(), | ||
) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> { | ||
Ok(()) | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
apps/backend/src/migrator/m20230927_remove_useless_tables.rs
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,88 @@ | ||
use std::env; | ||
|
||
use sea_orm::{DatabaseBackend, Statement}; | ||
use sea_orm_migration::prelude::*; | ||
|
||
use super::m20230412_create_creator::{Creator, MetadataToCreator}; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[derive(Iden)] | ||
pub enum Review { | ||
Table, | ||
CreatorId, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
if manager.has_column("review", "creator_id").await? { | ||
let db = manager.get_connection(); | ||
let stmt = Query::select() | ||
.expr(Func::count(Expr::col(Asterisk))) | ||
.from(Review::Table) | ||
.cond_where(Expr::col(Review::CreatorId).is_not_null()) | ||
.to_owned(); | ||
let (sql, values) = match manager.get_database_backend() { | ||
DatabaseBackend::MySql => stmt.build(MySqlQueryBuilder {}), | ||
DatabaseBackend::Postgres => stmt.build(PostgresQueryBuilder {}), | ||
DatabaseBackend::Sqlite => stmt.build(SqliteQueryBuilder {}), | ||
}; | ||
let stmt = Statement::from_sql_and_values(manager.get_database_backend(), sql, values); | ||
let resp = db.query_one(stmt).await?.unwrap(); | ||
let count = resp.try_get_by_index::<i64>(0)?; | ||
if count > 0 { | ||
let var_name = "MIGRATIONS_NO_CREATOR_CHECK"; | ||
let message = format!(" | ||
This migration will delete all old creators (changes introduced in `v2.19.0`) and associated reviews. | ||
You have reviews for {count} creator(s). | ||
Please downgrade to the `v2.19.0`, follow instructions at https://github.com/IgnisDa/ryot/releases/tag/v2.19.0 to migrate this data, and then upgrade again. | ||
If you want to skip this check, please set the environment variable `{var_name}=1`."); | ||
tracing::info!(message); | ||
if env::var(var_name).is_err() { | ||
return Err(DbErr::Custom("Unable to continue".to_owned())); | ||
} else { | ||
tracing::warn!("Deleting {} review(s) in 10 seconds.", count); | ||
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await; | ||
let stmt = Query::delete() | ||
.from_table(Review::Table) | ||
.cond_where(Expr::col(Review::CreatorId).is_not_null()) | ||
.to_owned(); | ||
let (sql, values) = match manager.get_database_backend() { | ||
DatabaseBackend::MySql => stmt.build(MySqlQueryBuilder {}), | ||
DatabaseBackend::Postgres => stmt.build(PostgresQueryBuilder {}), | ||
DatabaseBackend::Sqlite => stmt.build(SqliteQueryBuilder {}), | ||
}; | ||
let stmt = | ||
Statement::from_sql_and_values(manager.get_database_backend(), sql, values); | ||
db.execute(stmt).await?; | ||
} | ||
} | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Review::Table) | ||
.drop_column(Review::CreatorId) | ||
.to_owned(), | ||
) | ||
.await?; | ||
} | ||
if manager.has_table("metadata_to_creator").await? { | ||
manager | ||
.drop_table(Table::drop().table(MetadataToCreator::Table).to_owned()) | ||
.await?; | ||
} | ||
if manager.has_table("creator").await? { | ||
manager | ||
.drop_table(Table::drop().table(Creator::Table).to_owned()) | ||
.await?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> { | ||
Ok(()) | ||
} | ||
} |
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