Skip to content

Commit d546f18

Browse files
authored
Merge pull request #2845 from SamantazFox/more-db-improvements
More db improvements
2 parents be92bfd + 67dd2b4 commit d546f18

File tree

8 files changed

+87
-79
lines changed

8 files changed

+87
-79
lines changed

src/invidious.cr

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,7 @@ OUTPUT = CONFIG.output.upcase == "STDOUT" ? STDOUT : File.open(CONFIG.output, mo
112112
LOGGER = Invidious::LogHandler.new(OUTPUT, CONFIG.log_level)
113113

114114
# Check table integrity
115-
if CONFIG.check_tables
116-
Invidious::Database.check_enum(PG_DB, "privacy", PlaylistPrivacy)
117-
118-
Invidious::Database.check_table(PG_DB, "channels", InvidiousChannel)
119-
Invidious::Database.check_table(PG_DB, "channel_videos", ChannelVideo)
120-
Invidious::Database.check_table(PG_DB, "playlists", InvidiousPlaylist)
121-
Invidious::Database.check_table(PG_DB, "playlist_videos", PlaylistVideo)
122-
Invidious::Database.check_table(PG_DB, "nonces", Nonce)
123-
Invidious::Database.check_table(PG_DB, "session_ids", SessionId)
124-
Invidious::Database.check_table(PG_DB, "users", User)
125-
Invidious::Database.check_table(PG_DB, "videos", Video)
126-
127-
if CONFIG.cache_annotations
128-
Invidious::Database.check_table(PG_DB, "annotations", Annotation)
129-
end
130-
end
115+
Invidious::Database.check_integrity(CONFIG)
131116

132117
# Resolve player dependencies. This is done at compile time.
133118
#

src/invidious/database/base.cr

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,60 @@ require "pg"
33
module Invidious::Database
44
extend self
55

6-
def check_enum(db, enum_name, struct_type = nil)
6+
# Checks table integrity
7+
#
8+
# Note: config is passed as a parameter to avoid complex
9+
# dependencies between different parts of the software.
10+
def check_integrity(cfg)
11+
return if !cfg.check_tables
12+
Invidious::Database.check_enum("privacy", PlaylistPrivacy)
13+
14+
Invidious::Database.check_table("channels", InvidiousChannel)
15+
Invidious::Database.check_table("channel_videos", ChannelVideo)
16+
Invidious::Database.check_table("playlists", InvidiousPlaylist)
17+
Invidious::Database.check_table("playlist_videos", PlaylistVideo)
18+
Invidious::Database.check_table("nonces", Nonce)
19+
Invidious::Database.check_table("session_ids", SessionId)
20+
Invidious::Database.check_table("users", User)
21+
Invidious::Database.check_table("videos", Video)
22+
23+
if cfg.cache_annotations
24+
Invidious::Database.check_table("annotations", Annotation)
25+
end
26+
end
27+
28+
#
29+
# Table/enum integrity checks
30+
#
31+
32+
def check_enum(enum_name, struct_type = nil)
733
return # TODO
834

9-
if !db.query_one?("SELECT true FROM pg_type WHERE typname = $1", enum_name, as: Bool)
35+
if !PG_DB.query_one?("SELECT true FROM pg_type WHERE typname = $1", enum_name, as: Bool)
1036
LOGGER.info("check_enum: CREATE TYPE #{enum_name}")
1137

12-
db.using_connection do |conn|
38+
PG_DB.using_connection do |conn|
1339
conn.as(PG::Connection).exec_all(File.read("config/sql/#{enum_name}.sql"))
1440
end
1541
end
1642
end
1743

18-
def check_table(db, table_name, struct_type = nil)
44+
def check_table(table_name, struct_type = nil)
1945
# Create table if it doesn't exist
2046
begin
21-
db.exec("SELECT * FROM #{table_name} LIMIT 0")
47+
PG_DB.exec("SELECT * FROM #{table_name} LIMIT 0")
2248
rescue ex
2349
LOGGER.info("check_table: check_table: CREATE TABLE #{table_name}")
2450

25-
db.using_connection do |conn|
51+
PG_DB.using_connection do |conn|
2652
conn.as(PG::Connection).exec_all(File.read("config/sql/#{table_name}.sql"))
2753
end
2854
end
2955

3056
return if !struct_type
3157

3258
struct_array = struct_type.type_array
33-
column_array = get_column_array(db, table_name)
59+
column_array = get_column_array(PG_DB, table_name)
3460
column_types = File.read("config/sql/#{table_name}.sql").match(/CREATE TABLE public\.#{table_name}\n\((?<types>[\d\D]*?)\);/)
3561
.try &.["types"].split(",").map(&.strip).reject &.starts_with?("CONSTRAINT")
3662

@@ -41,14 +67,14 @@ module Invidious::Database
4167
if !column_array[i]?
4268
new_column = column_types.select(&.starts_with?(name))[0]
4369
LOGGER.info("check_table: ALTER TABLE #{table_name} ADD COLUMN #{new_column}")
44-
db.exec("ALTER TABLE #{table_name} ADD COLUMN #{new_column}")
70+
PG_DB.exec("ALTER TABLE #{table_name} ADD COLUMN #{new_column}")
4571
next
4672
end
4773

4874
# Column doesn't exist
4975
if !column_array.includes? name
5076
new_column = column_types.select(&.starts_with?(name))[0]
51-
db.exec("ALTER TABLE #{table_name} ADD COLUMN #{new_column}")
77+
PG_DB.exec("ALTER TABLE #{table_name} ADD COLUMN #{new_column}")
5278
end
5379

5480
# Column exists but in the wrong position, rotate
@@ -59,29 +85,29 @@ module Invidious::Database
5985
# There's a column we didn't expect
6086
if !new_column
6187
LOGGER.info("check_table: ALTER TABLE #{table_name} DROP COLUMN #{column_array[i]}")
62-
db.exec("ALTER TABLE #{table_name} DROP COLUMN #{column_array[i]} CASCADE")
88+
PG_DB.exec("ALTER TABLE #{table_name} DROP COLUMN #{column_array[i]} CASCADE")
6389

64-
column_array = get_column_array(db, table_name)
90+
column_array = get_column_array(PG_DB, table_name)
6591
next
6692
end
6793

6894
LOGGER.info("check_table: ALTER TABLE #{table_name} ADD COLUMN #{new_column}")
69-
db.exec("ALTER TABLE #{table_name} ADD COLUMN #{new_column}")
95+
PG_DB.exec("ALTER TABLE #{table_name} ADD COLUMN #{new_column}")
7096

7197
LOGGER.info("check_table: UPDATE #{table_name} SET #{column_array[i]}_new=#{column_array[i]}")
72-
db.exec("UPDATE #{table_name} SET #{column_array[i]}_new=#{column_array[i]}")
98+
PG_DB.exec("UPDATE #{table_name} SET #{column_array[i]}_new=#{column_array[i]}")
7399

74100
LOGGER.info("check_table: ALTER TABLE #{table_name} DROP COLUMN #{column_array[i]} CASCADE")
75-
db.exec("ALTER TABLE #{table_name} DROP COLUMN #{column_array[i]} CASCADE")
101+
PG_DB.exec("ALTER TABLE #{table_name} DROP COLUMN #{column_array[i]} CASCADE")
76102

77103
LOGGER.info("check_table: ALTER TABLE #{table_name} RENAME COLUMN #{column_array[i]}_new TO #{column_array[i]}")
78-
db.exec("ALTER TABLE #{table_name} RENAME COLUMN #{column_array[i]}_new TO #{column_array[i]}")
104+
PG_DB.exec("ALTER TABLE #{table_name} RENAME COLUMN #{column_array[i]}_new TO #{column_array[i]}")
79105

80-
column_array = get_column_array(db, table_name)
106+
column_array = get_column_array(PG_DB, table_name)
81107
end
82108
else
83109
LOGGER.info("check_table: ALTER TABLE #{table_name} DROP COLUMN #{column_array[i]} CASCADE")
84-
db.exec("ALTER TABLE #{table_name} DROP COLUMN #{column_array[i]} CASCADE")
110+
PG_DB.exec("ALTER TABLE #{table_name} DROP COLUMN #{column_array[i]} CASCADE")
85111
end
86112
end
87113
end
@@ -91,14 +117,14 @@ module Invidious::Database
91117
column_array.each do |column|
92118
if !struct_array.includes? column
93119
LOGGER.info("check_table: ALTER TABLE #{table_name} DROP COLUMN #{column} CASCADE")
94-
db.exec("ALTER TABLE #{table_name} DROP COLUMN #{column} CASCADE")
120+
PG_DB.exec("ALTER TABLE #{table_name} DROP COLUMN #{column} CASCADE")
95121
end
96122
end
97123
end
98124

99125
def get_column_array(db, table_name)
100126
column_array = [] of String
101-
db.query("SELECT * FROM #{table_name} LIMIT 0") do |rs|
127+
PG_DB.query("SELECT * FROM #{table_name} LIMIT 0") do |rs|
102128
rs.column_count.times do |i|
103129
column = rs.as(PG::ResultSet).field(i)
104130
column_array << column.name

src/invidious/database/channels.cr

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,31 @@ module Invidious::Database::Channels
3535
def update_author(id : String, author : String)
3636
request = <<-SQL
3737
UPDATE channels
38-
SET updated = $1, author = $2, deleted = false
39-
WHERE id = $3
38+
SET updated = now(), author = $1, deleted = false
39+
WHERE id = $2
40+
SQL
41+
42+
PG_DB.exec(request, author, id)
43+
end
44+
45+
def update_subscription_time(id : String)
46+
request = <<-SQL
47+
UPDATE channels
48+
SET subscribed = now()
49+
WHERE id = $1
4050
SQL
4151

42-
PG_DB.exec(request, Time.utc, author, id)
52+
PG_DB.exec(request, id)
4353
end
4454

4555
def update_mark_deleted(id : String)
4656
request = <<-SQL
4757
UPDATE channels
48-
SET updated = $1, deleted = true
49-
WHERE id = $2
58+
SET updated = now(), deleted = true
59+
WHERE id = $1
5060
SQL
5161

52-
PG_DB.exec(request, Time.utc, id)
62+
PG_DB.exec(request, id)
5363
end
5464

5565
# -------------------
@@ -67,14 +77,13 @@ module Invidious::Database::Channels
6777

6878
def select(ids : Array(String)) : Array(InvidiousChannel)?
6979
return [] of InvidiousChannel if ids.empty?
70-
values = ids.map { |id| %(('#{id}')) }.join(",")
7180

7281
request = <<-SQL
7382
SELECT * FROM channels
74-
WHERE id = ANY(VALUES #{values})
83+
WHERE id = ANY($1)
7584
SQL
7685

77-
return PG_DB.query_all(request, as: InvidiousChannel)
86+
return PG_DB.query_all(request, ids, as: InvidiousChannel)
7887
end
7988
end
8089

@@ -117,11 +126,11 @@ module Invidious::Database::ChannelVideos
117126

118127
request = <<-SQL
119128
SELECT * FROM channel_videos
120-
WHERE id IN (#{arg_array(ids)})
129+
WHERE id = ANY($1)
121130
ORDER BY published DESC
122131
SQL
123132

124-
return PG_DB.query_all(request, args: ids, as: ChannelVideo)
133+
return PG_DB.query_all(request, ids, as: ChannelVideo)
125134
end
126135

127136
def select_notfications(ucid : String, since : Time) : Array(ChannelVideo)

src/invidious/database/playlists.cr

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,52 +59,48 @@ module Invidious::Database::Playlists
5959
def update_subscription_time(id : String)
6060
request = <<-SQL
6161
UPDATE playlists
62-
SET subscribed = $1
63-
WHERE id = $2
62+
SET subscribed = now()
63+
WHERE id = $1
6464
SQL
6565

66-
PG_DB.exec(request, Time.utc, id)
66+
PG_DB.exec(request, id)
6767
end
6868

6969
def update_video_added(id : String, index : String | Int64)
7070
request = <<-SQL
7171
UPDATE playlists
7272
SET index = array_append(index, $1),
7373
video_count = cardinality(index) + 1,
74-
updated = $2
75-
WHERE id = $3
74+
updated = now()
75+
WHERE id = $2
7676
SQL
7777

78-
PG_DB.exec(request, index, Time.utc, id)
78+
PG_DB.exec(request, index, id)
7979
end
8080

8181
def update_video_removed(id : String, index : String | Int64)
8282
request = <<-SQL
8383
UPDATE playlists
8484
SET index = array_remove(index, $1),
8585
video_count = cardinality(index) - 1,
86-
updated = $2
87-
WHERE id = $3
86+
updated = now()
87+
WHERE id = $2
8888
SQL
8989

90-
PG_DB.exec(request, index, Time.utc, id)
90+
PG_DB.exec(request, index, id)
9191
end
9292

9393
# -------------------
9494
# Salect
9595
# -------------------
9696

97-
def select(*, id : String, raise_on_fail : Bool = false) : InvidiousPlaylist?
97+
def select(*, id : String) : InvidiousPlaylist?
9898
request = <<-SQL
9999
SELECT * FROM playlists
100100
WHERE id = $1
101101
SQL
102102

103-
if raise_on_fail
104-
return PG_DB.query_one(request, id, as: InvidiousPlaylist)
105-
else
106-
return PG_DB.query_one?(request, id, as: InvidiousPlaylist)
107-
end
103+
return PG_DB.query_one?(request, id, as: InvidiousPlaylist)
108104
end
109105

110106
def select_all(*, author : String) : Array(InvidiousPlaylist)

src/invidious/database/sessions.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ module Invidious::Database::SessionIDs
1010
def insert(sid : String, email : String, handle_conflicts : Bool = false)
1111
request = <<-SQL
1212
INSERT INTO session_ids
13-
VALUES ($1, $2, $3)
13+
VALUES ($1, $2, now())
1414
SQL
1515

1616
request += " ON CONFLICT (id) DO NOTHING" if handle_conflicts
1717

18-
PG_DB.exec(request, sid, email, Time.utc)
18+
PG_DB.exec(request, sid, email)
1919
end
2020

2121
# -------------------

src/invidious/database/users.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ module Invidious::Database::Users
143143
def clear_notifications(user : User)
144144
request = <<-SQL
145145
UPDATE users
146-
SET notifications = '{}', updated = $1
147-
WHERE email = $2
146+
SET notifications = '{}', updated = now()
147+
WHERE email = $1
148148
SQL
149149

150-
PG_DB.exec(request, Time.utc, user.email)
150+
PG_DB.exec(request, user.email)
151151
end
152152

153153
# -------------------

src/invidious/routes/feeds.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ module Invidious::Routes::Feeds
362362
end
363363

364364
if ucid = HTTP::Params.parse(URI.parse(topic).query.not_nil!)["channel_id"]?
365-
PG_DB.exec("UPDATE channels SET subscribed = $1 WHERE id = $2", Time.utc, ucid)
365+
Invidious::Database::Channels.update_subscription_time(ucid)
366366
elsif plid = HTTP::Params.parse(URI.parse(topic).query.not_nil!)["playlist_id"]?
367367
Invidious::Database::Playlists.update_subscription_time(plid)
368368
else

src/invidious/routes/playlists.cr

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,8 @@ module Invidious::Routes::Playlists
151151
page = env.params.query["page"]?.try &.to_i?
152152
page ||= 1
153153

154-
begin
155-
playlist = Invidious::Database::Playlists.select(id: plid, raise_on_fail: true)
156-
if !playlist || playlist.author != user.email
157-
return env.redirect referer
158-
end
159-
rescue ex
154+
playlist = Invidious::Database::Playlists.select(id: plid)
155+
if !playlist || playlist.author != user.email
160156
return env.redirect referer
161157
end
162158

@@ -235,12 +231,8 @@ module Invidious::Routes::Playlists
235231
page = env.params.query["page"]?.try &.to_i?
236232
page ||= 1
237233

238-
begin
239-
playlist = Invidious::Database::Playlists.select(id: plid, raise_on_fail: true)
240-
if !playlist || playlist.author != user.email
241-
return env.redirect referer
242-
end
243-
rescue ex
234+
playlist = Invidious::Database::Playlists.select(id: plid)
235+
if !playlist || playlist.author != user.email
244236
return env.redirect referer
245237
end
246238

0 commit comments

Comments
 (0)