尝试使用ragtime迁移完美的SQL脚本时发生执行错误

时间:2019-04-18 15:06:31

标签: mysql clojure

一个简单的迁移尝试:

(ns sindu.migration
  (:require [ragtime.jdbc :as jdbc]
            [ragtime.repl :as repl]))

(def ^:dynamic *db-host* "localhost")
(def ^:dynamic *db-port* 3306)
(def ^:dynamic *db-name* "sindu")
(def ^:dynamic *db-user* "sindu")
(def ^:dynamic *db-user-pass* "sindu")

(def db-config {:classname "com.mysql.jdbc.Driver"
                :subprotocol "mysql"
                :subname (str "//" *db-host* ":" *db-port* "/" *db-name*)
                :user *db-user*
                :password *db-user-pass*})

(def migration-config
  {:datastore  (jdbc/sql-database db-config)
   :migrations (jdbc/load-resources "migrations")})

(repl/migrate migration-config)

最后一行失败,并显示以下错误:

Applying 001-site
Execution error (SQLSyntaxErrorException) at com.mysql.cj.jdbc.exceptions.SQLError/createSQLException (SQLError.java:120).
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CREATE TABLE site_translation (site_id INT(11) NOT NULL,
                       ' at line 7

以及相关的SQL脚本(001-site.up.sql):

-- Create site and site_translation tables

CREATE TABLE site (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
                   name VARCHAR(255) NOT NULL,
                   base_url VARCHAR(511) NOT NULL);

CREATE TABLE site_translation (site_id INT(11) NOT NULL,
                               language VARCHAR(5),
                               display_name VARCHAR(255) NOT NULL,
                               FOREIGN KEY (site_id) REFERENCES site(id));

当我从MySQL控制台执行source 001-site.up.sql时,哪一个工作得很好。

此错误的原因是什么?

1 个答案:

答案 0 :(得分:0)

原来,我们需要在单独的语句之间插入--;;

-- Create site and site_translation tables

CREATE TABLE site (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
                   name VARCHAR(255) NOT NULL,
                   base_url VARCHAR(511) NOT NULL);

--;;

CREATE TABLE site_translation (site_id INT(11) NOT NULL,
                               language VARCHAR(5),
                               display_name VARCHAR(255) NOT NULL,
                               FOREIGN KEY (site_id) REFERENCES site(id));