Skip to content
Snippets Groups Projects
Unverified Commit 0409161d authored by Walter Leibbrandt's avatar Walter Leibbrandt Committed by GitHub
Browse files

Native query snippets (#11965)


* Support substituting SQL snippets in queries

* Rename SQL snippets to native query snippets

* Add migration and model for native query snippet table

* Add API endpoints for native query snippets CRUD

Tests are incomplete. Consequently the API is not well tested.

* Cosmetic: Docstring typo and indentation

* Remove pre-delete handler that prevents deletion

It breaks in cases where deletion is reasonable, like during tests. No
DELETE API endpoint will be added, so no it shouldn't be necessary to
add any additional safeguards.

* Fix `:database_id` field reference

And remove unused import.

* Test snippets listing via API

* Add more API test stubs

* Refactor snippets listing endpoint for improved readability

* Align NativeQuerySnippet model behavior with Segment model's

* Clean ns

* Fix and test individual snippet read via API

* Fix snippet name schema

* Test creation of snippets via API

* Test update API endpoint for native query snippets

* Handle query snippet model in H2 dump/load commands

* Port load-from-h2 tests to clojure.test

* Deduplicate models list used to dump/load H2 dbs

The failing load-from-h2 test that checks that all models are accounted
for alerted me that I need to handle the new `NativeQuerySnippet` model.
However, there was no such test for dump-to-h2. Instead of adding such a
test, I opted to simply use load-from-h2's list in dump-to-h2 to
eliminate the problem, rather than testing for it.

* Dump/load collections before models that depend on it

* Linter appeasement

* Use snippet ID and content in query parameter record

* Implement snippet lookup for template tags with ID

* Basic FE support for snippet template tags

* Use shorter "snippet" prefix instead of "native-query-snippet"

* Use simpler `tru` instead of `str` with `deferred-tru`

* Allow whitespace between snippet prefix and name

Also accept any sequence of characters after prefix.

* Don't allow snippet names to start with whitespace

Whitespace is allowed between the `snippet:` template tag prefix and the
snippet name. That whitespace is trimmed out when extracting the snippet
name.

* Add database ID to snippet template tags before expanding parameters

* Ensure that snippet db matches query db

* Avoid setting `:template-tags` for native queries without it

* Factor out snippet database ID validation

* Test snippet database ID validation

* Avoid validating nil snippets

* First pass at nested template tags in snippets

* Pass parent query parameters on to nested sub-queries

* Remove unused let-binding

* Added disabled test for reading query param from template tag

* Temporarily disable failing test

* Remove half baked nested snippet support

* Snippet model perms = native query perms

* Rename param k→ks for clarity

* Remove superfluous `some?` call

* Use utility functions to reduce cruft

* Test expansion of multiple snippets in (sub-)query

* Rather use testing utils from metabase.test

* Check for snippet changes only in provided fields

* Deduplicate field checks, with added test case output

* remove unused import from bad merge

Co-authored-by: default avatarPaul Rosenzweig <paul.a.rosenzweig@gmail.com>
Co-authored-by: default avatarPaul Rosenzweig <paulrosenzweig@users.noreply.github.com>
parent 1f3a7680
No related branches found
No related tags found
No related merge requests found
Showing
with 653 additions and 236 deletions
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment