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

unran -> unrun [ci all]

parent 983212a4
No related branches found
No related tags found
No related merge requests found
......@@ -106,6 +106,53 @@
;;; | MIGRATE! |
;;; +------------------------------------------------------------------------------------------------------------------------+
(defn- filename-without-path-or-prefix
"Strip the path and/or prefix from a migration FILENAME if it has them."
[filename]
(s/replace filename #"^(?:migrations/)?([\w\d]+)(?:\.(?:json|yaml))?$" "$1"))
(defn- migration-files:jar
"Return the set of migration filenames (without path or prefix) inside the uberjar.
(`io/resource` doesn't work here; this approach adapted from [this StackOverflow answer](http://stackoverflow.com/a/20073154/1198455).)"
[]
;; get path to the jar -- see this SO answer http://stackoverflow.com/a/320595/1198455
(let [^String jar-path (s/replace (-> ComboPooledDataSource .getProtectionDomain .getCodeSource .getLocation .getPath) #"%20" " ")
^JarFile$JarEntryIterator entries (.entries (JarFile. jar-path))
^Atom files (atom #{})]
(while (.hasMoreElements entries)
(let [^JarFile$JarFileEntry entry (.nextElement entries)
^String entry-name (.getName entry)]
(when (and (.startsWith entry-name "migrations/")
(not= entry-name "migrations/")) ; skip the directory itself
(swap! files conj (filename-without-path-or-prefix entry-name)))))
@files))
(defn- migration-files
"Return the set of migration filenames (without path or prefix) in the `resources/migrations` directory or from the JAR."
[]
;; unfortunately io/as-file doesn't seem to work for directories inside a JAR. Try it for local dev but fall back to hacky Java interop method if that fails
(try (set (map filename-without-path-or-prefix (.list (io/as-file (io/resource "migrations")))))
(catch Throwable _
(migration-files:jar))))
(declare quote-fn)
(defn- migration-entries
"Return a set of migration files (without path or prefix) that have already been run.
This is fetched from the `databasechangelog` table.
(migration-entires) -> #{\"001_initial_schema\", \"002_add_session_table\", ...}"
[]
;; an Exception will get thrown if there is no databasechangelog table yet; just return nil in that case because nil will never equal any set
(u/ignore-exceptions
(set (for [{filename :filename} (jdbc/query (jdbc-details) [(format "SELECT %s AS filename FROM %s;" ((quote-fn) "filename") ((quote-fn) "databasechangelog"))])]
(filename-without-path-or-prefix filename)))))
(defn- has-unrun-migration-files?
"`true` if the set of migration files is the same as the set of migrations that have already been run."
^Boolean []
(not= (migration-files)
(migration-entries)))
(defn migrate!
"Migrate the database (this can also be ran via command line like `java -jar metabase.jar migrate up` or `lein run migrate up`):
......@@ -251,15 +298,15 @@
[auto-migrate? db-details]
(when-not auto-migrate?
(print-migrations-and-quit! db-details))
(log/info "Database has unran migrations. Preparing to run migrations...")
(log/info "Database has unrun migrations. Preparing to run migrations...")
(migrate! db-details :up)
(log/info "Database Migrations Current ... " (u/emoji "✅")))
(defn- run-schema-migrations-if-needed!
"Check and see if we need to run any schema migrations, and run them if needed."
[auto-migrate? db-details]
(log/info "Checking to see if database has unran migrations...")
(if (has-unran-migration-files?)
(log/info "Checking to see if database has unrun migrations...")
(if (has-unrun-migration-files?)
(run-schema-migrations! auto-migrate? db-details)
(log/info "Database migrations are up to date. Skipping loading Liquibase.")))
......
......@@ -13,7 +13,7 @@
(def ^:private ^:const ^String changelog-file "liquibase.yaml")
(defn- migrations-sql
"Return a string of SQL containing the DDL statements needed to perform unran LIQUIBASE migrations."
"Return a string of SQL containing the DDL statements needed to perform unrun LIQUIBASE migrations."
^String [^Liquibase liquibase]
(let [writer (StringWriter.)]
(.update liquibase "" writer)
......@@ -31,8 +31,8 @@
(re-find #"^--" line)))]
line))
(defn- has-unran-migrations?
"Does LIQUIBASE have migration change sets that haven't been ran yet?
(defn- has-unrun-migrations?
"Does LIQUIBASE have migration change sets that haven't been run yet?
It's a good idea to Check to make sure there's actually something to do before running `(migrate :up)` because `migrations-sql` will
always contain SQL to create and release migration locks, which is both slightly dangerous and a waste of time when we won't be using them."
......@@ -54,15 +54,15 @@
(throw (Exception. "Database has migration lock; cannot run migrations. You can force-release these locks by running `java -jar metabase.jar migrate release-locks`.")))))
(defn- migrate-up-if-needed!
"Run any unran LIQUIBASE migrations, if needed.
"Run any unrun LIQUIBASE migrations, if needed.
This creates SQL for the migrations to be performed, then executes each DDL statement.
Running `.update` directly doesn't seem to work as we'd expect; it ends up commiting the changes made and they can't be rolled back at
the end of the transaction block. Converting the migration to SQL string and running that via `jdbc/execute!` seems to do the trick."
[conn, ^Liquibase liquibase]
(log/info "Checking if Database has unran migrations...")
(when (has-unran-migrations? liquibase)
(log/info "Database has unran migrations. Waiting for migration lock to be cleared...")
(log/info "Checking if Database has unrun migrations...")
(when (has-unrun-migrations? liquibase)
(log/info "Database has unrun migrations. Waiting for migration lock to be cleared...")
(wait-for-migration-lock-to-be-cleared liquibase)
(log/info "Migration lock is cleared. Running migrations...")
(doseq [line (migrations-lines liquibase)]
......@@ -79,7 +79,7 @@
Each DDL statement is ran inside a nested transaction; that way if the nested transaction fails we can roll it back without rolling back the entirety of changes
that were made. (If a single statement in a transaction fails you can't do anything futher until you clear the error state by doing something like calling `.rollback`.)"
[conn, ^Liquibase liquibase]
(when (has-unran-migrations? liquibase)
(when (has-unrun-migrations? liquibase)
(doseq [line (migrations-lines liquibase)]
(log/info line)
(jdbc/with-db-transaction [nested-transaction-connection conn]
......
(ns metabase.db.migrations
"Clojure-land data migration definitions and fns for running them.
These migrations are all ran once when Metabase is first launched, except when transferring data from an existing H2 database.
When data is transferred from an H2 database, migrations will already have been ran against that data; thus, all of these migrations
When data is transferred from an H2 database, migrations will already have been run against that data; thus, all of these migrations
need to be repeatable, e.g.:
CREATE TABLE IF NOT EXISTS ... -- Good
......@@ -39,7 +39,7 @@
(defn- run-migration-if-needed!
"Run migration defined by MIGRATION-VAR if needed.
RAN-MIGRATIONS is a set of migrations names that have already been ran.
RAN-MIGRATIONS is a set of migrations names that have already been run.
(run-migration-if-needed! #{\"migrate-base-types\"} #'set-card-database-and-table-ids)"
[ran-migrations migration-var]
......
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