20210619211347_dim.sql 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. -- Library table
  2. CREATE TABLE library (
  3. id INTEGER PRIMARY KEY NOT NULL,
  4. name TEXT NOT NULL UNIQUE,
  5. media_type TEXT NOT NULL
  6. );
  7. CREATE TABLE indexed_paths (
  8. id INTEGER PRIMARY KEY NOT NULL,
  9. -- must be absolute path
  10. location TEXT NOT NULL UNIQUE,
  11. library_id INTEGER NOT NULL,
  12. FOREIGN KEY (library_id) REFERENCES library(id) ON DELETE CASCADE
  13. );
  14. -- Media table
  15. -- This table contains the template for
  16. -- the movie and tv shows tables minus containing
  17. -- the paths because movies are streamable while
  18. -- tv shows generally arent
  19. -- The Episodes table will also inherit from here
  20. CREATE TABLE _tblmedia (
  21. id INTEGER NOT NULL,
  22. library_id INTEGER NOT NULL,
  23. name TEXT NOT NULL,
  24. description TEXT,
  25. rating REAL,
  26. year INTEGER,
  27. added TEXT,
  28. poster INTEGER,
  29. backdrop INTEGER,
  30. media_type TEXT NOT NULL,
  31. PRIMARY KEY (id),
  32. FOREIGN KEY (library_id) REFERENCES library(id) ON DELETE CASCADE,
  33. FOREIGN KEY (poster) REFERENCES assets(id),
  34. FOREIGN KEY (backdrop) REFERENCES assets(id)
  35. );
  36. -- Nicer view of media, ie we dont have to manually query some data.
  37. CREATE VIEW media AS
  38. SELECT _tblmedia.*, pp.local_path as poster_path, bp.local_path as backdrop_path
  39. FROM _tblmedia
  40. LEFT JOIN assets pp ON _tblmedia.poster = pp.id
  41. LEFT JOIN assets bp ON _tblmedia.backdrop = bp.id;
  42. CREATE TRIGGER media_delete
  43. INSTEAD OF DELETE ON media
  44. BEGIN
  45. DELETE FROM _tblmedia WHERE _tblmedia.id = old.id;
  46. END;
  47. CREATE UNIQUE INDEX media_idx ON _tblmedia(library_id, name, media_type) WHERE NOT _tblmedia.media_type = "episode";
  48. CREATE INDEX media_excl_ep_idx ON _tblmedia(name) WHERE NOT _tblmedia.media_type = "episode";
  49. CREATE TABLE _tblseason (
  50. id INTEGER,
  51. season_number INTEGER NOT NULL,
  52. tvshowid INTEGER NOT NULL,
  53. added TEXT,
  54. poster INTEGER,
  55. PRIMARY KEY (id),
  56. FOREIGN KEY(poster) REFERENCES assets(id),
  57. FOREIGN KEY(tvshowid) REFERENCES _tblmedia (id) ON DELETE CASCADE
  58. );
  59. CREATE UNIQUE INDEX season_idx ON _tblseason(season_number, tvshowid);
  60. -- Nicer view of _tblseason, ie we dont have to manually query some data.
  61. CREATE VIEW season AS
  62. SELECT _tblseason.id, _tblseason.season_number,
  63. _tblseason.tvshowid, _tblseason.added, assets.local_path as poster
  64. FROM _tblseason
  65. JOIN assets ON _tblseason.poster = assets.id;
  66. CREATE TRIGGER season_delete
  67. INSTEAD OF DELETE ON season
  68. BEGIN
  69. DELETE FROM _tblseason WHERE _tblseason.id = old.id;
  70. END;
  71. CREATE TABLE episode (
  72. id INTEGER,
  73. seasonid INTEGER NOT NULL,
  74. episode_ INTEGER NOT NULL,
  75. PRIMARY KEY (id),
  76. FOREIGN KEY(id) REFERENCES _tblmedia (id) ON DELETE CASCADE,
  77. FOREIGN KEY(seasonid) REFERENCES _tblseason (id) ON DELETE CASCADE
  78. );
  79. CREATE UNIQUE INDEX episode_idx ON episode(seasonid, episode_);
  80. CREATE TABLE mediafile (
  81. -- FIXME: Have to specify NOT NULL explictly otherwise sqlx thinks this field is nullable
  82. id INTEGER NOT NULL,
  83. media_id INTEGER, -- Optional, populated on metadata search
  84. library_id INTEGER NOT NULL,
  85. target_file TEXT NOT NULL UNIQUE,
  86. raw_name TEXT NOT NULL,
  87. raw_year INTEGER,
  88. quality TEXT(255),
  89. codec TEXT(255),
  90. container TEXT(255),
  91. audio TEXT(255),
  92. original_resolution TEXT(255),
  93. duration INTEGER,
  94. episode INTEGER,
  95. season INTEGER,
  96. corrupt BOOLEAN,
  97. PRIMARY KEY (id),
  98. FOREIGN KEY(media_id) REFERENCES _tblmedia (id) ON DELETE CASCADE ON UPDATE CASCADE,
  99. FOREIGN KEY(library_id) REFERENCES library(id) ON DELETE CASCADE
  100. );
  101. CREATE TABLE users (
  102. username TEXT PRIMARY KEY,
  103. password TEXT NOT NULL,
  104. prefs BLOB NOT NULL DEFAULT '{}',
  105. claimed_invite TEXT NOT NULL UNIQUE,
  106. roles TEXT[] NOT NULL DEFAULT 'User',
  107. picture INTEGER UNIQUE,
  108. FOREIGN KEY(claimed_invite) REFERENCES invites(id),
  109. FOREIGN KEY(picture) REFERENCES assets(id)
  110. );
  111. CREATE TABLE progress (
  112. id INTEGER NOT NULL,
  113. user_id TEXT NOT NULL,
  114. delta INTEGER NOT NULL,
  115. media_id INTEGER NOT NULL,
  116. populated INTEGER NOT NULL,
  117. PRIMARY KEY (id),
  118. FOREIGN KEY(media_id) REFERENCES _tblmedia (id) ON DELETE CASCADE ON UPDATE CASCADE,
  119. FOREIGN KEY(user_id) REFERENCES users(username) ON DELETE CASCADE
  120. );
  121. CREATE UNIQUE INDEX progress_idx ON progress(user_id, media_id);
  122. CREATE TABLE genre (
  123. id INTEGER PRIMARY KEY,
  124. name TEXT NOT NULL UNIQUE
  125. );
  126. CREATE TABLE genre_media (
  127. id INTEGER PRIMARY KEY,
  128. genre_id INTEGER NOT NULL,
  129. media_id INTEGER NOT NULL,
  130. FOREIGN KEY (media_id) REFERENCES _tblmedia(id) ON DELETE CASCADE,
  131. FOREIGN KEY (genre_id) REFERENCES genre(id) ON DELETE CASCADE
  132. );
  133. CREATE UNIQUE INDEX genre_media_idx ON genre_media(genre_id, media_id);
  134. CREATE TABLE invites (
  135. id TEXT PRIMARY KEY NOT NULL UNIQUE,
  136. date_added INTEGER NOT NULL
  137. );
  138. CREATE TABLE assets (
  139. id INTEGER PRIMARY KEY,
  140. remote_url TEXT UNIQUE,
  141. local_path TEXT NOT NULL UNIQUE,
  142. file_ext TEXT NOT NULL
  143. );
  144. CREATE TABLE media_posters (
  145. id INTEGER PRIMARY KEY,
  146. media_id INTEGER NOT NULL,
  147. asset_id INTEGER NOT NULL,
  148. FOREIGN KEY (media_id) REFERENCES _tblmedia(id) ON DELETE CASCADE,
  149. FOREIGN KEY (asset_id) REFERENCES assets(id) ON DELETE CASCADE
  150. );
  151. CREATE UNIQUE INDEX media_posters_idx ON media_posters(media_id, asset_id);
  152. CREATE TABLE media_backdrops (
  153. id INTEGER PRIMARY KEY,
  154. media_id INTEGER NOT NULL,
  155. asset_id INTEGER NOT NULL,
  156. FOREIGN KEY (media_id) REFERENCES _tblmedia(id) ON DELETE CASCADE,
  157. FOREIGN KEY (asset_id) REFERENCES assets(id) ON DELETE CASCADE
  158. );
  159. CREATE UNIQUE INDEX media_backdrops_idx ON media_backdrops(media_id, asset_id);