Repository navigation
View table support - #1666
View table support#1666aldychris wants to merge 7 commits into
Conversation
# Conflicts: # examples/chat/chat_server/lib/src/generated/protocol.dart # modules/serverpod_auth/serverpod_auth_server/lib/src/generated/protocol.dart # modules/serverpod_chat/serverpod_chat_server/lib/src/generated/protocol.dart # packages/serverpod/lib/src/generated/protocol.dart # tests/serverpod_test_client/lib/src/protocol/protocol.dart # tests/serverpod_test_server/generated/tables.pgsql # tests/serverpod_test_server/lib/src/generated/protocol.dart # tools/serverpod_cli/lib/src/analyzer/entities/definitions.dart # tools/serverpod_cli/lib/src/database/create_definition.dart # tools/serverpod_cli/lib/src/generator/dart/library_generators/library_generator.dart # tools/serverpod_cli/lib/src/generator/psql/legacy_pgsql_generator.dart
SandPod
left a comment
There was a problem hiding this comment.
Thanks for taking this on, this is probably going to be quite a big feature connecting many different systems together. I would expect this to result in many rounds of feedback before we have it in it's desired state.
Regarding the approach it looks like you are on the right track.
If I understand your progress so far there is no way to actually query the view tables yet. I would recommend reaching a state where the views are definable in migrations and queryable before the next review. There are probably going to be obstacles there that might change assumptions made earlier on.
Have a couple of comments around the progress so far.
Select Query Builder: Is the query builder required here? Since the query is specified by the user directly we shouldn't need it right?
Materialized views: There is a different type of view called Materialized views that allows indexes to be created on them. Should we support that as well? I think we should at least create an architecture that allows us to add them in the future.
Tests: Tests needs to include all changes in the analyzer. We expect a unit test that covers each validation case and a validation that parsing is successful if all rules are followed.
| ) | ||
| ], | ||
| managed: true, | ||
| viewTable: false, |
There was a problem hiding this comment.
Suggestion: Use a geneal type and add enums for table types.
A simple bool won't scale if we add more types of tables in the future.
If we supported inheritance in our models it would have been great to introduce a new object called ViewTableDefintion used for view tables.
| ### Indicates if the table is view object. | ||
| viewTable: bool? |
There was a problem hiding this comment.
Suggestion: Change to an enum informing what type of table definition this is.
| Map<String, Map<String, dynamic>> rawRow, | ||
| Map<String, Map<int, List<dynamic>>> resolvedListRelations, { | ||
| Include? include, | ||
| bool viewTable = false, |
There was a problem hiding this comment.
Question 1: How does this work with the recursive call on line 47? It looks like we need to assert that include and view table are never used together right?
Question 2: If we don't want the recursive calls, would it make sense to have a specific inplementation for resolvedPrefixedQueryRow that only operates on viewTables and shares code with this method.
Question 3: Could we encode this information directly in the table instead of having to pass it in as separate variable? Maybe a specific type table type could be used for view tables?
There was a problem hiding this comment.
I think there is no need to process the relation table for viewTable since it is already defined in the raw query inside the protocol. I will think to have specific implementation for this method.
| bool viewTable = false, | ||
| }) { | ||
| assert(!viewTable || (viewTable && rawRow.containsKey('')), | ||
| 'Invalid rawRow for viewTable=true'); |
There was a problem hiding this comment.
Suggestion: Chage to a throw.
Asserts only work in debug environments so this method would be run if the rawRow had the wrong format in a production environment.
Also, I think this error message needs to be inproved to help any future developer understand what has gone wrong.
| String? join; | ||
| if (!_viewTable) { | ||
| join = _buildJoinQuery( | ||
| where: _where, | ||
| manyRelationWhereAddition: _manyRelationWhereAddition, | ||
| having: _having, | ||
| orderBy: _orderBy, | ||
| include: _include, | ||
| subQueries: subQueries, | ||
| countTableRelation: _countTableRelation, | ||
| joinOneLevelManyRelations: _joinOneLevelManyRelationWhereExpressions, | ||
| ); | ||
| } |
There was a problem hiding this comment.
Suggestion: Add a throw if a join is attempted while a view table is used.
This will silently not join a table if the view table property is set to true. That probably won't yield the result the developer is after and it is better to inform them that this operation is not supported.
| if (classDefinition.viewName != null) { | ||
| return []; | ||
| } |
There was a problem hiding this comment.
Suggestion: add a validation rule that informs the user if an index is specified for a view.
Since we ignore any indexes specified for views (correctly) we should inform the developer that the view model is not correct.
| } | ||
|
|
||
| Field _buildRepositoryField( | ||
| String className, String fieldName, String assignment) { |
There was a problem hiding this comment.
NIT: add comma so formatting looks good in 80 column width settings.
| ]); | ||
|
|
||
| if (serverCode && tableName != null) { | ||
| if (serverCode && (tableName != null || viewName != null)) { |
There was a problem hiding this comment.
Suggestion: Break out this check to a method with a fitting name and reference.
The check inside (...) is reflected in multiple places in the code, better to break it out so that it has a shared definition.
| var selectPattern = RegExp(r'\bselect\b', caseSensitive: false); | ||
| var fromPattern = RegExp(r'\bfrom\b', caseSensitive: false); |
There was a problem hiding this comment.
Question: The column names used in the query need to match the fields of the model for our deserialization to work correctly. Could we add validation for that?
There was a problem hiding this comment.
Yes, let me add validation to match the field with the query that the user defined in Yaml.
| timeoutInSeconds: 60, | ||
| transaction: transaction, | ||
| include: include, | ||
| viewTable: viewTable, |
There was a problem hiding this comment.
Question: Shouldn't the query be replaced here?
There was a problem hiding this comment.
Question: Shouldn't the query be replaced here?
Sorry, I need some clarification, do you mean querying on line 72 should replace?
There was a problem hiding this comment.
Thanks for taking this on, this is probably going to be quite a big feature connecting many different systems together. I would expect this to result in many rounds of feedback before we have it in it's desired state.
Regarding the approach it looks like you are on the right track.
If I understand your progress so far there is no way to actually query the view tables yet. I would recommend reaching a state where the views are definable in migrations and queryable before the next review. There are probably going to be obstacles there that might change assumptions made earlier on.
Have a couple of comments around the progress so far.
Select Query Builder: Is the query builder required here? Since the query is specified by the user directly we shouldn't need it right?
Materialized views: There is a different type of view called Materialized views that allows indexes to be created on them. Should we support that as well? I think we should at least create an architecture that allows us to add them in the future.
Tests: Tests needs to include all changes in the analyzer. We expect a unit test that covers each validation case and a validation that parsing is successful if all rules are followed.
CREATE or REPLACE: Note that views can only be replaced if the same columns are represented. If any column is removed or its location is moved REPLACE is not supported, source: https://www.postgresql.org/docs/current/sql-createview.html
|
Closing this as stale but will reference the implementation in the issue: #228 |
Basic functionality for view table abilities for serverpod, based on discussion on #932 and issues on #228
Missing
Pre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.