Skip to content
Snippets Groups Projects
Commit a00022de authored by Cam Saül's avatar Cam Saül Committed by Cam Saül
Browse files

Fix merge error :cry:

parent 51e5e724
No related branches found
No related tags found
No related merge requests found
......@@ -45,7 +45,7 @@
(defn- pre-cascade-delete [{:keys [id]}]
(db/cascade-delete 'PulseCard :card_id id)
(db/cascade-delete 'Revision :model "Card" :model_id id)
(db/cascade-delete 'Revision :model "Card", :model_id id)
(db/cascade-delete 'DashboardCardSeries :card_id id)
(db/cascade-delete 'DashboardCard :card_id id)
(db/cascade-delete 'CardFavorite :card_id id)
......
......@@ -220,6 +220,6 @@
(log/error "Unexpected error analyzing table" t))
(finally
(u/prog1 (swap! finished-tables-count inc)
(log/info (u/format-color 'blue "%s Analyzed table '%s'." (u/emoji-progress-bar <> tables-count) table-name))))))
(log/info (u/format-color 'blue "%s Analyzed table '%s'." (u/emoji-progress-bar <> tables-count) table-name)))))))
(log/info (u/format-color 'blue "Analysis of %s database '%s' completed." (name driver) (:name database))))
......@@ -7,8 +7,8 @@
[schema.core :as schema]
(metabase [db :as db]
[driver :as driver])
(metabase.models [raw-column :as raw-column]
[raw-table :as raw-table])
(metabase.models [raw-column :refer [RawColumn]]
[raw-table :refer [RawTable]])
[metabase.sync-database.interface :as i]
[metabase.util :as u]))
......@@ -25,7 +25,7 @@
{:pre [(integer? table-id) (integer? database-id)]}
(kdb/transaction
;; start by simply resetting all fks and then we'll add them back as defined
(k/update raw-column/RawColumn
(k/update RawColumn
(k/where {:raw_table_id table-id})
(k/set-fields {:fk_target_column_id nil}))
......@@ -35,7 +35,7 @@
(when-let [dest-table-id (db/sel :one :id RawTable, :database_id database-id, :schema (:schema dest-table), :name (:name dest-table))]
(when-let [dest-column-id (db/sel :one :id RawColumn, :raw_table_id dest-table-id, :name dest-column-name)]
(log/debug (u/format-color 'cyan "Marking foreign key '%s.%s' -> '%s.%s'." (named-table table) fk-column-name (named-table dest-table) dest-column-name))
(db/upd raw-column/RawColumn source-column-id
(db/upd RawColumn source-column-id
:fk_target_column_id dest-column-id)))))))
(defn- save-all-table-columns!
......@@ -44,14 +44,14 @@
[{:keys [id]} columns]
{:pre [(integer? id) (coll? columns) (every? map? columns)]}
(kdb/transaction
(let [existing-columns (into {} (for [{:keys [name] :as column} (db/sel :many :fields [raw-column/RawColumn :id :name] :raw_table_id id)]
(let [existing-columns (into {} (for [{:keys [name] :as column} (db/sel :many :fields [RawColumn :id :name] :raw_table_id id)]
{name column}))]
;; deactivate any columns which were removed
(doseq [[column-name {column-id :id}] (sort-by :name existing-columns)]
(when-not (some #(= column-name (:name %)) columns)
(log/debug (u/format-color 'cyan "Marked column %s as inactive." column-name))
(db/upd raw-column/RawColumn column-id :active false)))
(db/upd RawColumn column-id :active false)))
;; insert or update the remaining columns
(doseq [{column-name :name, :keys [base-type pk? special-type details]} (sort-by :name columns)]
......@@ -61,13 +61,13 @@
is_pk (true? pk?)]
(if-let [{column-id :id} (get existing-columns column-name)]
;; column already exists, update it
(db/upd raw-column/RawColumn column-id
(db/upd RawColumn column-id
:name column-name
:is_pk is_pk
:details details
:active true)
;; must be a new column, insert it
(db/ins raw-column/RawColumn
(db/ins RawColumn
:raw_table_id id
:name column-name
:is_pk is_pk
......@@ -79,7 +79,7 @@
[database-id {table-name :name, table-schema :schema, :keys [details fields]}]
{:pre [(integer? database-id) (string? table-name)]}
(log/debug (u/format-color 'cyan "Found new table: %s" (named-table table-schema table-name)))
(let [table (db/ins raw-table/RawTable
(let [table (db/ins RawTable
:database_id database-id
:schema table-schema
:name table-name
......@@ -93,7 +93,7 @@
[{table-id :id, :as table} {:keys [details fields]}]
;; NOTE: the schema+name of a table makes up the natural key and cannot be modified on update
;; if they were to be different we'd simply assume that's a new table instead
(db/upd raw-table/RawTable table-id
(db/upd RawTable table-id
:details (or details {})
:active true)
;; save columns
......@@ -106,11 +106,11 @@
(let [table-ids (filter identity table-ids)]
(kdb/transaction
;; disable the tables
(k/update raw-table/RawTable
(k/update RawTable
(k/where {:id [in table-ids]})
(k/set-fields {:active false}))
;; whenever a table is disabled we need to disable all of its fields too (and remove fk references)
(k/update raw-column/RawColumn
(k/update RawColumn
(k/where {:raw_table_id [in table-ids]})
(k/set-fields {:active false, :fk_target_column_id nil})))))
......@@ -150,7 +150,7 @@
(schema/validate i/DescribeDatabase <>))
;; This is a protection against cases where the returned table-def has no :schema key
tables (map #(update % :schema identity) tables)
existing-tables (into {} (for [{:keys [name schema] :as table} (db/sel :many :fields [raw-table/RawTable :id :schema :name] :database_id database-id)]
existing-tables (into {} (for [{:keys [name schema] :as table} (db/sel :many :fields [RawTable :id :schema :name] :database_id database-id)]
{{:name name, :schema schema} table}))]
;; introspect each table and save off the schema details we find
......@@ -187,7 +187,7 @@
(try
(when-let [table-fks (u/prog1 (driver/describe-table-fks driver database table-def)
(schema/validate i/DescribeTableFKs <>))]
(when-let [raw-tbl (db/sel :one raw-table/RawTable :database_id database-id, :schema table-schema, :name table-name)]
(when-let [raw-tbl (db/sel :one RawTable, :database_id database-id, :schema table-schema, :name table-name)]
(save-all-table-fks! raw-tbl table-fks)))
(catch Throwable t
(log/error (u/format-color 'red "Unexpected error introspecting table fks: %s" (named-table table-schema table-name)) t)))))
......
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