Skip to content

View table support - #1666

Closed
aldychris wants to merge 7 commits into
serverpod:mainfrom
aldychris:feature/view-table
Closed

aldychris wants to merge 7 commits into
serverpod:mainfrom
aldychris:feature/view-table

Conversation

@aldychris

Copy link
Copy Markdown
Contributor

Basic functionality for view table abilities for serverpod, based on discussion on #932 and issues on #228

Missing

  • More comprehensive unit test
  • View Table on DB Migration

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

# 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 SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +24 to +25
### Indicates if the table is view object.
viewTable: bool?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +71 to +83
String? join;
if (!_viewTable) {
join = _buildJoinQuery(
where: _where,
manyRelationWhereAddition: _manyRelationWhereAddition,
having: _having,
orderBy: _orderBy,
include: _include,
subQueries: subQueries,
countTableRelation: _countTableRelation,
joinOneLevelManyRelations: _joinOneLevelManyRelationWhereExpressions,
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +79 to +81
if (classDefinition.viewName != null) {
return [];
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

NIT: add comma so formatting looks good in 80 column width settings.

]);

if (serverCode && tableName != null) {
if (serverCode && (tableName != null || viewName != null)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +483 to +484
var selectPattern = RegExp(r'\bselect\b', caseSensitive: false);
var fromPattern = RegExp(r'\bfrom\b', caseSensitive: false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Question: Shouldn't the query be replaced here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Question: Shouldn't the query be replaced here?

Sorry, I need some clarification, do you mean querying on line 72 should replace?

@SandPod SandPod left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

@SandPod

SandPod commented Jun 25, 2025

Copy link
Copy Markdown
Contributor

Closing this as stale but will reference the implementation in the issue: #228

@SandPod SandPod closed this Jun 25, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants