             [clojure.string :as str]
             [selmer.parser :as selmer]))
 
-(defn get-tables [db]
+(defn get-tables
+  "Retrieves a list containing all tables in the database."
+  [db]
   (j/query db "show tables"))
 
-(defn schema-info [db table]
+(defn column-usage
+  "Gets all the foreign keys declarations for a particular schema and table."
+  [db schema table]
   (j/query db ["select *
                 from INFORMATION_SCHEMA.KEY_COLUMN_USAGE
                 where table_name = ?
                 and referenced_table_name is not null
+                and table_schema = ?
                 and column_name is not null
                 and referenced_column_name is not null
                 and constraint_name <> 'PRIMARY'"
-               table]))
+               table
+               schema]))
 
-(defn has-foreign-keys? [db table]
-  (boolean (seq (schema-info db table))))
-
-(defn tables-info [db tables]
+(defn describe-tables
+  "Retrieves information regarding all the tables passed as argument."
+  [db tables]
   (map (fn [table]
          (conj
           (j/query db (str "describe " table))
           {:type "table" :name table :key ""}))
        tables))
 
-(defn column-type [{:keys [type]}]
+(defn column-type
+  "Translates the MySQL column type to Phinx column type."
+  [{:keys [type]}]
   (cond
     (re-find #"int\(\d+\)" (str type))      "integer"
     (= "datetime" type)                     "datetime"


        $table
        {% for field in fields %}
            {% if field.options %}
            ->addColumn('{{field.name}}', '{{field.type}}', [
                {% for option in field.options %}
                '{{option.0}}' => '{{option.1}}',
                {% endfor %}
            ])
            {% else %}
            ->addColumn('{{field.name}}', '{{field.type}}')
            {% endif %}
        {% endfor %}
        {% for fk in fks %}
            ->addForeignKey('{{fk.col-name}}', '{{fk.referenced-table}}', '{{fk.referenced-col-name}}')

  (let [col-type (column-type column)]
    {:name    (:field column)
     :type    col-type
     :options (cond-> [["null" (= "YES" (:null column))]]
                (= "enum" col-type)                  (conj ["values" (enum-values column)])
                (not (str/blank? (:default column))) (conj ["default" (:default column)]))}))

(defn camelize-table [table-name]
  (let [parts (str/split table-name #"_")]
    (reduce str (map str/capitalize parts))))

(defn table-data [db schema table]
  (let [pk-entry   (->> table
                        (filter #(= (:key %) "PRI"))
                        first)
