Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom migration runner, forbid some diesel migration commands, fix old migrations #4673

Merged
merged 107 commits into from
Jan 8, 2025

Conversation

dullbananas
Copy link
Collaborator

@dullbananas dullbananas commented Apr 28, 2024

Reverting and testing migrations will be done with lemmy_server migration commands, and diesel migration commands will do nothing and fail when trying to run or revert migrations.

Benefits:

  • Changes in replaceable_schema will no longer require a corresponding migration
  • Migrations will now be run and reverted without the "r" schema applied, instead of needing to work both with and without it
  • All migrations, not just the newest one, will be tested with schema diff (moved to Improve migration diff check and fix old migrations #5204)
  • Multiple lemmy_server processes trying to run migrations at the same time will now be prevented
  • Name and duration of each migration will be displayed when run in lemmy_server

#4333 (comment)

The new tests (even just reverting all migrations) revealed some mistakes in old migrations, so I fixed those, and I also discovered that 'now'::timestamp in old views used the timestamp of the view's creation instead of the current timestamp.

@dullbananas dullbananas changed the title Custom migration runner Custom migration runner (diesel migration no longer allowed) Apr 28, 2024
@dullbananas dullbananas changed the title Custom migration runner (diesel migration no longer allowed) Custom migration runner (some diesel migration commands no longer allowed) May 11, 2024
@dullbananas dullbananas changed the title Custom migration runner (some diesel migration commands no longer allowed) Add custom migration runner, forbid some diesel migration commands, fix old migrations May 13, 2024
@dessalines
Copy link
Member

Once this passes, I'll test with a local production DB to make sure everything is okay. Then we should get this merged because it is an older PR.

@dessalines
Copy link
Member

Trying to test this with a prod db, but running this with cd docker && ./docker_update.sh gives the following error:

thread 'main' panicked at crates/db_schema/src/schema_setup.rs:43:6
failed to get migration source: MigrationDirectoryNotFound("/")

@dullbananas
Copy link
Collaborator Author

try again with the new commit

@dessalines
Copy link
Member

dessalines commented Dec 20, 2024

Error is now MigrationDirectoryNotFound("/lemmy/crates/db_schema")

Maybe we need to add a different env var for that. Or just embed them even if its debug.

@dullbananas
Copy link
Collaborator Author

I will move the use of FileBasedMigrations to a separate PR

@dessalines
Copy link
Member

Testing this out now, seems to be going okay, but the smoosh migration is taking a long time for a prod db. I'll keep you posted.

@dessalines
Copy link
Member

K it did work correctly, so this can probably be merged. (well, loading pages is broken but that's not due to this PR).

Here's the migration times btw, for a lemmy.ml prod DB on my dev machine.

The smoosh migration took ~3 hours, which is unfortunate but I greatly doubt there's a way to speed that up.

lemmy-1  | Running Database migrations (This may take a long time)...
lemmy-1  | 9ms run 2023-10-24-140438_enable_private_messages
lemmy-1  | 5ms run 2024-03-04-143245_remove_show_scores_column
lemmy-1  | 10ms run 2024-04-08-204327_custom_emoji_tagline_changes
lemmy-1  | 5ms run 2024-06-24-000000_ap_id_triggers
lemmy-1  | 4275013ms run 2024-07-01-014711_exponential_controversy
lemmy-1  | 8145ms run 2024-08-03-155932_increase_post_url_max_length
lemmy-1  | 37ms run 2024-09-12-130204_drop-enable-nsfw
lemmy-1  | 24ms run 2024-09-16-000000_default_comment_sort_type
lemmy-1  | 5071ms run 2024-09-16-095656_schedule-post
lemmy-1  | 232ms run 2024-09-16-174833_create_oauth_provider
lemmy-1  | 14ms run 2024-09-20-134838_add_federation_vote_rejection
lemmy-1  | 19ms run 2024-09-23-133038_remove_auto_expand
lemmy-1  | 10ms run 2024-10-16-141718_add_short_community_description
lemmy-1  | 536973ms run 2024-10-18-074533_no-individual-inboxes
lemmy-1  | 150ms run 2024-10-23-091053_comment-vote-remote-postid
lemmy-1  | 14691ms run 2024-10-29-090055_private-community
lemmy-1  | 34ms run 2024-11-01-233231_add_mark_fetched_posts_as_read
lemmy-1  | 10463904ms run 2024-11-10-134311_smoosh-tables-together
lemmy-1  | 11ms run 2024-11-12-090437_move-triggers
lemmy-1  | 52ms run 2024-11-18-012112_forbid_diesel_cli
lemmy-1  | 161ms run 2024-11-18-012113_custom_migration_runner

cc @Nutomic

#[arg(long, default_value_t = 1)]
number: u64,
},
}
Copy link
Member

Choose a reason for hiding this comment

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

These params need documentation, especially number is unclear.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@@ -0,0 +1,40 @@
#![expect(clippy::expect_used)]
Copy link
Member

Choose a reason for hiding this comment

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

Also mark this file as cfg[test] so it cannot accidentally be used from production code and result in crashes from expect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved the attribute here

/// Reduces clutter
fn o() -> Options {
Options::default()
}
Copy link
Member

Choose a reason for hiding this comment

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

Define a variable instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@Nutomic
Copy link
Member

Nutomic commented Jan 3, 2025

The smoosh migration took ~3 hours, which is unfortunate but I greatly doubt there's a way to speed that up.

Damn thats a very long downtime. It would definitely be good if we could optimize that in some way.


let duration = start_time.elapsed().as_millis();
let name = migration.name();
println!("{duration}ms revert {name}");
Copy link
Member

@Nutomic Nutomic Jan 3, 2025

Choose a reason for hiding this comment

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

You can convert the duration to chrono::TimeDelta which has a Display impl and should work better to display longer durations. Same for run_migration.

https://docs.rs/chrono/latest/chrono/struct.TimeDelta.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@dessalines dessalines requested a review from Nutomic January 5, 2025 16:47
@dessalines
Copy link
Member

Damn thats a very long downtime. It would definitely be good if we could optimize that in some way.

I looked over migrations/2024-11-10-134311_smoosh-tables-together/up.sql , and I don't see anything else we could do for it. Its going to massively increase join performance tho, so the 1-day downtime doesn't bother me too much.

@dullbananas
Copy link
Collaborator Author

I'm now working on a PR to try to optimize some of the migrations, including smoosh-tables-together

@Nutomic Nutomic merged commit 6b1b294 into LemmyNet:main Jan 8, 2025
1 check passed
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