mysql - 我可以合并一个运行 insert into 的 .sql 文件和一个在 2 列上创建虚拟数据的 Laravel 工厂播种器吗?
问题描述
我有一个为 2 列创建虚拟数据的工厂文件:代码和条形码。但是,我想在运行工厂命令后运行一个替换数据的 .sql 文件。有了这个,我猜会发生合并,可能会删除一些记录,或者添加新记录。是否可以合并数据。喜欢自动映射?
这是我的 .sql 文件的内容:
-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Apr 23, 2021 at 12:14 PM
-- Server version: 10.1.28-MariaDB
-- PHP Version: 7.1.11
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `db_zachmatic`
--
--
-- Dumping data for table `products`
--
REPLACE INTO `products` (`id`, `code`, `name`, `description`, `unit`, `cost`, `srp`, `supplier`, `qty_on_hand`, `category`, `delivery_date`, `created_at`) VALUES
(12, 'P-824830', 'magnum-v', '45/90-17 MV-360 tube type tires only', 'Pieces', '', '1246', 'HH All Ventures', 3, 'Tires', '', ''),
(13, 'P-232033', 'magnum-v', '50/100-17 MV-360 tube type tire only', 'Select Product ', '', '1246', 'HH All Ventures', 1, 'Tires', '', ''),
(14, 'P-73032309', 'magnum-v', '60/90-17 MV-360 Tube Type Tire Only', 'Pieces', '', '1341', 'HH All Ventures', 0, 'Tires', '', ''),
(15, 'P-0022252', 'magnum-v', '45/90-17 MV-329 Tube Type with Tube', 'Pieces', '', '1120', 'HH All Ventures', 9, 'Tires', '', ''),
(16, 'P-323694', 'magnum-v', '50/100-17 MV-329 tube type with tube', 'Select Product ', '', '1120', 'HH All Ventures', 0, 'Tires', '', ''),
(17, 'P-023202', 'magnum-v', '60/90-17 MV-329 tube type with tube', 'Pieces', '', '1499', 'HH All Ventures', 0, 'Tires', '', ''),
(18, 'P-4729320', 'magnum-v', '70/90-17 MV-329 tube type with tube', 'Pieces', '', '1678', 'HH All Ventures', 0, 'Tires', '', ''),
(19, 'P-3020323', 'magnum-v', '120/70-13 MV-119C Tubeless', 'Pieces', '', '1829', 'HH All Ventures', 5, 'Tires', '', ''),
(20, 'P-3220830', 'magnum-v', '130/70-13 MV-119C tubeless', 'Pieces', '', '2062', 'HH All Ventures', 0, 'Tires', '', ''),
--
-- AUTO_INCREMENT for table `products`
--
ALTER TABLE `products` DROP COLUMN `id`;
ALTER TABLE `products` ADD COLUMN `id` INT AUTO_INCREMENT UNIQUE FIRST;
--
-- Drop `code` column to delete old barcodes
-- then re-add `code` column, then add `barcode` column
--
ALTER TABLE `products` DROP COLUMN `code`;
ALTER TABLE `products` ADD COLUMN code VARCHAR(255) AFTER id;
--
-- Drop `delivery_date` column to delete old column
-- then re-add `delivery_date` column, then add `barcode` column
--
ALTER TABLE `products` DROP COLUMN `delivery_date`;
ALTER TABLE `products` ADD COLUMN delivery_date DATETIME;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
然后我的工厂文件:
<?php
namespace Database\Factories;
use App\Models\Product;
use Illuminate\Database\Eloquent\Factories\Factory;
use Picqer;
class ProductFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Product::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$code = $this->faker->bothify('PHZM-##########');
$barcode = $this->generateBarcode($code);
return [
'code' => $code,
'barcode' => $barcode,
];
}
private function generateBarcode($code_to_convert) {
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
$barcode = $generator->getBarcode($code_to_convert, $generator::TYPE_CODE_128, 1, 15);
return $barcode;
}
}
任何帮助表示赞赏。
解决方案
我猜你必须创建播种器来运行这个脚本文件,如下所示:
<?php
use Illuminate\Database\Seeder;
class SqlFileSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$path = public_path('sql/File.sql');
$sql = file_get_contents($path);
DB::unprepared($sql);
}
}
在您的工厂调用的另一个播种机中,您在调用工厂后也运行此播种机
<?php
use Illuminate\Database\Seeder;
class FooSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Product::factory()->count(20)->create();
$this->call([SqlFileSeeder::class]);
}
}
推荐阅读
- gatsby - 在 gatsby 构建期间无法读取样式化组件的主题属性
- python - 训练,神经网络,但为什么 Model.add 不起作用?
- c# - 无法建立 HTTP/2 连接,因为服务器未完成 HTTP/2 握手
- go - 如何在 GORM 中处理递归关联
- jsf - Primefaces 3.4.1 不会在对话框中使用表单进行 POST
- excel - 如何搜索两个不同的范围并输入答案
- css - 如何在 Ant Design 中调整拖动行的高度?
- marklogic - 我可以将 mlDatabasesWithForestsOnOneHost 与副本一起使用吗?
- html - 让 div "zxing-frame" 适合视频部分而不是覆盖整个网页?
- r - geom_density_ridges 中的“缩放”统计信息