Add script to update database paths
Add script for creating database functions
This commit is contained in:
parent
39d5a05520
commit
0d9615f52c
34
scripts/db_paths_to_relative.py
Normal file
34
scripts/db_paths_to_relative.py
Normal file
@ -0,0 +1,34 @@
|
||||
from pathlib import Path
|
||||
from database import Session, Comic, Movie, TvShow, Game, TvEpisode, DupComic
|
||||
|
||||
OLD_COMICS_DIRECTORY = Path("/usb/storage/media/Comics/")
|
||||
OLD_MOVIES_DIRECTORY = Path("/usb/storage/media/Videos/Movies/")
|
||||
OLD_TV_SHOWS_DIRECTORY = Path("/usb/storage/media/Videos/TV/")
|
||||
OLD_GAMES_DIRECTORY = Path("/usb/storage/Games/")
|
||||
|
||||
NEW_COMICS_DIRECTORY = Path("/srv/comics/")
|
||||
NEW_MOVIES_DIRECTORY = Path("/srv/movies/")
|
||||
NEW_TV_SHOWS_DIRECTORY = Path("/srv/tv/")
|
||||
NEW_GAMES_DIRECTORY = Path("/srv/games/")
|
||||
|
||||
|
||||
def update_db_paths(table, old_root, new_root):
|
||||
session = Session()
|
||||
|
||||
rows = session.query(table).all()
|
||||
for row in rows:
|
||||
absolute = Path(row.path)
|
||||
relative = absolute.relative_to(old_root)
|
||||
print(absolute)
|
||||
print(new_root / relative)
|
||||
print()
|
||||
row.path = str(new_root / relative)
|
||||
session.commit()
|
||||
|
||||
|
||||
update_db_paths(Comic, OLD_COMICS_DIRECTORY, NEW_COMICS_DIRECTORY)
|
||||
update_db_paths(DupComic, OLD_COMICS_DIRECTORY, NEW_COMICS_DIRECTORY)
|
||||
update_db_paths(Movie, OLD_MOVIES_DIRECTORY, NEW_MOVIES_DIRECTORY)
|
||||
update_db_paths(TvShow, OLD_TV_SHOWS_DIRECTORY, NEW_TV_SHOWS_DIRECTORY)
|
||||
update_db_paths(TvEpisode, OLD_TV_SHOWS_DIRECTORY, NEW_TV_SHOWS_DIRECTORY)
|
||||
update_db_paths(Game, OLD_GAMES_DIRECTORY, NEW_GAMES_DIRECTORY)
|
33
scripts/funcs.sql
Normal file
33
scripts/funcs.sql
Normal file
@ -0,0 +1,33 @@
|
||||
create function is_movie_finished(movie_tmdb_id integer, user_email text) returns boolean
|
||||
language plpgsql
|
||||
as
|
||||
$$
|
||||
DECLARE
|
||||
is_finished BOOLEAN:= false;
|
||||
user_data text;
|
||||
BEGIN
|
||||
SELECT tmdb_id INTO user_data FROM rpiwebapp.user_tv_movie_data WHERE tmdb_id=movie_tmdb_id AND "user"=user_email AND finished=true;
|
||||
IF user_data IS NOT NULL THEN
|
||||
is_finished:= true;
|
||||
END IF;
|
||||
RETURN is_finished;
|
||||
END
|
||||
$$;
|
||||
|
||||
create function is_tv_show_finished(show_id integer, user_email text) returns boolean
|
||||
language plpgsql
|
||||
as
|
||||
$$
|
||||
DECLARE
|
||||
len_user_data INTEGER;
|
||||
len_tv_episodes INTEGER;
|
||||
is_finished BOOLEAN:= false;
|
||||
BEGIN
|
||||
SELECT count(*) INTO len_user_data FROM rpiwebapp.user_tv_movie_data WHERE parent_tmdb=show_id AND "user"=user_email AND finished=true;
|
||||
SELECT count(*) INTO len_tv_episodes FROM rpiwebapp.tv_episodes WHERE parent_tmdb_id=show_id;
|
||||
IF len_user_data = len_tv_episodes THEN
|
||||
is_finished := true;
|
||||
END IF;
|
||||
RETURN is_finished;
|
||||
END
|
||||
$$;
|
Loading…
Reference in New Issue
Block a user