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

sql/backwards_extremities: Shift to table format and share code #985

Merged
merged 3 commits into from
Apr 28, 2020

Conversation

kegsay
Copy link
Member

@kegsay kegsay commented Apr 28, 2020

This is an initial cut to reduce boilerplate at the storage layer.
It removes the need for 2x _table.go files, one for each DB engine,
replacing it with a single struct which has an interface which
implements the raw SQL statements.

The actual impl sits alongside the interface declaration which is
generally regarded as best practice (though no canonical sources).
Especially in this case where the impl is tiny (functions returning
strings) and relies heavily on the function signatures of the
table struct (for parameters), having the context in the same file
is useful.


Assuming we're happy with this structure, I'll go ahead and convert the rest of syncapi to use this new format (the mapping is fairly mechanical and boring) as it's annoying to have a mix of both formats in a component.

The main victory will be to share as much of the Database implementation as possible, which will hopefully be possible once the tables have been ported over. Crucially, nothing stops us from breaking out of this structure should the tables/statements be vastly different between the two DB engines. We should only be making files in the tables package if there is genuine overlap.

This is an initial cut to reduce boilerplate at the storage layer.
It removes the need for 2x `_table.go` files, one for each DB engine,
replacing it with a single struct which has an interface which
implements the raw SQL statements.

The actual impl sits alongside the interface declaration which is
generally regarded as best practice (though no canonical sources).
Especially in this case where the impl is tiny (functions returning
strings) and relies heavily on the function signatures of the
table struct (for parameters), having the context in the same file
is useful.
Copy link
Contributor

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

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

Generally I quite like this pattern. What we will need to be particularly careful of is if there are any statements where we give parameters in a different order or if we provide different parameters altogether (because they are commented out, like in the public rooms API).

@kegsay
Copy link
Member Author

kegsay commented Apr 28, 2020

Yeah this is where we will still need different files for different DB engines. That should hopefully be the exception though, not the rule!

@kegsay kegsay merged commit 35b7cbd into master Apr 28, 2020
@kegsay kegsay deleted the kegan/backfill-bwards-extrems-roomserver branch April 28, 2020 14:50
kegsay added a commit that referenced this pull request May 13, 2020
This goes down a different route than #985
which tried to even reduce the boilerplate of `ExecContext` etc. The previous pattern
fails badly when there are subtle differences in parameters and hence the shared
boilerplate to read from `QueryContext` breaks. Rather than attacking it at that level,
the main place where we want to reuse code is for the `syncserver.go` itself - the
database implementation which has lots of complex logic. So instead, this commit:
 - Makes `invites_table.go` an interface.
 - Makes `SyncServerDatasource` use that interface
 - This means some functions are now identical for pq/sqlite, so factor them out
   to a temporary `shared.Database` struct which will grow until it replaces all of
   `SyncServerDatasource`.
kegsay added a commit that referenced this pull request May 13, 2020
* Initial syncapi storage refactor to share pq/sqlite code

This goes down a different route than #985
which tried to even reduce the boilerplate of `ExecContext` etc. The previous pattern
fails badly when there are subtle differences in parameters and hence the shared
boilerplate to read from `QueryContext` breaks. Rather than attacking it at that level,
the main place where we want to reuse code is for the `syncserver.go` itself - the
database implementation which has lots of complex logic. So instead, this commit:
 - Makes `invites_table.go` an interface.
 - Makes `SyncServerDatasource` use that interface
 - This means some functions are now identical for pq/sqlite, so factor them out
   to a temporary `shared.Database` struct which will grow until it replaces all of
   `SyncServerDatasource`.

* Missing files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants