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

Use less verbose Comparator::compareSchema() #4704

Closed

Conversation

trompette
Copy link
Contributor

Q A
Type improvement
BC Break no
Fixed issues none

Summary

While working on #4698, I noticed a small inconsistency when comparing schemas with Comparator.

This PR uses an existing shortcut everywhere instead of a verbose alternative.

This commit uses an existing shortcut instead of a verbose alternative.
$schemaDiff = $comparator->compare($this, $toSchema);

return $schemaDiff->toSql($platform);
return Comparator::compareSchemas($this, $toSchema)->toSql($platform);
Copy link
Member

Choose a reason for hiding this comment

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

This is the only static Comparator method, so I'd rather replace it with a non-static one for consistency with the rest of the API and deprecated the compare() method since its purpose is unclear from the name.

Please also see #4659 (comment).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at the code and how it is used, it is quite odd the other static diffing methods are not present. Specially given the Comparator object does not have any state.

As mentionned in #4659 (comment), if the goal is to rename all methods anyways, maybe we could introduce a new Differ class with the following public API:

class Differ
{
    public static function compareSchemas(Schema $s1, Schema $s2): SchemaDiff;
    public static function compareSequence(Sequence $s1, Sequence $s2): bool;
    public static function compareTable(Table $t1, Table $t2): TableDiff|false;
    public static function compareForeignKey(ForeignKeyConstraint $k1, ForeignKeyConstraint $k2): bool;
    public static function compareColumn(Column $c1, Column $c2): string[]; // could also be ColumnDiff
    public static function compareIndex(Index $i1, Index $i2): bool;
}

(Now that I see the API, maybe the methods returning a boolean should be renamed for clarity or moved to a new class.)

What do you think ?

Copy link
Member

@morozov morozov Jul 9, 2021

Choose a reason for hiding this comment

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

"Differ" is a bit odd name, I'd prefer the same class name: the comparator compares things and returns a diff – looks like a solid naming foundation to me. As for the specific method names, I think they should contain the plural form of the object (e.g. compareColumns()).

IIRC, we'll need to only migrate compareSchemas() from being static to non-static which doesn't require introduction of a new class. We can deprecate non-static calls to the method (which PHP allows) and make it non-static in the next major release.

Oh, and I don't think static methods is a good idea. This way, it will be impossible to evolve the API further, e.g. split it into multiple classes, define interfaces, pass it as a dependency, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"Differ" sounded great from my suburb of Paris, France... 😉

I did not think about evolving the Comparator API further, you are quite right!

Copy link
Member

Choose a reason for hiding this comment

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

"Differ" sounded great from my suburb of Paris, France... 😉

I think that would be perfect in the Java world where they smash an -er on top of any noun :-)

@trompette
Copy link
Contributor Author

Let's close this PR. I will open another one to make Comparator API more consistent.

@trompette trompette closed this Jul 9, 2021
@trompette trompette deleted the use-comparator-statically branch July 9, 2021 16:02
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants