Skip to content

Commit

Permalink
Drop old creator tables (#368)
Browse files Browse the repository at this point in the history
* 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
IgnisDa authored Sep 27, 2023
1 parent c9d4b43 commit dd78453
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 5 deletions.
2 changes: 1 addition & 1 deletion 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 apps/backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ryot"
version = "2.19.0"
version = "2.19.1"
edition = "2021"
repository = "https://github.com/IgnisDa/ryot"
license = "GPL-V3"
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async fn main() -> Result<()> {
};
tracing::info!("Using database backend: {selected_database:?}");

Migrator::up(&db, None).await.unwrap();
Migrator::up(&db, None).await?;

match env::args().nth(1) {
None => {}
Expand Down
4 changes: 3 additions & 1 deletion apps/backend/src/migrator/m20230413_create_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::Metadata;
pub struct Migration;

pub static METADATA_TO_PERSON_PRIMARY_KEY: &str = "pk-media-item_person";
pub static PERSON_IDENTIFIER_UNIQUE_KEY: &str = "person-identifier-source__unique_index";

#[derive(Iden)]
pub enum Person {
Expand Down Expand Up @@ -81,9 +82,10 @@ impl MigrationTrait for Migration {
.create_index(
Index::create()
.unique()
.name("person-identifier-source__unique_index")
.name(PERSON_IDENTIFIER_UNIQUE_KEY)
.table(Person::Table)
.col(Person::Identifier)
.col(Person::Source)
.to_owned(),
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ impl MigrationTrait for Migration {
Table::alter()
.table(Review::Table)
.add_column(ColumnDef::new(Review::PersonId).integer().null())
.to_owned(),
)
.await?;
manager
.alter_table(
Table::alter()
.table(Review::Table)
.add_foreign_key(
TableForeignKey::new()
.name(PERSON_TO_REVIEW_FOREIGN_KEY)
Expand Down
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 apps/backend/src/migrator/m20230927_remove_useless_tables.rs
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(())
}
}
4 changes: 4 additions & 0 deletions apps/backend/src/migrator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ mod m20230919_add_num_times_updated_field_to_seen;
mod m20230919_change_foreign_keys;
mod m20230920_add_columns_to_metadata_table;
mod m20230927_add_person_id_field_to_review;
mod m20230927_change_faulty_index_person_table;
mod m20230927_remove_useless_tables;

pub use m20230410_create_metadata::{Metadata, MetadataLot, MetadataSource};
pub use m20230417_create_user::{UserLot, UserToMetadata};
Expand Down Expand Up @@ -78,6 +80,8 @@ impl MigratorTrait for Migrator {
Box::new(m20230919_change_foreign_keys::Migration),
Box::new(m20230920_add_columns_to_metadata_table::Migration),
Box::new(m20230927_add_person_id_field_to_review::Migration),
Box::new(m20230927_change_faulty_index_person_table::Migration),
Box::new(m20230927_remove_useless_tables::Migration),
]
}
}
4 changes: 3 additions & 1 deletion apps/backend/src/notification.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::env;

use anyhow::{anyhow, Result};
use convert_case::{Case, Casing};
use http_types::mime;
Expand All @@ -11,7 +13,7 @@ impl UserNotificationSetting {
// TODO: Allow formatting messages
pub async fn send_message(&self, msg: &str) -> Result<()> {
let project_name = PROJECT_NAME.to_case(Case::Title);
if std::env::var("DISABLE_NOTIFICATIONS").is_ok() {
if env::var("DISABLE_NOTIFICATIONS").is_ok() {
return Ok(());
}
match self {
Expand Down

0 comments on commit dd78453

Please sign in to comment.