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,28 +65,38 @@ 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
# While iterating through, we may have an email stored from a deleted account legacy_view_name = "subscriptions_#{sha256(email)[0..7]}"
if db.query_one?("SELECT true FROM users WHERE email = $1", email, as: Bool)
db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \ db.exec("SELECT * FROM #{legacy_view_name} LIMIT 0")
SELECT * FROM channel_videos WHERE \ logger.write("RENAME MATERIALIZED VIEW #{legacy_view_name}\n")
ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{email.gsub("'", "\\'")}')::text[]) \ db.exec("ALTER MATERIALIZED VIEW #{legacy_view_name} RENAME TO #{view_name}")
ORDER BY published DESC;") rescue ex
logger.write("CREATE #{view_name}\n") begin
# 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)
logger.write("CREATE #{view_name}\n")
db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \
SELECT * FROM channel_videos WHERE \
ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{email.gsub("'", "\\'")}')::text[]) \
ORDER BY published DESC;")
end
rescue ex
logger.write("REFRESH #{email} : #{ex.message}\n")
end end
else
logger.write("REFRESH #{email} : #{ex.message}\n")
end end
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[]) \