Liking cljdoc? Tell your friends :D

metabase.db

Application database definition, and setup logic, and helper functions for interacting with it.

Application database definition, and setup logic, and helper functions for interacting with it.
raw docstring

*allow-potentailly-unsafe-connections*clj

We want to make every database connection made by the drivers safe -- read-only, only connect if DB file exists, etc. At the same time, we'd like to be able to use driver functionality like can-connect-with-details? to check whether we can connect to the Metabase database, in which case we'd like to allow connections to databases that don't exist.

So we need some way to distinguish the Metabase database from other databases. We could add a key to the details map specifying that it's the Metabase DB, but what if some shady user added that key to another database?

We could check if a database details map matched db-connection-details above, but what if a shady user went Meta-Metabase and added the Metabase DB to Metabase itself? Then when they used it they'd have potentially unsafe access.

So this is where dynamic variables come to the rescue. We'll make this one true when we use can-connect? for the Metabase DB, in which case we'll allow connection to non-existent H2 (etc.) files, and leave it false happily and forever after, making all other connnections "safe".

We want to make *every* database connection made by the drivers safe -- read-only, only connect if DB file exists,
etc.  At the same time, we'd like to be able to use driver functionality like `can-connect-with-details?` to check
whether we can connect to the Metabase database, in which case we'd like to allow connections to databases that
don't exist.

So we need some way to distinguish the Metabase database from other databases. We could add a key to the details
map specifying that it's the Metabase DB, but what if some shady user added that key to another database?

We could check if a database details map matched `db-connection-details` above, but what if a shady user went
Meta-Metabase and added the Metabase DB to Metabase itself? Then when they used it they'd have potentially unsafe
access.

So this is where dynamic variables come to the rescue. We'll make this one `true` when we use `can-connect?` for the
Metabase DB, in which case we'll allow connection to non-existent H2 (etc.) files, and leave it `false` happily and
forever after, making all other connnections "safe".
sourceraw docstring

*disable-data-migrations*clj

Should we skip running data migrations when setting up the DB? (Default is false). There are certain places where we don't want to do this; for example, none of the migrations should be ran when Metabase is launched via load-from-h2. That's because they will end up doing things like creating duplicate entries for the "magic" groups and permissions entries.

Should we skip running data migrations when setting up the DB? (Default is `false`).
There are certain places where we don't want to do this; for example, none of the migrations should be ran when
Metabase is launched via `load-from-h2`.  That's because they will end up doing things like creating duplicate
entries for the "magic" groups and permissions entries. 
sourceraw docstring

database-factoryclj

(database-factory)

Return an instance of the Liquibase DatabaseFactory. This is done on a background thread at launch because otherwise it adds 2 seconds to startup time.

Return an instance of the Liquibase `DatabaseFactory`. This is done on a background thread at launch because
otherwise it adds 2 seconds to startup time.
sourceraw docstring

db-fileclj

Path to our H2 DB file from env var or app config.

Path to our H2 DB file from env var or app config.
sourceraw docstring

db-is-setup?clj

(db-is-setup?)

True if the Metabase DB is setup and ready.

True if the Metabase DB is setup and ready.
sourceraw docstring

db-typeclj

(db-type)

The type of backing DB used to run Metabase. :h2, :mysql, or :postgres.

The type of backing DB used to run Metabase. `:h2`, `:mysql`, or `:postgres`.
sourceraw docstring

isaclj

(isa type-keyword)
(isa expr type-keyword)

Convenience for generating an HoneySQL IN clause for a keyword and all of its descendents. Intended for use with the type hierarchy in metabase.types.

(db/select Field :special_type (mdb/isa :type/URL)) -> (db/select Field :special_type [:in #{"type/URL" "type/ImageURL" "type/AvatarURL"}])

Also accepts optional EXPR for use directly in a HoneySQL where:

(db/select Field {:where (mdb/isa :special_type :type/URL)}) -> (db/select Field {:where [:in :special_type #{"type/URL" "type/ImageURL" "type/AvatarURL"}]})

Convenience for generating an HoneySQL `IN` clause for a keyword and all of its descendents.
Intended for use with the type hierarchy in `metabase.types`.

  (db/select Field :special_type (mdb/isa :type/URL))
   ->
  (db/select Field :special_type [:in #{"type/URL" "type/ImageURL" "type/AvatarURL"}])

Also accepts optional EXPR for use directly in a HoneySQL `where`:

  (db/select Field {:where (mdb/isa :special_type :type/URL)})
  ->
  (db/select Field {:where [:in :special_type #{"type/URL" "type/ImageURL" "type/AvatarURL"}]})
sourceraw docstring

jdbc-detailsclj

(jdbc-details)
(jdbc-details db-details)

Takes our own MB details map and formats them properly for connection details for JDBC.

Takes our own MB details map and formats them properly for connection details for JDBC.
sourceraw docstring

joinclj

(join [source-entity fk] [dest-entity pk])

Convenience for generating a HoneySQL JOIN clause.

(db/select-ids FieldValues (mdb/join [FieldValues :field_id] [Field :id]) :active true)

Convenience for generating a HoneySQL `JOIN` clause.

(db/select-ids FieldValues
  (mdb/join [FieldValues :field_id] [Field :id])
  :active true)
sourceraw docstring

migrate!clj

(migrate!)
(migrate! direction)
(migrate! db-details direction)

Migrate the database (this can also be ran via command line like java -jar metabase.jar migrate up or lein run migrate up):

  • :up - Migrate up
  • :force - Force migrate up, ignoring locks and any DDL statements that fail.
  • :down-one - Rollback a single migration
  • :print - Just print the SQL for running the migrations, don't actually run them.
  • :release-locks - Manually release migration locks left by an earlier failed migration. (This shouldn't be necessary now that we run migrations inside a transaction, but is available just in case).

Note that this only performs schema migrations, not data migrations. Data migrations are handled separately by metabase.db.migrations/run-all!. (setup-db!, below, calls both this function and run-all!).

Migrate the database (this can also be ran via command line like `java -jar metabase.jar migrate up` or `lein run
migrate up`):

*  `:up`            - Migrate up
*  `:force`         - Force migrate up, ignoring locks and any DDL statements that fail.
*  `:down-one`      - Rollback a single migration
*  `:print`         - Just print the SQL for running the migrations, don't actually run them.
*  `:release-locks` - Manually release migration locks left by an earlier failed migration.
                      (This shouldn't be necessary now that we run migrations inside a transaction, but is
                      available just in case).

Note that this only performs *schema migrations*, not data migrations. Data migrations are handled separately by
`metabase.db.migrations/run-all!`. (`setup-db!`, below, calls both this function and `run-all!`).
sourceraw docstring

setup-db!clj

(setup-db!)

Do general preparation of database by validating that we can connect. Caller can specify if we should run any pending database migrations. If DB is already set up, this function will no-op.

Do general preparation of database by validating that we can connect. Caller can
specify if we should run any pending database migrations. If DB is already set up, this function will no-op.
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close