-
Notifications
You must be signed in to change notification settings - Fork 897
Pretty print auto-migration plans #2065
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
Draft
kazimuth
wants to merge
8
commits into
master
Choose a base branch
from
jgilles/pretty_print
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
026ec2a
Add pretty printing code for automigrations to schema
kazimuth 13780fb
Add plumbing to print auto migration plans
kazimuth f975529
Update pretty printing code to be deterministic & better formatted
kazimuth 3ddb957
Add report verification in auto migration smoketest
kazimuth 64ff276
Fix critical security vulnerability in automigration code
kazimuth 68af111
Placate clippy
kazimuth 4d0f742
Fix scheduled_at syntax
kazimuth 7c6a071
RLS policies are printed in alphabetical order
kazimuth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,41 +6,32 @@ use spacetimedb_data_structures::{ | |
| use spacetimedb_lib::db::raw_def::v9::{RawRowLevelSecurityDefV9, TableType}; | ||
| use spacetimedb_sats::WithTypespace; | ||
|
|
||
| pub mod pretty_print; | ||
|
|
||
| pub type Result<T> = std::result::Result<T, ErrorStream<AutoMigrateError>>; | ||
|
|
||
| /// A plan for a migration. | ||
| #[derive(Debug)] | ||
| pub enum MigratePlan<'def> { | ||
| Manual(ManualMigratePlan<'def>), | ||
| Auto(AutoMigratePlan<'def>), | ||
| } | ||
|
|
||
| impl<'def> MigratePlan<'def> { | ||
| /// Get the old `ModuleDef` for this migration plan. | ||
| pub fn old_def(&self) -> &'def ModuleDef { | ||
| match self { | ||
| MigratePlan::Manual(plan) => plan.old, | ||
| MigratePlan::Auto(plan) => plan.old, | ||
| } | ||
| } | ||
|
|
||
| /// Get the new `ModuleDef` for this migration plan. | ||
| pub fn new_def(&self) -> &'def ModuleDef { | ||
| match self { | ||
| MigratePlan::Manual(plan) => plan.new, | ||
| MigratePlan::Auto(plan) => plan.new, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A plan for a manual migration. | ||
| /// `new` must have a reducer marked with `Lifecycle::Update`. | ||
| #[derive(Debug)] | ||
| pub struct ManualMigratePlan<'def> { | ||
| pub old: &'def ModuleDef, | ||
| pub new: &'def ModuleDef, | ||
| } | ||
|
|
||
| /// A plan for an automatic migration. | ||
| #[derive(Debug)] | ||
| pub struct AutoMigratePlan<'def> { | ||
|
|
@@ -52,45 +43,64 @@ pub struct AutoMigratePlan<'def> { | |
| /// There is also an implied check: that the schema in the database is compatible with the old ModuleDef. | ||
| pub prechecks: Vec<AutoMigratePrecheck<'def>>, | ||
| /// The migration steps to perform. | ||
| /// Order should not matter, as the steps are independent. | ||
| /// Order matters: `Remove`s of a particular `Def` must be ordered before `Add`s. | ||
| pub steps: Vec<AutoMigrateStep<'def>>, | ||
| } | ||
|
|
||
| /// Checks that must be performed before performing an automatic migration. | ||
| /// These checks can access table contents and other database state. | ||
| #[derive(PartialEq, Eq, Debug)] | ||
| #[derive(PartialEq, Eq, Debug, PartialOrd, Ord)] | ||
| pub enum AutoMigratePrecheck<'def> { | ||
| /// Perform a check that adding a sequence is valid (the relevant column contains no values | ||
| /// greater than the sequence's start value). | ||
| CheckAddSequenceRangeValid(<SequenceDef as ModuleDefLookup>::Key<'def>), | ||
| } | ||
|
|
||
| /// A step in an automatic migration. | ||
| #[derive(PartialEq, Eq, Debug)] | ||
| #[derive(PartialEq, Eq, Debug, PartialOrd, Ord)] | ||
| pub enum AutoMigrateStep<'def> { | ||
| // It is important FOR CORRECTNESS that `Remove` variants are declared before `Add` variants in this enum! | ||
| // | ||
| // The ordering is used to sort the steps of an auto-migration. | ||
| // If adds go before removes, and the user tries to remove an index and then re-add it with new configuration, | ||
| // the following can occur: | ||
| // | ||
| // 1. `AddIndex("indexname")` | ||
| // 2. `RemoveIndex("indexname")` | ||
| // | ||
| // This results in the index being added -- which, at time of writing, does nothing -- and then removed, | ||
| // resulting in the intended index not being created. | ||
| // | ||
| // For now, we just ensure that we declare all `Remove` variants before `Add` variants | ||
| // and let `#[derive(PartialOrd)]` take care of the rest. | ||
| // TODO: when this enum is made serializable, a more durable fix will be needed here. | ||
| // Probably we will want to have separate arrays of add and remove steps. | ||
| // | ||
| /// Remove an index. | ||
| RemoveIndex(<IndexDef as ModuleDefLookup>::Key<'def>), | ||
| /// Remove a constraint. | ||
| RemoveConstraint(<ConstraintDef as ModuleDefLookup>::Key<'def>), | ||
| /// Remove a sequence. | ||
| RemoveSequence(<SequenceDef as ModuleDefLookup>::Key<'def>), | ||
| /// Remove a schedule annotation from a table. | ||
| RemoveSchedule(<ScheduleDef as ModuleDefLookup>::Key<'def>), | ||
| /// Remove a row-level security query. | ||
| RemoveRowLevelSecurity(<RawRowLevelSecurityDefV9 as ModuleDefLookup>::Key<'def>), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: this change got pulled out of this PR. |
||
|
|
||
| /// Add a table, including all indexes, constraints, and sequences. | ||
| /// There will NOT be separate steps in the plan for adding indexes, constraints, and sequences. | ||
| AddTable(<TableDef as ModuleDefLookup>::Key<'def>), | ||
| /// Add an index. | ||
| AddIndex(<IndexDef as ModuleDefLookup>::Key<'def>), | ||
| /// Remove an index. | ||
| RemoveIndex(<IndexDef as ModuleDefLookup>::Key<'def>), | ||
| /// Remove a constraint. | ||
| RemoveConstraint(<ConstraintDef as ModuleDefLookup>::Key<'def>), | ||
| /// Add a sequence. | ||
| AddSequence(<SequenceDef as ModuleDefLookup>::Key<'def>), | ||
| /// Remove a sequence. | ||
| RemoveSequence(<SequenceDef as ModuleDefLookup>::Key<'def>), | ||
| /// Change the access of a table. | ||
| ChangeAccess(<TableDef as ModuleDefLookup>::Key<'def>), | ||
| /// Add a schedule annotation to a table. | ||
| AddSchedule(<ScheduleDef as ModuleDefLookup>::Key<'def>), | ||
| /// Remove a schedule annotation from a table. | ||
| RemoveSchedule(<ScheduleDef as ModuleDefLookup>::Key<'def>), | ||
| /// Add a row-level security query. | ||
| AddRowLevelSecurity(<RawRowLevelSecurityDefV9 as ModuleDefLookup>::Key<'def>), | ||
| /// Remove a row-level security query. | ||
| RemoveRowLevelSecurity(<RawRowLevelSecurityDefV9 as ModuleDefLookup>::Key<'def>), | ||
|
|
||
| /// Change the access of a table. | ||
| ChangeAccess(<TableDef as ModuleDefLookup>::Key<'def>), | ||
| } | ||
|
|
||
| /// Something that might prevent an automatic migration. | ||
|
|
@@ -181,6 +191,9 @@ pub fn ponder_auto_migrate<'def>(old: &'def ModuleDef, new: &'def ModuleDef) -> | |
|
|
||
| let ((), (), (), (), ()) = (tables_ok, indexes_ok, sequences_ok, constraints_ok, rls_ok).combine_errors()?; | ||
|
|
||
| plan.steps.sort(); | ||
| plan.prechecks.sort(); | ||
|
|
||
| Ok(plan) | ||
| } | ||
|
|
||
|
|
@@ -418,9 +431,14 @@ fn auto_migrate_constraints(plan: &mut AutoMigratePlan, new_tables: &HashSet<&Id | |
| .collect_all_errors() | ||
| } | ||
|
|
||
| // Because we can refer to many tables and fields on the row level-security query, we need to remove all of them, | ||
| // then add the new ones, instead of trying to track the graph of dependencies. | ||
| fn auto_migrate_row_level_security(plan: &mut AutoMigratePlan) -> Result<()> { | ||
| // Because we can refer to many tables and fields on the row level-security query, we need to remove all of them, | ||
| // then add the new ones, instead of trying to track the graph of dependencies. | ||
| // When pretty-printing, steps to remove and re-add a row-level-security policy are not shown, | ||
| // since they are an implementation detail that will be surprising to users. | ||
| // TODO: change this to not add-and-remove unchanged policies, and hand the responsibility for reinitializing | ||
| // queries to the `core` crate instead. | ||
| // When you do this, you will need to update `pretty_print.rs`. | ||
| for rls in plan.old.row_level_security() { | ||
| plan.steps.push(AutoMigrateStep::RemoveRowLevelSecurity(rls.key())); | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: I guess I should check if the terminal supports ANSI color codes here, since the summary may include those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this TODO meant for this PR, or should it be a comment/ticket?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that this is the best UX. It seems like these update summaries can be pretty large, and I don't think that most users are interested in reviewing them. Could we put this behind a CLI flag
--show-migration-summaryor something like that?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a reminder to myself. Also yeah I can put it behind a flag, sure.
TBH I'm not sure a flag is needed -- our tools are already pretty chatty and I don't think this adds an overwhelming amount of information. But probably a better long-term solution would be a history audit tool on the website; that would be a lot less ephemeral than console output. It's a larger project though.
I do think it's particularly important to print an explanation when automatic migrations fail -- but, come to think of it, this PR hasn't implemented that, so I suppose I should do it as well, either here or in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that reminder meant to be implemented in this PR? (i.e. should I hold off on reviewing?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this PR yeah, I'll mark as draft until it's ready again.