Skip to content
Snippets Groups Projects
Unverified Commit a2f3656b authored by Cal Herries's avatar Cal Herries Committed by GitHub
Browse files

Only copy active fields and tables with data/with-temp-copy-of-db (#39862)

parent ef9fb057
No related branches found
No related tags found
No related merge requests found
......@@ -133,7 +133,8 @@
[database-id :- ::lib.schema.id/database]
(t2/select-fn->pk (juxt (constantly database-id) :name)
[:model/Table :id :name]
:db_id database-id))
:db_id database-id
:active true))
(mu/defn ^:private build-field-lookup-map
[table-id :- ::lib.schema.id/table]
......@@ -169,7 +170,7 @@
(defn- table-id-from-app-db
[db-id table-name]
(t2/select-one-pk [Table :id] :db_id db-id, :name table-name))
(t2/select-one-pk [Table :id] :db_id db-id, :name table-name, :active true))
(defn- throw-unfound-table-error [db-id table-name]
(let [{driver :engine, db-name :name} (t2/select-one [:model/Database :name :engine] :id db-id)]
......@@ -198,7 +199,8 @@
(str (t2/select-one-fn (fn [field]
(qualified-field-name (:parent_id field) (:name field)))
[:model/Field :parent_id :name]
:id parent-id)
:id parent-id
:active true)
\.
field-name)
field-name))
......@@ -244,11 +246,11 @@
(defn- copy-table-fields! [old-table-id new-table-id]
(t2/insert! Field
(for [field (t2/select Field :table_id old-table-id {:order-by [[:id :asc]]})]
(for [field (t2/select Field :table_id old-table-id, :active true, {:order-by [[:id :asc]]})]
(-> field (dissoc :id :fk_target_field_id) (assoc :table_id new-table-id))))
;; now copy the FieldValues as well.
(let [old-field-id->name (t2/select-pk->fn :name Field :table_id old-table-id)
new-field-name->id (t2/select-fn->pk :name Field :table_id new-table-id)
(let [old-field-id->name (t2/select-pk->fn :name Field :table_id old-table-id :active true)
new-field-name->id (t2/select-fn->pk :name Field :table_id new-table-id :active true)
old-field-values (t2/select FieldValues :field_id [:in (set (keys old-field-id->name))])]
(t2/insert! FieldValues
(for [{old-field-id :field_id, :as field-values} old-field-values
......@@ -262,7 +264,7 @@
(update :human_readable_values not-empty))))))
(defn- copy-db-tables! [old-db-id new-db-id]
(let [old-tables (t2/select Table :db_id old-db-id {:order-by [[:id :asc]]})
(let [old-tables (t2/select Table :db_id old-db-id, :active true, {:order-by [[:id :asc]]})
new-table-ids (t2/insert-returning-pks! Table
(for [table old-tables]
(-> table (dissoc :id) (assoc :db_id new-db-id))))]
......@@ -273,8 +275,8 @@
(doseq [{:keys [source-field source-table target-field target-table]}
(mdb.query/query {:select [[:source-field.name :source-field]
[:source-table.name :source-table]
[:target-field.name :target-field]
[:target-table.name :target-table]]
[:target-field.name :target-field]
[:target-table.name :target-table]]
:from [[:metabase_field :source-field]]
:left-join [[:metabase_table :source-table] [:= :source-field.table_id :source-table.id]
[:metabase_field :target-field] [:= :source-field.fk_target_field_id :target-field.id]
......@@ -282,6 +284,10 @@
:where [:and
[:= :source-table.db_id old-db-id]
[:= :target-table.db_id old-db-id]
:source-field.active
:target-field.active
:source-table.active
:target-table.active
[:not= :source-field.fk_target_field_id nil]]})]
(t2/update! Field (the-field-id (the-table-id new-db-id source-table) source-field)
{:fk_target_field_id (the-field-id (the-table-id new-db-id target-table) target-field)})))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment