Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
I am Cam

Cam Saul camsaul

💭
I am Cam
View GitHub Profile
@camsaul
camsaul / custom_methods.clj
Last active Jul 15, 2022
≈ example tests
View custom_methods.clj
(deftest custom-unify-methods
(is ( {[String java.time.temporal.Temporal]
(fn [_next-method expected actual]
(let [actual-str (u.date/format actual)]
(when-not (= expected actual-str)
(list 'not= expected (symbol "#t") actual-str))))}
"2022-07-14"
#t "2022-07-14"))
(is ( {[String String]
@camsaul
camsaul / x.clj
Created Jun 3, 2022
Metabase Query Processor Demo Stuff
View x.clj
(ns metabase.x
(:require [clojure.test :refer :all]
dev.debug-qp
[metabase.models.database :refer [Database]]
[metabase.query-processor :as qp]
[metabase.test :as mt]
[metabase.query-processor.context :as qp.context]
[metabase.query-processor.context.default :as context.default]
[clojure.pprint :as pprint]
[metabase.driver.sql.query-processor :as sql.qp]
@camsaul
camsaul / defenterprise.clj
Last active Apr 20, 2022
defenterprise macro PoC
View defenterprise.clj
(ns metabase.public-settings.premium-features.defenterprise
(:require [clojure.string :as str]
[metabase.plugins.classloader :as classloader]
[metabase.public-settings.premium-features :as premium-features]
[metabase.util :as u]))
;;; Map of (qualified) function name -> {:oss <oss/fallback fn>
;;; :ee <ee fn>}
(defonce ^:private registry
(atom {}))
View asm.clj
(ns asm
(:import liquibase.database.core.H2Database
liquibase.database.jvm.JdbcConnection
[org.objectweb.asm Opcodes ClassWriter Type]
[org.objectweb.asm.commons Method GeneratorAdapter]))
(defn x ^ClassWriter []
(let [flags 0
classwriter (org.objectweb.asm.ClassWriter. flags)]
;; define the Class
@camsaul
camsaul / h2.clj
Created Feb 18, 2022
Custom Liquibase Table SQL Generator in Clojure
View h2.clj
(ns metabase.db.liquibase.h2
"Loading this namespace will replace the default `H2Database` implementation with our custom subclass that uppercases
all identifiers."
(:require [metabase.util :as u]
[clojure.string :as str])
(:import liquibase.database.DatabaseFactory
liquibase.database.jvm.JdbcConnection
liquibase.change.ChangeMetaData
liquibase.change.core.CreateTableChange
liquibase.change.ChangeFactory
@camsaul
camsaul / sparksql_connection_test.clj
Created Feb 16, 2022
Tree Shaking the Hive JDBC Driver
View sparksql_connection_test.clj
(ns metabase.driver.sparksql-connection-test
(:require [clojure.java.jdbc :as jdbc]
[clojure.test :refer :all]))
(deftest connection-test
(println (pr-str (Class/forName "org.apache.hive.jdbc.HiveConnection")))
(is (some? (Class/forName "org.apache.hive.jdbc.HiveConnection")))
(println (pr-str (require 'metabase.driver.hive-like.fixed-hive-connection)))
(is (nil? (require 'metabase.driver.hive-like.fixed-hive-connection)))
@camsaul
camsaul / perms.md
Created Feb 10, 2022
MB Download Perms
View perms.md
User has full perms e.g. /download/db/1/ User has perms for all MBQL queries e.g. /download/db/1/schema/ User has perms for a specific Table e.g. /download/db/1/schema/public/table/1/
User can download native results? (/download/db/1/native) yes no no
User can download results for Table 1 (/download/db/1/schema/public/table/1)? yes yes yes
User can download results for Table 2 (/download/db/1/schema/public/table/2)? yes yes no
@camsaul
camsaul / reify_all.clj
Created Jan 27, 2022
Reify all/sneaky proxy
View reify_all.clj
;; we have to do a bit of magic to make the [[ConnectionDataSource]] work -- normally if someone were to use it with
;; `with-open` then the original connection would get closed when someone called `.close` on the one from the
;; `DataSource`. So we need to actually have [[ConnectionDataSource]] return a sneaky special proxy Connection that
;; forwards ALL of its other methods to the original Connection EXCEPT for `.close`, which should just no-op.
;;
;; This is all a little hacky for my tastes and it would be better if we could just eliminate [[ConnectionDataSource]]
;; entirely so we didn't need to use this anymore. It's only used in two places anyway.
(defn- reify-all-method [^java.lang.reflect.Method method original-symb]
(let [method-name (-> (symbol (.getName method))
@camsaul
camsaul / profiling.clj
Last active Jan 13, 2022
Profiling Results
View profiling.clj
;; profile everything in a namespace
(doseq [[_ varr] (ns-interns *ns*)] (profile.core/profile-var* varr))
;; run something and get profiling results
(profile.core/profile {} (no-lazyness-test))
@camsaul
camsaul / data.clj
Created Aug 25, 2021
Metabase data permissions graph spec
View data.clj
(ns metabase.models.permissions.graph.data
(:require [clojure.spec.alpha :as s]))
(s/def ::id
(s/and int? pos?))
(s/def :metabase.models.permissions.graph.data.table/read
#{:all :none})
(s/def :metabase.models.permissions.graph.data.table/query