1、先使用 sed 命令将虚拟字段的创建代码转变成普通字段的代码:
windows 使用 PwoerShell 执行以下命令:
& 'D:\Program Files\Git\usr\bin\sed.exe' -i 's/GENERATED ALWAYS AS .* VIRTUAL/NULL/' 'E://day_20230511.sql'
注意:sed 是 git 自带的工具,该命令的意思是将 GENERATED ALWAYS AS .* VIRTUAL 替换为 NULL。
linux 执行以下命令:
sed -i 's/GENERATED ALWAYS AS .* VIRTUAL/NULL/' mydump.sql
macOS 执行以下命令:
sed -i '.bak' 's/GENERATED ALWAYS AS .* VIRTUAL/NULL/' mydump.sql
2、导入 sql 文件
D:
cd D:\tools\phpstudy\Extensions\MySQL5.7.26\bin
mysql -u root -p db_name < e:/day_20230511.sql
注意:因为 mysqldump 命令导出的 SQL 文件中有自动创建数据库的命令,因此事先新建的不同名称的数据库不会产生作用。
另外:windows 下面要到 CMD 里执行以上命令。
3、使用查询语句恢复虚拟字段
恢复 admin_product 的虚拟字段和索引:
ALTER TABLE admin_product DROP COLUMN product_id;
ALTER TABLE admin_product ADD COLUMN product_id bigint(20) GENERATED ALWAYS AS (`data` ->> '$.product.id') VIRTUAL;
create INDEX idx_admin_product_product_id on admin_product(product_id);
ALTER TABLE admin_product DROP COLUMN customer_id;
ALTER TABLE admin_product ADD COLUMN customer_id bigint(20) GENERATED ALWAYS AS (`data` ->> '$.customer.id') VIRTUAL;
create INDEX idx_admin_product_customer_id on admin_product(customer_id);
恢复 admin_scheduled_task的虚拟字段和索引:
ALTER TABLE admin_scheduled_task DROP COLUMN mode_id;
ALTER TABLE admin_scheduled_task ADD COLUMN mode_id bigint(20) GENERATED ALWAYS AS (`data` ->> '$.mode_id') VIRTUAL;
create INDEX idx_admin_scheduled_task_mode_id on admin_scheduled_task(mode_id);
ALTER TABLE admin_scheduled_task DROP COLUMN class_name;
ALTER TABLE admin_scheduled_task ADD COLUMN class_name VARCHAR(255) GENERATED ALWAYS AS (`data` ->> '$.className') VIRTUAL;
create INDEX idx_admin_scheduled_task_class_name on admin_scheduled_task(class_name);
ALTER TABLE admin_scheduled_task DROP COLUMN model_change_type;
ALTER TABLE admin_scheduled_task ADD COLUMN model_change_type VARCHAR(255) GENERATED ALWAYS AS (`data` ->> '$.modelChangeType') VIRTUAL;
create INDEX idx_admin_scheduled_task_model_change_type on admin_scheduled_task(model_change_type);