我正在嘗試爲單元測試創建一個MariaDB數據庫,並希望使用Spring的ScriptUtils
來執行包含表和testdata轉儲的SQL文件。當我創建使用HeidiSQL轉儲它插入註釋生成的SQL文件,如下所示禁用外鍵檢查,同時在裝載包含代碼:Spring的ScriptUtils忽略包含轉儲註釋的代碼
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
當我通過HeidiSQL一切正常執行dump腳本很好。在拋出,因爲外鍵約束失敗例外
@BeforeClass
public static void initializeForTest() throws Exception {
context = new ClassPathXmlApplicationContext("Beans.xml");
dsRoot = (DataSource) context.getBean("dataSourceRoot");
ScriptUtils.executeSqlScript(dsRoot.getConnection(), new ClassPathResource("DROP_AND_CREATE_DATABASE.sql"));
}
結果,因爲轉儲創建了哪個仍然要創建表的外鍵引用一些表:在Java中使用下面的函數,雖然。例如:
CREATE TABLE `cities` (
`postal_code` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`country_alpha2` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`postal_code`,`country_alpha2`),
KEY `country_alpha2` (`country_alpha2`),
CONSTRAINT `cities_ibfk_1` FOREIGN KEY (`country_alpha2`) REFERENCES `countries` (`alpha2`)
);
/* ... */
CREATE TABLE `countries` (
`code` int(3) NOT NULL,
`alpha2` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`alpha3` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL,
`langCS` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`langDE` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`langEN` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`langES` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`langFR` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`langIT` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`langNL` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`code`),
UNIQUE KEY `alpha2` (`alpha2`),
UNIQUE KEY `alpha3` (`alpha3`)
);
我能以某種方式執行腳本而不忽略含代碼的註釋嗎?