laravel - 如何在 Laravel 中执行 DDL 迁移脚本?
问题描述
我没有使用 Laravel 的静态类(如 DB 和 Schema),而是尝试通过读取脚本来执行完整的 DDL 脚本,然后将其执行到 MySQL 实例。
class CreateDb extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
$script = $this->readScript("create_tables.sql");
DB::statement($script);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
$script = $this->readScript("drop_table.sql");
DB::statement($script);
}
/**
* @param $str
* @return false|string
*/
protected function readScript($str) {
$scripts = getcwd() . "/database/scripts/";
$script_location = $scripts . $str;
$myfile = fopen($script_location, "r") or die("Unable to open script file: " . $script_location);
$script = fread($myfile, filesize($script_location));
fclose($myfile);
return $script;
}
}
但是,我收到此错误:
Syntax error or access violation: 1064 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 if not exists comments
当我在控制台(Intellij Idea Console)中执行脚本时,它可以工作,所以我认为 Ii 可能DB::statement($script)
不是完成这项工作的正确功能!
SQL 文件如下所示:
解决方案
推荐阅读
- angular - Chartjs + DataLabelPlugin:即使没有插件,数据标签也会突然出现在每个图表中?
- javascript - 为什么我得到 2 倍的数据空值和数据?
- android - l 不同设备内有RelativeLayout问题
- kotlin - 缩短此代码以检查表达式中的 null
- javascript - 使用 jquery 显示块后 Jquery 插件不起作用
- swift - 通过提供自动布局约束来制作具有相同间距的按钮
- syntax - 位向量函数 Z3
- windows - 在 Windows Server 中自动启动我自己的脚本
- python - 如何获得2个连续记录之间的“时间差”?
- sql - 从子查询更新多个字段