You've already forked flix
Throw away flix files in favor of a flix database
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
use sea_orm::DbErr;
|
||||
use sea_orm::{ConnectionTrait, DbBackend, Statement};
|
||||
use sea_orm_migration::SchemaManager;
|
||||
|
||||
pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
manager
|
||||
.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,
|
||||
m.id,
|
||||
'movie' AS type
|
||||
FROM flix_movies m
|
||||
WHERE m.parent IS NOT NULL
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
s.parent,
|
||||
s.id,
|
||||
'show' AS type
|
||||
FROM flix_shows s
|
||||
WHERE s.parent IS NOT NULL
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
c.parent,
|
||||
ci.id,
|
||||
ci.type
|
||||
FROM collection_items ci
|
||||
JOIN flix_collections c
|
||||
ON c.id = ci.parent
|
||||
)
|
||||
SELECT
|
||||
ci.parent 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 = ci.parent
|
||||
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, wi.user_id;
|
||||
"#,
|
||||
))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn down(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
manager
|
||||
.get_connection()
|
||||
.execute_raw(Statement::from_string(
|
||||
DbBackend::Sqlite,
|
||||
r#"
|
||||
DROP VIEW flix_watched_collections
|
||||
;
|
||||
"#,
|
||||
))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
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};
|
||||
|
||||
use crate::migration::m_000001::FlixInfoEpisodes;
|
||||
|
||||
#[derive(Iden)]
|
||||
pub enum FlixWatchedEpisodes {
|
||||
Table,
|
||||
Show,
|
||||
Season,
|
||||
Episode,
|
||||
UserId,
|
||||
WatchedDate,
|
||||
}
|
||||
|
||||
pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(FlixWatchedEpisodes::Table)
|
||||
.col(integer(FlixWatchedEpisodes::Show))
|
||||
.col(integer(FlixWatchedEpisodes::Season))
|
||||
.col(integer(FlixWatchedEpisodes::Episode))
|
||||
.col(integer(FlixWatchedEpisodes::UserId))
|
||||
.col(date(FlixWatchedEpisodes::WatchedDate))
|
||||
.primary_key(
|
||||
Index::create()
|
||||
.col(FlixWatchedEpisodes::Show)
|
||||
.col(FlixWatchedEpisodes::Season)
|
||||
.col(FlixWatchedEpisodes::Episode)
|
||||
.col(FlixWatchedEpisodes::UserId),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKeyCreateStatement::new()
|
||||
.name("fk-flix_watched_episodes-show_season_episode")
|
||||
.from_tbl(FlixWatchedEpisodes::Table)
|
||||
.from_tbl(FlixWatchedEpisodes::Table)
|
||||
.from_col(FlixWatchedEpisodes::Show)
|
||||
.from_col(FlixWatchedEpisodes::Season)
|
||||
.from_col(FlixWatchedEpisodes::Episode)
|
||||
.to_tbl(FlixInfoEpisodes::Table)
|
||||
.to_col(FlixInfoEpisodes::Show)
|
||||
.to_col(FlixInfoEpisodes::Season)
|
||||
.to_col(FlixInfoEpisodes::Episode),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn down(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(FlixWatchedEpisodes::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
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};
|
||||
|
||||
use crate::migration::m_000001::FlixInfoMovies;
|
||||
|
||||
#[derive(Iden)]
|
||||
pub enum FlixWatchedMovies {
|
||||
Table,
|
||||
Id,
|
||||
UserId,
|
||||
WatchedDate,
|
||||
}
|
||||
|
||||
pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(FlixWatchedMovies::Table)
|
||||
.col(integer(FlixWatchedMovies::Id))
|
||||
.col(integer(FlixWatchedMovies::UserId))
|
||||
.col(date(FlixWatchedMovies::WatchedDate))
|
||||
.primary_key(
|
||||
Index::create()
|
||||
.col(FlixWatchedMovies::Id)
|
||||
.col(FlixWatchedMovies::UserId),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKeyCreateStatement::new()
|
||||
.name("fk-flix_watched_movies-id")
|
||||
.from_tbl(FlixWatchedMovies::Table)
|
||||
.from_col(FlixWatchedMovies::Id)
|
||||
.to_tbl(FlixInfoMovies::Table)
|
||||
.to_col(FlixInfoMovies::Id),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn down(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(FlixWatchedMovies::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
use sea_orm::DbErr;
|
||||
use sea_orm::{ConnectionTrait, DbBackend, Statement};
|
||||
use sea_orm_migration::SchemaManager;
|
||||
|
||||
pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
manager
|
||||
.get_connection()
|
||||
.execute_raw(Statement::from_string(
|
||||
DbBackend::Sqlite,
|
||||
r#"
|
||||
CREATE VIEW flix_watched_seasons AS
|
||||
SELECT
|
||||
w.show,
|
||||
w.season,
|
||||
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 = w.show
|
||||
AND e.season = w.season
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM flix_watched_episodes wc
|
||||
WHERE wc.show = e.show
|
||||
AND wc.season = e.season
|
||||
AND wc.episode = e.episode
|
||||
AND wc.user_id = w.user_id
|
||||
)
|
||||
)
|
||||
GROUP BY w.show, w.season, w.user_id
|
||||
;
|
||||
"#,
|
||||
))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn down(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
manager
|
||||
.get_connection()
|
||||
.execute_raw(Statement::from_string(
|
||||
DbBackend::Sqlite,
|
||||
r#"
|
||||
DROP VIEW flix_watched_seasons
|
||||
;
|
||||
"#,
|
||||
))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
use sea_orm::DbErr;
|
||||
use sea_orm::{ConnectionTrait, DbBackend, Statement};
|
||||
use sea_orm_migration::SchemaManager;
|
||||
|
||||
pub async fn up(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
manager
|
||||
.get_connection()
|
||||
.execute_raw(Statement::from_string(
|
||||
DbBackend::Sqlite,
|
||||
r#"
|
||||
CREATE VIEW flix_watched_shows AS
|
||||
SELECT
|
||||
w.show 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 = w.show
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM flix_watched_seasons wc
|
||||
WHERE wc.show = s.show
|
||||
AND wc.season = s.season
|
||||
AND wc.user_id = w.user_id
|
||||
)
|
||||
)
|
||||
GROUP BY w.show, w.user_id
|
||||
;
|
||||
"#,
|
||||
))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn down(manager: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
manager
|
||||
.get_connection()
|
||||
.execute_raw(Statement::from_string(
|
||||
DbBackend::Sqlite,
|
||||
r#"
|
||||
DROP VIEW flix_watched_shows
|
||||
;
|
||||
"#,
|
||||
))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user