Skip to content
Snippets Groups Projects
Commit 6606d3bc authored by Allen Gilliland's avatar Allen Gilliland
Browse files

add new migration which adds :database_id, :table_id, and :query_type as...

add new migration which adds :database_id, :table_id, and :query_type as direct columns on the Card entity.
parent 196ba061
Branches
Tags
No related merge requests found
databaseChangeLog:
- changeSet:
id: 12
author: agilliland
changes:
- addColumn:
tableName: report_card
columns:
- column:
name: database_id
type: int
constraints:
nullable: true
references: metabase_database(id)
foreignKeyName: fk_report_card_ref_database_id
deferrable: false
initiallyDeferred: false
- column:
name: table_id
type: int
constraints:
nullable: true
references: metabase_table(id)
foreignKeyName: fk_report_card_ref_table_id
deferrable: false
initiallyDeferred: false
- column:
name: query_type
type: varchar(16)
constraints:
nullable: true
......@@ -9,6 +9,7 @@
{"include": {"file": "migrations/008_add_display_name_columns.json"}},
{"include": {"file": "migrations/009_add_table_visibility_type_column.json"}},
{"include": {"file": "migrations/010_add_revision_table.yaml"}},
{"include": {"file": "migrations/011_cleanup_dashboard_perms.yaml"}}
{"include": {"file": "migrations/011_cleanup_dashboard_perms.yaml"}},
{"include": {"file": "migrations/012_add_card_query_fields.yaml"}}
]
}
......@@ -119,7 +119,11 @@
(log/info "Database Migrations Current ... CHECK")
;; Establish our 'default' Korma DB Connection
(kdb/default-connection (kdb/create-db @jdbc-connection-details)))
(kdb/default-connection (kdb/create-db @jdbc-connection-details))
;; Do any custom code-based migrations now that the db structure is up to date
;; NOTE: we use dynamic resolution to prevent circular dependencies
((u/runtime-resolved-fn 'metabase.db.migrations 'run-all)))
(defn setup-db-if-needed [& args]
(when-not @setup-db-has-been-called?
......
(ns metabase.db.migrations
(:require [clojure.tools.logging :as log]
[korma.core :as k]
[metabase.db :as db]
[metabase.models.card :refer [Card]]))
(defn- set-card-database-and-table-ids
"Upgrade for the `Card` model when `:database_id` and `:table_id` were added and needed populating.
This reads through all saved cards, extracts the JSON from the `:dataset_query`, and tries to populate
the values for `:database_id` and `:table_id` if possible."
[]
;; only execute when `:database_id` column on all cards is `nil`
(when (= 0 (:cnt (first (k/select Card (k/aggregate (count :*) :cnt) (k/where (not= :database_id nil))))))
(log/info "Data migration: Setting database/table/type fields on all Cards.")
(doseq [{id :id {{table :source_table} :query :keys [database type]} :dataset_query} (db/sel :many [Card :id :dataset_query])]
(when type
(db/upd Card id
:query_type type
:database_id database
:table_id table)))))
(defn run-all
"Run all coded data migrations."
[]
;; Append to the bottom of this list so that these run in chronological order
(set-card-database-and-table-ids))
(ns metabase.models.card
(:require [korma.core :refer :all, :exclude [defentity update]]
[metabase.api.common :refer [*current-user-id*]]
[metabase.db :refer :all]
(metabase.models [interface :refer :all]
[user :refer [User]])))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment