Migrate to the new entity format

This commit is contained in:
2025-11-02 11:04:19 -07:00
parent 508c4ed32f
commit c5475585d4
69 changed files with 2815 additions and 4038 deletions
+90 -32
View File
@@ -1,39 +1,88 @@
use seamantic::schema::sqlite_rowid_alias;
use sea_orm::sea_query;
use sea_orm::sea_query::{Index, Table};
use sea_orm::{DbErr, Iden};
use sea_orm_migration::SchemaManager;
use sea_orm_migration::schema::string;
#[derive(Iden)]
pub enum FlixInfoCollections {
Table,
Id,
Title,
Overview,
}
use sea_orm::prelude::*;
use sea_orm::sea_query::Table;
use sea_orm::{ConnectionTrait, DbBackend, Statement};
use sea_orm_migration::prelude::*;
pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(FlixInfoCollections::Table)
.col(sqlite_rowid_alias(FlixInfoCollections::Id))
.col(string(FlixInfoCollections::Title))
.col(string(FlixInfoCollections::Overview))
.to_owned(),
)
.drop_table(Table::drop().table("flix_watched_collections").to_owned())
.await?;
manager
.create_index(
Index::create()
.name("idx-flix_info_collections-title")
.table(FlixInfoCollections::Table)
.col(FlixInfoCollections::Title)
.to_owned(),
)
.get_connection()
.execute_raw(Statement::from_string(
DbBackend::Sqlite,
r#"
CREATE VIEW flix_watched_collections AS
WITH RECURSIVE
watched_items AS (
SELECT
w.id,
w.user_id,
w.watched_date,
'movie' AS type
FROM flix_watched_movies w
UNION ALL
SELECT
w.id,
w.user_id,
w.watched_date,
'show' AS type
FROM flix_watched_shows w
),
collection_items AS (
SELECT
m.parent_id,
m.id,
'movie' AS type
FROM flix_movies m
WHERE m.parent_id IS NOT NULL
UNION ALL
SELECT
s.parent_id,
s.id,
'show' AS type
FROM flix_shows s
WHERE s.parent_id IS NOT NULL
UNION ALL
SELECT
c.parent_id,
ci.id,
ci.type
FROM collection_items ci
JOIN flix_collections c
ON c.id = ci.parent_id
)
SELECT
ci.parent_id AS id,
wi.user_id,
MAX(wi.watched_date) AS watched_date
FROM collection_items ci
JOIN watched_items wi
ON wi.id = ci.id
AND wi.type = ci.type
WHERE NOT EXISTS (
SELECT 1
FROM collection_items ci2
WHERE ci2.parent_id = ci.parent_id
AND NOT EXISTS (
SELECT 1
FROM watched_items wi2
WHERE wi2.id = ci2.id
AND wi2.type = ci2.type
AND wi2.user_id = wi.user_id
)
)
GROUP BY ci.parent_id, wi.user_id
;
"#,
))
.await?;
Ok(())
@@ -41,6 +90,15 @@ pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
pub async fn down(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(FlixInfoCollections::Table).to_owned())
.await
.get_connection()
.execute_raw(Statement::from_string(
DbBackend::Sqlite,
r#"
DROP VIEW flix_watched_collections
;
"#,
))
.await?;
Ok(())
}
@@ -1,68 +0,0 @@
use sea_orm::sea_query;
use sea_orm::sea_query::{ForeignKeyCreateStatement, Index, Table};
use sea_orm::{DbErr, Iden};
use sea_orm_migration::SchemaManager;
use sea_orm_migration::schema::{date, integer, string};
use crate::migration::m_000001::FlixInfoShows;
use super::FlixInfoSeasons;
#[derive(Iden)]
pub enum FlixInfoEpisodes {
Table,
Show,
Season,
Episode,
Title,
Overview,
Date,
}
pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(FlixInfoEpisodes::Table)
.col(integer(FlixInfoEpisodes::Show))
.col(integer(FlixInfoEpisodes::Season))
.col(integer(FlixInfoEpisodes::Episode))
.col(string(FlixInfoEpisodes::Title))
.col(string(FlixInfoEpisodes::Overview))
.col(date(FlixInfoEpisodes::Date))
.primary_key(
Index::create()
.col(FlixInfoEpisodes::Show)
.col(FlixInfoEpisodes::Season)
.col(FlixInfoEpisodes::Episode),
)
.foreign_key(
ForeignKeyCreateStatement::new()
.name("fk-flix_info_episodes-show")
.from_tbl(FlixInfoEpisodes::Table)
.from_col(FlixInfoEpisodes::Show)
.to_tbl(FlixInfoShows::Table)
.to_col(FlixInfoShows::Id),
)
.foreign_key(
ForeignKeyCreateStatement::new()
.name("fk-flix_info_episodes-show_season")
.from_tbl(FlixInfoEpisodes::Table)
.from_col(FlixInfoEpisodes::Show)
.from_col(FlixInfoEpisodes::Season)
.to_tbl(FlixInfoSeasons::Table)
.to_col(FlixInfoSeasons::Show)
.to_col(FlixInfoSeasons::Season),
)
.to_owned(),
)
.await?;
Ok(())
}
pub async fn down(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(FlixInfoEpisodes::Table).to_owned())
.await
}
@@ -1,60 +0,0 @@
use seamantic::schema::sqlite_rowid_alias;
use sea_orm::sea_query;
use sea_orm::sea_query::{Index, Table};
use sea_orm::{DbErr, Iden};
use sea_orm_migration::SchemaManager;
use sea_orm_migration::schema::{date, string};
#[derive(Iden)]
pub enum FlixInfoMovies {
Table,
Id,
Title,
Tagline,
Overview,
Date,
}
pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(FlixInfoMovies::Table)
.col(sqlite_rowid_alias(FlixInfoMovies::Id))
.col(string(FlixInfoMovies::Title))
.col(string(FlixInfoMovies::Tagline))
.col(string(FlixInfoMovies::Overview))
.col(date(FlixInfoMovies::Date))
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx-flix_info_movies-title")
.table(FlixInfoMovies::Table)
.col(FlixInfoMovies::Title)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx-flix_info_movies-date")
.table(FlixInfoMovies::Table)
.col(FlixInfoMovies::Date)
.to_owned(),
)
.await?;
Ok(())
}
pub async fn down(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(FlixInfoMovies::Table).to_owned())
.await
}
+47 -41
View File
@@ -1,46 +1,43 @@
use sea_orm::sea_query;
use sea_orm::sea_query::{ForeignKeyCreateStatement, Index, Table};
use sea_orm::{DbErr, Iden};
use sea_orm_migration::SchemaManager;
use sea_orm_migration::schema::{date, integer, string};
use super::FlixInfoShows;
#[derive(Iden)]
pub enum FlixInfoSeasons {
Table,
Show,
Season,
Title,
Overview,
Date,
}
use sea_orm::prelude::*;
use sea_orm::sea_query::Table;
use sea_orm::{ConnectionTrait, DbBackend, Statement};
use sea_orm_migration::prelude::*;
pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(FlixInfoSeasons::Table)
.col(integer(FlixInfoSeasons::Show))
.col(integer(FlixInfoSeasons::Season))
.col(string(FlixInfoSeasons::Title))
.col(string(FlixInfoSeasons::Overview))
.col(date(FlixInfoSeasons::Date))
.primary_key(
Index::create()
.col(FlixInfoSeasons::Show)
.col(FlixInfoSeasons::Season),
.drop_table(Table::drop().table("flix_watched_seasons").to_owned())
.await?;
manager
.get_connection()
.execute_raw(Statement::from_string(
DbBackend::Sqlite,
r#"
CREATE VIEW flix_watched_seasons AS
SELECT
w.show_id,
w.season_number,
w.user_id,
MAX(w.watched_date) AS watched_date
FROM flix_watched_episodes w
WHERE NOT EXISTS (
SELECT 1
FROM flix_episodes e
WHERE e.show_id = w.show_id
AND e.season_number = w.season_number
AND NOT EXISTS (
SELECT 1
FROM flix_watched_episodes wc
WHERE wc.show_id = e.show_id
AND wc.season_number = e.season_number
AND wc.episode_number = e.episode_number
AND wc.user_id = w.user_id
)
)
.foreign_key(
ForeignKeyCreateStatement::new()
.name("fk-flix_info_seasons-show")
.from_tbl(FlixInfoSeasons::Table)
.from_col(FlixInfoSeasons::Show)
.to_tbl(FlixInfoShows::Table)
.to_col(FlixInfoShows::Id),
)
.to_owned(),
)
GROUP BY w.show_id, w.season_number, w.user_id
;
"#,
))
.await?;
Ok(())
@@ -48,6 +45,15 @@ pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
pub async fn down(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(FlixInfoSeasons::Table).to_owned())
.await
.get_connection()
.execute_raw(Statement::from_string(
DbBackend::Sqlite,
r#"
DROP VIEW flix_watched_seasons
;
"#,
))
.await?;
Ok(())
}
+42 -46
View File
@@ -1,53 +1,40 @@
use seamantic::schema::sqlite_rowid_alias;
use sea_orm::sea_query;
use sea_orm::sea_query::{Index, Table};
use sea_orm::{DbErr, Iden};
use sea_orm_migration::SchemaManager;
use sea_orm_migration::schema::{date, string};
#[derive(Iden)]
pub enum FlixInfoShows {
Table,
Id,
Title,
Tagline,
Overview,
Date,
}
use sea_orm::prelude::*;
use sea_orm::sea_query::Table;
use sea_orm::{ConnectionTrait, DbBackend, Statement};
use sea_orm_migration::prelude::*;
pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(FlixInfoShows::Table)
.col(sqlite_rowid_alias(FlixInfoShows::Id))
.col(string(FlixInfoShows::Title))
.col(string(FlixInfoShows::Tagline))
.col(string(FlixInfoShows::Overview))
.col(date(FlixInfoShows::Date))
.to_owned(),
)
.drop_table(Table::drop().table("flix_watched_shows").to_owned())
.await?;
manager
.create_index(
Index::create()
.name("idx-flix_info_shows-title")
.table(FlixInfoShows::Table)
.col(FlixInfoShows::Title)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx-flix_info_shows-date")
.table(FlixInfoShows::Table)
.col(FlixInfoShows::Date)
.to_owned(),
)
.get_connection()
.execute_raw(Statement::from_string(
DbBackend::Sqlite,
r#"
CREATE VIEW flix_watched_shows AS
SELECT
w.show_id as id,
w.user_id,
MAX(w.watched_date) AS watched_date
FROM flix_watched_seasons w
WHERE NOT EXISTS (
SELECT 1
FROM flix_seasons s
WHERE s.show_id = w.show_id
AND NOT EXISTS (
SELECT 1
FROM flix_watched_seasons wc
WHERE wc.show_id = s.show_id
AND wc.season_number = s.season_number
AND wc.user_id = w.user_id
)
)
GROUP BY w.show_id, w.user_id
;
"#,
))
.await?;
Ok(())
@@ -55,6 +42,15 @@ pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
pub async fn down(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(FlixInfoShows::Table).to_owned())
.await
.get_connection()
.execute_raw(Statement::from_string(
DbBackend::Sqlite,
r#"
DROP VIEW flix_watched_shows
;
"#,
))
.await?;
Ok(())
}