Skip to content
Snippets Groups Projects
Commit 488473aa authored by Cam Saul's avatar Cam Saul
Browse files

OMG THINGS WORK <3

parent b97f6f13
No related branches found
No related tags found
No related merge requests found
(ns metabase.driver.generic-sql.query-processor.annotate
"Functions related to annotating results returned by the Query Processor."
(:require [clojure.core.memoize :as memo]
[metabase.db :refer :all]
(:require [metabase.db :refer :all]
[metabase.models.field :refer [Field field->fk-table]]))
(declare get-column-names
......@@ -55,14 +54,6 @@
(->> (concat breakout-fields other-fields)
(map :castified))))
(def ^:private ^{:arglists '([table-id])} table-id->field-positions
"Return Field information for ordering results for a Table.
This is info is cached for 30 seconds to avoid slamming the DB when doing things like syncing
(which now relies heavily on the QP)."
(memo/ttl (fn [table-id]
(sel :many :fields [Field :id :name :position] :table_id table-id))
:ttl/threshold (* 30 1000)))
(defn- order-columns
"Return CASTIFIED-FIELD-NAMES in the order we'd like to display them in the output.
They should be ordered as follows:
......@@ -70,8 +61,8 @@
1. All breakout fields, in the same order as BREAKOUT-FIELD-IDS
2. Any aggregate fields like `count`
3. All other columns in the same order as `Field.position`."
[{{source-table-id :source_table breakout-field-ids :breakout} :query} castified-field-names]
(-order-columns (table-id->field-positions source-table-id)
[{{source-table :source_table breakout-field-ids :breakout} :query} castified-field-names]
(-order-columns (sel :many :fields [Field :id :name :position] :table_id source-table)
(filter identity breakout-field-ids) ; handle empty breakout clauses like [nil]
castified-field-names))
......
......@@ -25,7 +25,8 @@
This does a little bit of smart caching (for 60 seconds) to avoid creating new connections when unneeded."
(let [-db->korma-db (memo/ttl (fn [database]
(log/debug (color/red "Creating a new DB connection..."))
(kdb/create-db (db->connection-spec database)))
(assoc (kdb/create-db (db->connection-spec database))
:make-pool? true))
:ttl/threshold (* 60 1000))]
;; only :engine and :details are needed for driver/connection so just pass those so memoization works as expected
(fn [database]
......
......@@ -199,14 +199,11 @@
(when-let [dest-table-id (table-name->id dest-table-name)]
(when-let [dest-column-id (sel :one :id Field :table_id dest-table-id :name dest-column-name)]
(log/info (format "Marking foreign key '%s.%s' -> '%s.%s'." (:name table) fk-column-name dest-table-name dest-column-name))
(log/info "<1>")
(ins ForeignKey
:origin_id fk-column-id
:destination_id dest-column-id
:relationship (determine-fk-type {:id fk-column-id, :table (delay table)})) ; fake a Field instance
(log/info "<2>")
(upd Field fk-column-id :special_type :fk)
(log/info "<3>"))))))))))
(upd Field fk-column-id :special_type :fk))))))))))
;; ### 4) sync-table-fields-metadata!
......
......@@ -119,8 +119,7 @@
;; if base_type or special_type were affected then we should asynchronously create corresponding FieldValues objects if need be
(when (or (contains? field :base_type)
(contains? field :special_type))
(future
(create-field-values-if-needed (sel :one [Field :id :table_id :base_type :special_type] :id id)))))
(future (create-field-values-if-needed (sel :one [Field :id :table_id :base_type :special_type] :id id)))))
(defmethod pre-cascade-delete Field [_ {:keys [id]}]
(cascade-delete ForeignKey (where (or (= :origin_id id)
......
......@@ -31,12 +31,12 @@
;; ACTIVE-COLUMN-NAMES->TYPE
(expect
#{{:type_name "INTEGER", :column_name "CATEGORY_ID"}
{:type_name "DOUBLE", :column_name "LONGITUDE"}
{:type_name "INTEGER", :column_name "PRICE"}
{:type_name "BIGINT", :column_name "ID"}
{:type_name "VARCHAR", :column_name "NAME"}
{:type_name "DOUBLE", :column_name "LATITUDE"}}
{"NAME" :TextField
"LATITUDE" :FloatField
"LONGITUDE" :FloatField
"PRICE" :IntegerField
"CATEGORY_ID" :IntegerField
"ID" :BigIntegerField}
(i/active-column-names->type h2/driver @venues-table))
......
......@@ -84,9 +84,9 @@
;; ## Tests for DETERMINE-FK-TYPE
;; Since COUNT(category_id) > COUNT(DISTINCT(category_id)) the FK relationship should be Mt1
(expect :Mt1
(sync/determine-fk-type (korma-entity (sel :one Table :id (table->id :venues))) "CATEGORY_ID"))
(sync/determine-fk-type (sel :one Field :id (field->id :venues :category_id))))
;; Since COUNT(id) == COUNT(DISTINCT(id)) the FK relationship should be 1t1
;; (yes, ID isn't really a FK field, but determine-fk-type doesn't need to know that)
(expect :1t1
(sync/determine-fk-type (korma-entity (sel :one Table :id (table->id :venues))) "ID"))
(sync/determine-fk-type (sel :one Field :id (field->id :venues :id))))
......@@ -54,7 +54,7 @@
(log/info "Adding foreign key constraints...")
(add-foreign-key-constraints!)
(log/info "Syncing database...")
(driver/sync-database! db)
(time (driver/sync-database! db))
(log/info "Adding Schema Metadata...")
(add-metadata!)
(log/info "Finished. Enjoy your test data <3")
......
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