Update view names to avoid collisions

This commit is contained in:
Omar Roth 2019-04-10 19:56:38 -05:00
parent 8640d6bb1e
commit e1c78fcbd3
3 changed files with 33 additions and 23 deletions

View File

@ -666,7 +666,7 @@ get "/search" do |env|
user = env.get? "user" user = env.get? "user"
if user if user
user = user.as(User) user = user.as(User)
view_name = "subscriptions_#{sha256(user.email)[0..7]}" view_name = "subscriptions_#{sha256(user.email)}"
end end
channel = nil channel = nil
@ -1114,7 +1114,7 @@ post "/login" do |env|
PG_DB.exec("INSERT INTO users VALUES (#{args})", user_array) PG_DB.exec("INSERT INTO users VALUES (#{args})", user_array)
PG_DB.exec("INSERT INTO session_ids VALUES ($1, $2, $3)", sid, email, Time.now) PG_DB.exec("INSERT INTO session_ids VALUES ($1, $2, $3)", sid, email, Time.now)
view_name = "subscriptions_#{sha256(user.email)[0..7]}" view_name = "subscriptions_#{sha256(user.email)}"
PG_DB.exec("CREATE MATERIALIZED VIEW #{view_name} AS \ PG_DB.exec("CREATE MATERIALIZED VIEW #{view_name} AS \
SELECT * FROM channel_videos WHERE \ SELECT * FROM channel_videos WHERE \
ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')::text[]) \ ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')::text[]) \
@ -1834,7 +1834,7 @@ post "/delete_account" do |env|
next templated "error" next templated "error"
end end
view_name = "subscriptions_#{sha256(user.email)[0..7]}" view_name = "subscriptions_#{sha256(user.email)}"
PG_DB.exec("DELETE FROM users * WHERE email = $1", user.email) PG_DB.exec("DELETE FROM users * WHERE email = $1", user.email)
PG_DB.exec("DELETE FROM session_ids * WHERE email = $1", user.email) PG_DB.exec("DELETE FROM session_ids * WHERE email = $1", user.email)
PG_DB.exec("DROP MATERIALIZED VIEW #{view_name}") PG_DB.exec("DROP MATERIALIZED VIEW #{view_name}")
@ -1969,7 +1969,7 @@ get "/feed/subscriptions" do |env|
notifications = PG_DB.query_one("SELECT notifications FROM users WHERE email = $1", user.email, notifications = PG_DB.query_one("SELECT notifications FROM users WHERE email = $1", user.email,
as: Array(String)) as: Array(String))
view_name = "subscriptions_#{sha256(user.email)[0..7]}" view_name = "subscriptions_#{sha256(user.email)}"
if preferences.notifications_only && !notifications.empty? if preferences.notifications_only && !notifications.empty?
# Only show notifications # Only show notifications
@ -2253,7 +2253,7 @@ get "/feed/private" do |env|
sort = env.params.query["sort"]? sort = env.params.query["sort"]?
sort ||= "published" sort ||= "published"
view_name = "subscriptions_#{sha256(user.email)[0..7]}" view_name = "subscriptions_#{sha256(user.email)}"
if latest_only if latest_only
videos = PG_DB.query_all("SELECT DISTINCT ON (ucid) * FROM #{view_name} \ videos = PG_DB.query_all("SELECT DISTINCT ON (ucid) * FROM #{view_name} \

View File

@ -54,7 +54,7 @@ def refresh_feeds(db, logger, max_threads = 1)
db.query("SELECT email FROM users") do |rs| db.query("SELECT email FROM users") do |rs|
rs.each do rs.each do
email = rs.read(String) email = rs.read(String)
view_name = "subscriptions_#{sha256(email)[0..7]}" view_name = "subscriptions_#{sha256(email)}"
if active_threads >= max_threads if active_threads >= max_threads
if active_channel.receive if active_channel.receive
@ -65,30 +65,40 @@ def refresh_feeds(db, logger, max_threads = 1)
active_threads += 1 active_threads += 1
spawn do spawn do
begin begin
db.query("SELECT * FROM #{view_name} LIMIT 1") do |rs| # Drop outdated views
# Drop view that doesn't contain same number of rows as ChannelVideo column_array = get_column_array(db, view_name)
if ChannelVideo.from_rs(rs)[0]?.try &.to_a.size.try &.!= rs.column_count ChannelVideo.to_type_tuple.each_with_index do |name, i|
if name != column_array[i]?
logger.write("DROP MATERIALIZED VIEW #{view_name}\n")
db.exec("DROP MATERIALIZED VIEW #{view_name}") db.exec("DROP MATERIALIZED VIEW #{view_name}")
raise "valid schema does not exist" raise "view does not exist"
end end
end end
db.exec("REFRESH MATERIALIZED VIEW #{view_name}") db.exec("REFRESH MATERIALIZED VIEW #{view_name}")
rescue ex rescue ex
# Create view if it doesn't exist # Rename old views
if ex.message.try &.ends_with?("does not exist") begin
legacy_view_name = "subscriptions_#{sha256(email)[0..7]}"
db.exec("SELECT * FROM #{legacy_view_name} LIMIT 0")
logger.write("RENAME MATERIALIZED VIEW #{legacy_view_name}\n")
db.exec("ALTER MATERIALIZED VIEW #{legacy_view_name} RENAME TO #{view_name}")
rescue ex
begin
# While iterating through, we may have an email stored from a deleted account # While iterating through, we may have an email stored from a deleted account
if db.query_one?("SELECT true FROM users WHERE email = $1", email, as: Bool) if db.query_one?("SELECT true FROM users WHERE email = $1", email, as: Bool)
logger.write("CREATE #{view_name}\n")
db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \ db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \
SELECT * FROM channel_videos WHERE \ SELECT * FROM channel_videos WHERE \
ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{email.gsub("'", "\\'")}')::text[]) \ ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{email.gsub("'", "\\'")}')::text[]) \
ORDER BY published DESC;") ORDER BY published DESC;")
logger.write("CREATE #{view_name}\n")
end end
else rescue ex
logger.write("REFRESH #{email} : #{ex.message}\n") logger.write("REFRESH #{email} : #{ex.message}\n")
end end
end end
end
active_channel.send(true) active_channel.send(true)
end end

View File

@ -125,7 +125,7 @@ def get_user(sid, headers, db, refresh = true)
ON CONFLICT (id) DO NOTHING", sid, user.email, Time.now) ON CONFLICT (id) DO NOTHING", sid, user.email, Time.now)
begin begin
view_name = "subscriptions_#{sha256(user.email)[0..7]}" view_name = "subscriptions_#{sha256(user.email)}"
db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \ db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \
SELECT * FROM channel_videos WHERE \ SELECT * FROM channel_videos WHERE \
ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')::text[]) \ ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')::text[]) \
@ -147,7 +147,7 @@ def get_user(sid, headers, db, refresh = true)
ON CONFLICT (id) DO NOTHING", sid, user.email, Time.now) ON CONFLICT (id) DO NOTHING", sid, user.email, Time.now)
begin begin
view_name = "subscriptions_#{sha256(user.email)[0..7]}" view_name = "subscriptions_#{sha256(user.email)}"
db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \ db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \
SELECT * FROM channel_videos WHERE \ SELECT * FROM channel_videos WHERE \
ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')::text[]) \ ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')::text[]) \