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

Feature/untangle criteria class #1747

Merged

Conversation

mringler
Copy link
Contributor

@mringler mringler commented Jun 4, 2021

I was in the mood for some refactoring, and decided it is time to go for the white whale, aka the Criteria classes.

I believe those are Propel's achilles heel, since they are the center classes of ActiveQuery and parent class to all Query classes, and completely unmaintainable - with together almost 6000 lines of code, a fuzzy extension hierarchy, and them doing literally dozens of things at once.
In practice, that is acceptable, as long as everything works perfectly. But almost all bugs I have fixed were in those three classes, and I am pretty sure there are more. Also, I am pretty sure that a lot of performance is lost by hacky workarounds, of which I have seen plenty. I think those bugs and workarounds show that the information flow in those classes is not understandable anymore.

So here is a proposal to start getting things back in order by removing sql query generation and execution out of those classes. This already removes more than 500 lines of code.

The basic idea is to have the doSelect(), doInsert(), do...() methods delegate to dedicated classes. Here is an overview of the new classes (not sure if the tree output renders in all browsers):

ActiveQuery
├── BaseModelCriteria.php
├── Criteria.php
├── ModelCriteria.php
├── ...
├── QueryExecutor
│   ├── AbstractQueryExecutor.php
│   ├── CountQueryExecutor.php
│   ├── DeleteAllQueryExecutor.php
│   ├── DeleteQueryExecutor.php
│   ├── InsertQueryExecutor.php
│   ├── QueryExecutionException.php
│   ├── SelectQueryExecutor.php
│   └── UpdateQueryExecutor.php
└── SqlBuilder
    ├── AbstractSqlQueryBuilder.php
    ├── CountQuerySqlBuilder.php
    ├── DeleteQuerySqlBuilder.php
    ├── InsertQuerySqlBuilder.php
    ├── SelectQuerySqlBuilder.php
    └── UpdateQuerySqlBuilder.php

The changes are very thoroughly tested through the Criteria classes and a few new tests.

@mringler
Copy link
Contributor Author

mringler commented Jun 4, 2021

Hmm, tests fail before they even start:

Tests started in temp /tmp.
Could not read "tests/sqlite.phpunit.xml".
Error: Process completed with exit code 2.

I know it is what everybody says, but I don't see how this is my fault. I have not changed the phpunit.xml files.

Please let me know how to proceed.

@mringler mringler mentioned this pull request Jun 5, 2021
@mringler mringler force-pushed the feature/untangle_criteria_class branch 2 times, most recently from b4bcf15 to 60889bc Compare June 10, 2021 14:45
@codecov-commenter
Copy link

codecov-commenter commented Jun 10, 2021

Codecov Report

Merging #1747 (a70d586) into master (a8f3afa) will increase coverage by 0.03%.
The diff coverage is 94.31%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1747      +/-   ##
============================================
+ Coverage     87.89%   87.93%   +0.03%     
- Complexity     7827     7870      +43     
============================================
  Files           267      281      +14     
  Lines         22546    22670     +124     
