Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Made POST /api/apps/generate/app accept an existing app's entity_id and collection_id (#84511)

Timofey Kachalovf7505236a32b
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/README.md b/enterprise/backend/src/metabase_enterprise/data_apps/README.md
index 0b5455a521c..76b86cadcfb 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/README.md
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/README.md
@@ -89,8 +89,9 @@ assets (`metabase.server.routes/static-files-handler`).
- `PUT /api/apps/:slug` — update manifest fields or the bundle, or toggle `enabled` (superuser).
- `DELETE /api/apps/:slug` — drop a row, its bundle, and its owned resources (superuser).
- `GET /api/apps/repo-status` — whether a repo is connected (superuser).
-- `POST /api/apps/generate/app` — a new app's `data_app.yaml` and its collection's file, each at its path
- from the repository root, with new entity IDs (`generate/app.clj`; superuser).
+- `POST /api/apps/generate/app` — an app's `data_app.yaml` and its collection's file, each at its path
+ from the repository root, with new entity IDs, or with the `entity_id` and `collection_id` an existing
+ app passes (`generate/app.clj`; superuser).
- `GET /api/apps/generate/schemas` — the TypeScript module an app's definitions are written against: the
root libraries' tables and metrics, and the query actions that belong to no model
(`generate/schemas.clj`; superuser).
@@ -147,7 +148,7 @@ The Data Apps feature is required for all app API endpoints, including group lis
| `resources.clj` | Lifecycle of the app-owned collection and derived collection permissions. |
| `models/data_app.clj` | The `:model/DataApp` Toucan model: hooks, permissions, default fields, serialization. |
| `generate.clj` | The facade over what Metabase generates for an app's author: its files, its schema, its collection's files. |
-| `generate/app.clj` | A new app's `data_app.yaml` and collection file, each at its path with its YAML. |
+| `generate/app.clj` | An app's `data_app.yaml` and collection file, each at its path with its YAML. |
| `generate/resources.clj` | The files of an app's collection: built queries, action copies, metric copies, each with its YAML. |
| `generate/schemas.clj` | The TypeScript schema of the root libraries' tables and metrics and the model-less query actions. |
| `generate/schemas/` | The schema's stages: data access (`source`), entities (`table`, `metric`, `action`, `common`), rendering (`render`, `javascript`). |
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
index c01924a6a7d..f5f9d4f14a4 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
@@ -264,9 +264,10 @@
nil)
(api.macros/defendpoint :post "/generate/app" :- ::data-apps.schema/app-files
- "A new data app's `data_app.yaml` and its collection's file, each at its path from the repository root and with the
- YAML a remote-sync export writes, with new entity IDs. For superusers: the files are written into the app's
- repository, which only an admin works with."
+ "A data app's `data_app.yaml` and its collection's file, each at its path from the repository root and with the
+ YAML a remote-sync export writes. An existing app passes its `entity_id` and `collection_id` to keep them; a new app
+ gets new entity IDs. For superusers: the files are written into the app's repository, which only an admin works
+ with."
[_route-params
_query-params
body :- ::data-apps.schema/app-request]
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/app.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/app.clj
index 8d72cb20072..dbe34a56fba 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/app.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/generate/app.clj
@@ -35,14 +35,15 @@
:yaml (serialization/entity-yaml entity)})
(mu/defn generate :- ::data-apps.schema/app-files
- "The `data_app.yaml` of a new app named `name`, served at `slug` (from the name by default), and its collection's
- file, both with new entity IDs and the YAML an export writes for them once a pull saves them."
- [{app-name :name :keys [slug description]} :- ::data-apps.schema/app-request]
+ "The `data_app.yaml` of an app named `name`, served at `slug` (from the name by default), and its collection's file,
+ with the entity IDs `entity_id` and `collection_id` of an existing app, or new ones, and the YAML an export writes
+ for them once a pull saves them."
+ [{app-name :name :keys [slug description entity_id collection_id]} :- ::data-apps.schema/app-request]
(let [slug (or slug (name->slug app-name))
_ (when-not (mr/validate ::data-apps.schema/slug slug)
(throw (ex-info (tru "Pass a slug: \"{0}\" is not a valid one." slug) {:status-code 400})))
- app-id (u/generate-nano-id)
- collection-id (u/generate-nano-id)
+ app-id (or entity_id (u/generate-nano-id))
+ collection-id (or collection_id (u/generate-nano-id))
created-at (u.date/format (mi/current-timestamp))
collection {:name (str "Data App: " slug)
:namespace "data-apps"
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj b/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj
index 48736427bf3..fd1cc6ef934 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj
@@ -150,14 +150,17 @@
[:error :string]]])
(mr/def ::app-request
- "What a new data app's files are generated from."
+ "What a data app's files are generated from. An existing app passes the entity IDs of its `data_app.yaml` and of its
+ collection, so its files keep them."
[:map {:closed true}
[:name ::display-name]
[:slug {:optional true} [:maybe ::slug]]
- [:description {:optional true} ::description]])
+ [:description {:optional true} ::description]
+ [:entity_id {:optional true} [:maybe ms/NanoIdString]]
+ [:collection_id {:optional true} [:maybe ms/NanoIdString]]])
(mr/def ::app-files
- "A new data app's files, each at its path from the repository root."
+ "A data app's files, each at its path from the repository root."
[:map {:closed true}
[:files [:sequential [:map {:closed true}
[:path :string]
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/app_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/app_test.clj
index 0a78d849ba7..b45bc1774d2 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/app_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/generate/app_test.clj
@@ -50,6 +50,15 @@
(testing "the collection is named after the slug, so apps sharing a display name don't share a collection file"
(is (=? {:name "Data App: ops"} (file-yaml result "collections/data_apps/data_app__ops.yaml"))))))
+(deftest keeps-the-entity-ids-of-an-existing-app-test
+ (let [app-id "existingAppIdxxxxxxxx"
+ collection-id "existingCollIdxxxxxxx"
+ result (generate.app/generate {:name "Sales Ops" :entity_id app-id :collection_id collection-id})]
+ (is (=? {:entity_id app-id :collection collection-id}
+ (file-yaml result "data_apps/sales-ops/data_app.yaml")))
+ (is (=? {:entity_id collection-id}
+ (file-yaml result "collections/data_apps/data_app__sales_ops.yaml")))))
+
(deftest derives-the-slug-from-the-name-test
(are [app-name slug] (some? (file-yaml (generate.app/generate {:name app-name})
(str "data_apps/" slug "/data_app.yaml")))
@@ -74,6 +83,17 @@
(testing "an explicit slug is kept"
(is (=? {:files [{:path "data_apps/ops/data_app.yaml"} {}]}
(mt/user-http-request :crowberto :post 200 "apps/generate/app" {:name "Sales Ops" :slug "ops"}))))
+ (testing "an existing app's entity IDs are kept"
+ (let [{[app collection] :files} (mt/user-http-request :crowberto :post 200 "apps/generate/app"
+ {:name "Sales Ops"
+ :entity_id "existingAppIdxxxxxxxx"
+ :collection_id "existingCollIdxxxxxxx"})]
+ (is (=? {:entity_id "existingAppIdxxxxxxxx" :collection "existingCollIdxxxxxxx"}
+ (yaml/parse-string (:yaml app))))
+ (is (=? {:entity_id "existingCollIdxxxxxxx"}
+ (yaml/parse-string (:yaml collection))))))
+ (testing "an entity ID that is not a NanoID is refused"
+ (mt/user-http-request :crowberto :post 400 "apps/generate/app" {:name "Sales Ops" :entity_id "not-an-id"}))
(testing "only a superuser writes an app's repository"
(is (= "You don't have permissions to do that."
(mt/user-http-request :rasta :post 403 "apps/generate/app" {:name "Sales Ops"}))))