Skip to content

Commit

Permalink
Move existing archived items to the trash (#42241)
Browse files Browse the repository at this point in the history
Move everything that's archived directly to the trash. It's not ideal
because we're wiping out the existing collection structure, but the
existing Archive page also has no collection structure.
  • Loading branch information
johnswanson authored and sloansparger committed May 9, 2024
1 parent a03f07f commit 7b0808c
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
104 changes: 104 additions & 0 deletions resources/migrations/001_update_migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7038,6 +7038,110 @@ databaseChangeLog:
);
DELETE FROM collection WHERE type = 'trash';
- changeSet:
id: v50.2024-05-09T15:55:06
author: johnswanson
comment: Move existing archived collections to the Trash - (Postgres)
preConditions:
- onFail: MARK_RAN
- dbms:
type: postgresql
changes:
- sql:
sql: >-
UPDATE collection AS c1
SET trashed_from_location = c1.location,
location = CONCAT('/', c2.id, '/')
FROM (SELECT id FROM collection WHERE type = 'trash') AS c2
WHERE c1.archived = true AND c1.namespace IS NULL;
rollback:
- sql:
sql: >-
UPDATE collection
SET trashed_from_location = NULL, location = trashed_from_location
WHERE archived = true AND namespace IS NULL;
- changeSet:
id: v50.2024-05-09T16:16:24
author: johnswanson
comment: Move existing archived collections to the Trash - (H2)
preConditions:
- onFail: MARK_RAN
- dbms:
type: h2
changes:
- sql:
sql: >-
UPDATE collection AS c1
SET trashed_from_location = c1.location,
location = CONCAT('/', (SELECT id FROM collection WHERE type = 'trash'), '/')
WHERE c1.archived = true AND c1.namespace IS NULL;
rollback:
- sql:
sql: >-
UPDATE collection
SET trashed_from_location = NULL, location = trashed_from_location
WHERE archived = true AND namespace IS NULL;
- changeSet:
id: v50.2024-05-09T16:16:31
author: johnswanson
comment: Move existing archived collections to the Trash - (MySQL/MariaDB)
preConditions:
- onFail: MARK_RAN
- dbms:
type: mysql,mariadb
changes:
- sql:
sql: >-
UPDATE collection AS c1
JOIN (
SELECT id FROM collection WHERE type = 'trash'
) AS c2
SET c1.trashed_from_location = c1.location,
c1.location = CONCAT('/', c2.id, '/')
WHERE c1.archived = true AND c1.namespace IS NULL;
rollback:
- sql:
sql: >-
UPDATE collection
SET trashed_from_location = NULL, location = trashed_from_location
WHERE archived = true AND namespace IS NULL;
- changeSet:
id: v50.2024-05-09T16:16:38
author: johnswanson
comment: Move existing archived dashboards to the Trash
changes:
- sql:
sql: >-
UPDATE report_dashboard
SET trashed_from_collection_id = collection_id, collection_id = (SELECT id FROM collection WHERE type = 'trash')
WHERE archived = true;
rollback:
- sql:
sql: >-
UPDATE report_dashboard
SET trashed_from_collection_id = NULL, collection_id = trashed_from_collection_id
WHERE archived = true;
- changeSet:
id: v50.2024-05-09T16:16:42
author: johnswanson
comment: Move existing archived cards to the Trash
changes:
- sql:
sql: >-
UPDATE report_card
SET trashed_from_collection_id = collection_id, collection_id = (SELECT id FROM collection WHERE type = 'trash')
WHERE archived = true;
rollback:
- sql:
sql: >-
UPDATE report_card
SET trashed_from_collection_id = NULL, collection_id = trashed_from_collection_id
WHERE archived = true;
# >>>>>>>>>> DO NOT ADD NEW MIGRATIONS BELOW THIS LINE! ADD THEM ABOVE <<<<<<<<<<

########################################################################################################################
Expand Down
39 changes: 39 additions & 0 deletions test/metabase/db/schema_migrations_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
(:require
[cheshire.core :as json]
[clojure.java.jdbc :as jdbc]
[clojure.set :as set]
[clojure.test :refer :all]
[java-time.api :as t]
[metabase.config :as config]
Expand All @@ -30,6 +31,7 @@
PermissionsGroup
Table
User]]
[metabase.models.collection :as collection]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[toucan2.core :as t2]))
Expand Down Expand Up @@ -2051,3 +2053,40 @@
(is (= 1 (t2/select-one-fn :view_count :metabase_table table-id)))
(is (= 1 (t2/select-one-fn :view_count :report_card card-id)))
(is (= 2 (t2/select-one-fn :view_count :report_dashboard dash-id)))))))

(deftest move-to-trash-test
(testing "existing archived items should be moved to the Trash"
(impl/test-migrations ["v50.2024-05-09T15:55:06" "v50.2024-05-09T16:16:42"] [migrate!]
(let [user (create-raw-user! (mt/random-email))
db (t2/insert-returning-pk! :metabase_database (-> (mt/with-temp-defaults Database)
(update :details json/generate-string)
(update :engine str)))
dash (t2/insert-returning-pk! (t2/table-name :model/Dashboard)
{:name "A dashboard"
:archived true
:creator_id (:id user)
:created_at :%now
:updated_at :%now
:parameters ""})
card (t2/insert-returning-pk! (t2/table-name Card)
{:name "Card"
:archived true
:display "table"
:dataset_query "{}"
:visualization_settings "{}"
:cache_ttl 30
:creator_id (:id user)
:database_id db
:created_at :%now
:updated_at :%now})
collection (t2/insert-returning-pk! (t2/table-name Collection)
{:name "Silly Collection"
:slug "silly-collection"
:archived true})]
(is (empty? (t2/select-fn-set :id :model/Dashboard :collection_id (collection/trash-collection-id))))
(is (empty? (t2/select-fn-set :id :model/Card :collection_id (collection/trash-collection-id))))
(is (empty? (t2/select-fn-set :id :model/Collection :location (collection/children-location (collection/trash-collection)))))
(migrate!)
(is (set/subset? #{dash} (t2/select-fn-set :id :model/Dashboard :collection_id (collection/trash-collection-id))))
(is (set/subset? #{card} (t2/select-fn-set :id :model/Card :collection_id (collection/trash-collection-id))))
(is (set/subset? #{collection} (t2/select-fn-set :id :model/Collection :location (collection/children-location (collection/trash-collection)))))))))

0 comments on commit 7b0808c

Please sign in to comment.