============================================
+ Hits          19817    19934     +117     
- Misses         2729     2736       +7     
Flag Coverage Δ
5-max 87.93% <94.31%> (+0.03%) ⬆️
7.4 87.93% <94.31%> (+0.03%) ⬆️
agnostic 67.44% <71.76%> (-5.91%) ⬇️
mysql 70.38% <92.71%> (+0.19%) ⬆️
pgsql 70.33% <93.50%> (+0.20%) ⬆️
sqlite 68.30% <91.33%> (+0.19%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...untime/ActiveQuery/Criterion/AbstractCriterion.php 89.10% <ø> (ø)
.../ActiveQuery/QueryExecutor/InsertQueryExecutor.php 81.25% <81.25%> (ø)
...tiveQuery/QueryExecutor/DeleteAllQueryExecutor.php 83.33% <83.33%> (ø)
...me/ActiveQuery/SqlBuilder/CountQuerySqlBuilder.php 84.00% <84.00%> (ø)
src/Propel/Runtime/ActiveQuery/ModelCriteria.php 95.31% <87.50%> (+0.48%) ⬆️
src/Propel/Runtime/Adapter/Pdo/PdoAdapter.php 90.96% <92.30%> (-0.47%) ⬇️
.../ActiveQuery/QueryExecutor/UpdateQueryExecutor.php 94.44% <94.44%> (ø)
...e/ActiveQuery/SqlBuilder/UpdateQuerySqlBuilder.php 94.64% <94.64%> (ø)
...e/ActiveQuery/SqlBuilder/SelectQuerySqlBuilder.php 94.66% <94.66%> (ø)
...e/ActiveQuery/SqlBuilder/InsertQuerySqlBuilder.php 96.15% <96.15%> (ø)
... and 31 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a8f3afa...a70d586. Read the comment docs.

@mringler mringler force-pushed the feature/untangle_criteria_class branch 2 times, most recently from b263a3e to a723dc3 Compare June 10, 2021 15:33
@mringler
Copy link
Contributor Author

Added test fixed from #1751. I'll remove them and rebase when that is resolved.

@mringler mringler force-pushed the feature/untangle_criteria_class branch from a723dc3 to b2f0f0a Compare June 14, 2021 10:20
@mringler mringler force-pushed the feature/untangle_criteria_class branch from 105c69f to 8c06fca Compare June 15, 2021 06:12
@dereuromark
Copy link
Contributor

Small conflict btw.

@mringler
Copy link
Contributor Author

This is probably not going to get merged, so I am just going to close the PR.
Should you actually want to see this merged, let me know, and I'll fix the conflict and re-open.

@mringler mringler closed this Aug 10, 2021
@dereuromark
Copy link
Contributor

From what I can see it cleans up the baseline quite a bit
So it seems worth finishing this up IMO.

@mringler
Copy link
Contributor Author

Great, I have fixed the conflict

@mringler mringler reopened this Aug 10, 2021
@dereuromark
Copy link
Contributor

@nederdirk can also maybe help with the review for this to be merged today?

@mringler mringler force-pushed the feature/untangle_criteria_class branch from 81b74e2 to 46c4303 Compare August 10, 2021 10:31
@mringler mringler force-pushed the feature/untangle_criteria_class branch from 46c4303 to 2aa9adc Compare August 10, 2021 10:32
Copy link
Contributor

@nederdirk nederdirk left a comment

Choose a reason for hiding this comment

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

I'm not done yet, but it's lunch time for me now 😄

use Propel\Runtime\ServiceContainer\ServiceContainerInterface;
use Throwable;

abstract class AbstractQueryExecutor
Copy link
Contributor

Choose a reason for hiding this comment

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

This may be a good opportunity to add comments about 'what is the structure of the subsystem these classes represent', i.e.

`QueryExecutor`'s responsibility is to translate a `Criteria` object to the specific RDBMS dialect and execute the query.
Use it like this: <insert example here>

Copy link
Contributor

Choose a reason for hiding this comment

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

Comments can also be added later if we don't have time now, as they are not changing the functional parts.

* @param \Propel\Runtime\ActiveQuery\Criteria $criteria
* @param \Propel\Runtime\Connection\ConnectionInterface|null $con
*/
public function __construct(Criteria $criteria, ?ConnectionInterface $con = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

with abstract classes, it might be good to think about whether the constructor should be final: given a class-string<AbstractQueryExecutor> $className, should users of the Propel library be able to do new $className($criteria, $connection);?

If concrete implementing classes (i.e. extends AbstractQueryExecutors) should be able to override the constructor, consider having AbstractQueryExecutor implement an interface ConstructableQueryExecutor { function __construct(Criteria $criteria, ?ConnectionInterface $con = null); }, so implementers are bound to that interface, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure, what's the advantage here?

Copy link
Contributor

Choose a reason for hiding this comment

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

We could probably rediscuss this also later. Right now I am a bit concerned about the time line as this PR needs to land today for us to go into the release phase.

@mringler mringler force-pushed the feature/untangle_criteria_class branch from 2aa9adc to 4fc2186 Compare August 10, 2021 11:03
@mringler mringler force-pushed the feature/untangle_criteria_class branch 3 times, most recently from 371c49e to 0fd9718 Compare August 10, 2021 11:25
Copy link
Contributor

@gechetspr gechetspr left a comment

Choose a reason for hiding this comment

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

Few minor things. Not sure if they are blocker, but IMO nice to fix

src/Propel/Runtime/ActiveQuery/Criteria.php Show resolved Hide resolved
src/Propel/Runtime/Adapter/Pdo/PdoAdapter.php Show resolved Hide resolved
src/Propel/Runtime/Adapter/Pdo/PdoAdapter.php Outdated Show resolved Hide resolved
@dereuromark
Copy link
Contributor

I see 1-2 important comments, otherwise we can probably continue for the beta release.
Lets see if we can merge this today then.

@mringler mringler force-pushed the feature/untangle_criteria_class branch from 0fd9718 to 313fac4 Compare August 11, 2021 15:43
@mringler mringler force-pushed the feature/untangle_criteria_class branch from 313fac4 to a70d586 Compare August 11, 2021 15:50
@dereuromark
Copy link
Contributor

This one fail

$DB_USER not set. Using 'root'.
/usr/bin/mysql Ver 14.14 Distrib 5.7.35, for Linux (x86_64) using EditLine wrapper

looks unrelated

@dereuromark
Copy link
Contributor

Should this be a squash merge?

@mringler
Copy link
Contributor Author

One of the mysql tests always fails, but I think it is a problem with the pipeline (it's a different one each time)
https://github.com/propelorm/Propel2/pull/1747/checks?check_run_id=3302733292
https://github.com/propelorm/Propel2/runs/3302662191?check_suite_focus=true

@mringler
Copy link
Contributor Author

mringler commented Aug 11, 2021

Should this be a squash merge?

I'd prefer it

@mringler mringler force-pushed the feature/untangle_criteria_class branch from a70d586 to 364ae31 Compare August 11, 2021 16:00
@dereuromark dereuromark merged commit faf29e2 into propelorm:master Aug 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants