Skip to content

backup database#4359

Open
broquemonsieur wants to merge 9 commits intoiv-org:masterfrom
broquemonsieur:invidious_migrations
Open

backup database#4359
broquemonsieur wants to merge 9 commits intoiv-org:masterfrom
broquemonsieur:invidious_migrations

Conversation

@broquemonsieur
Copy link

@broquemonsieur broquemonsieur commented Dec 28, 2023

the first step in addressing automatic migration
#4190 (comment)

@broquemonsieur broquemonsieur requested review from a team and unixfox as code owners December 28, 2023 17:36
@broquemonsieur broquemonsieur changed the title Invidious migrations backup database Dec 29, 2023
@github-actions

This comment has been minimized.

@github-actions github-actions bot added the stale label Mar 28, 2024
@syeopite syeopite removed the stale label Mar 28, 2024
)
SQL

@db.exec("DROP TABLE backup.#{table_name}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem I see with that is that it might nuke a perfectly valid backup if invidious is interrupted mid-migration and the user tries to run it again immediately after.

Copy link
Author

@broquemonsieur broquemonsieur Jul 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so there's stage 1,2,3. the migration can get interrupted pre-1,pre-2,pre-3 & post-3.

pre-1 : The migration wouldn't be run (hopefully upon re-attempting migration the migration is question will still be queued up)

pre-2 : At this point, there exists a table backup.cool_table - it is empty. An interuption now, would leave a empty table that'd be re-populated upon re-attempt

pre-3 : at this point, there is a backup.cool_table and a lack of backup.cool_table (this latter one being cool_tables version right before migration). At this point, critical data is destroyed and we must replenish it ASAP - an interuption now would be fatal

Ok - I see what you're saying, what if I created a copy of backup.cool_table (Pre-Migration's Version) earlier on?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I see! Though I still don't understand something:

Stage 1: You create the backup table (if it doesn't already exist)
Stage 2: You remove that backup table
Stage 3: You create a new backup table from the content of the regular table

If my understanding is correct, stage 1 is useless.

Copy link
Author

@broquemonsieur broquemonsieur Aug 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following is based off the assumption that former broquemonsieur would write SQL that looks redundant/hacky but actually is doing the right thing due to SQL's odd and vague limitations where you cannot do something that should be simple or the only way to achieve a simple operation (that seemingly should be a one-liner) is the combo of two ops (a.k.a. two-liner). Present day broquemonsieur has failing memory but can vouch for this ^ :)


Meet Sonic, a new Invidious host. At this point in her users have a few playlists saved and Sonic has not migrated her DB...ever. Her users playlists look like this:

title author
Mark Hamill Moments kilowattwomxn
Awesome Trailers movieDad

Sonic migrates her DB:

Stage 1 - Creates a table backup.playlists. It is empty
Stage 2 - This op done nothing (it fails silently). (If Sonic were to have a backup of her users' playlists already there, it would be destroyed now to make way for a snapshot of her user's present-day playlists) tl;dr This stages deletes the prior version of backup.playlists, not the one you just read out in Stage 1 (Yes, SQL forums agree this is weird that SQL has to be written this way lol)
Stage 3 - This op populates backup.playlists with

title author
Mark Hamill Moments kilowattwomxn
Awesome Trailers movieDad

Meet Amy, a jaded Invidious host who already has a backup for her users' playlists.

Amy migrates her DB:

you get the idea :)


That should clear it up (mainly I was trying to explain for my recollection to myself lol). But basically, the system is vulnerable . At time this comment is sent, I begin efforts to prevent memory loss given an asteroid strike at any point in time

Copy link
Author

@broquemonsieur broquemonsieur Aug 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Reporting back from my coding session. Now, we only destroy the "second most recent" backup. Whenever an asteroid strikes, the "most recent" backup will always be preserved.

Copy link
Author

@broquemonsieur broquemonsieur Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asteroid strikes are useless analogies.
Meet Dale, he is chilling in Mexican beach propping a MacBook Air on pool tube. He is SSH'd to his Invidious server and tries to do DB migration - a major part of which is DB backup.

The hotel Wifi could go out, the MacBook could get wet or the Server itself could shutdown at any point during this critical time and still Dale's users would not lose

  1. "most recent" backup
  2. regular DB tables

The "2nd most recent" backup, however, is susceptible to being lost in the event of a crash but when you hear "2nd most recent" think "safety cap for most recent" - it takes the bullet for it

@github-actions github-actions bot added the stale label Jul 21, 2024
@syeopite syeopite removed the stale label Jul 21, 2024
@iv-org iv-org deleted a comment from github-actions bot Jul 25, 2024
)
SQL

@db.exec("DROP TABLE backup.#{table_name}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I see! Though I still don't understand something:

Stage 1: You create the backup table (if it doesn't already exist)
Stage 2: You remove that backup table
Stage 3: You create a new backup table from the content of the regular table

If my understanding is correct, stage 1 is useless.

Comment on lines +58 to +61
table_names_request = <<-SQL
SELECT tablename FROM pg_catalog.pg_tables
WHERE schemaname = 'public'
SQL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, does this copy views, data types, and other database elements?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That SQL query retrieves only table names from the pg_tables view in PostgreSQL. It does not include views, data types, or other database elements.

Views: Views are stored in the pg_views catalog.
Data Types: Data types are found in the pg_type catalog.
Other Database Elements: Other elements, such as indexes or sequences, are in different catalog tables like pg_indexes and pg_sequences.

If you need to include views or other database elements, additional queries targeting the respective catalog tables would be required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants