1.4 KiB
1.4 KiB
Walkthrough - Initial Database Data Seeding
I have implemented a database-agnostic Laravel Seeder to handle the initial database population from the db/kulakposmysql.sql file. This approach is compatible with both MySQL and PostgreSQL.
Changes Made
Database Seeders
- Created InitialDataSeeder.php:
- This seeder dynamically reads the
db/kulakposmysql.sqlfile. - It parses
INSERT INTOstatements and uses Laravel'sDB::table()->insert()to populate the tables. - It automatically detects the database driver (MySQL or PostgreSQL) and applies the correct commands to temporarily disable foreign key constraints during the import.
- It uses Laravel's Schema Builder to map data values to the correct table columns.
- This seeder dynamically reads the
How to Verify
- Run standard migrations first (if not already done):
php artisan migrate - Run the Initial Data Seeder:
php artisan db:seed --class=InitialDataSeeder - Verify Data: Check your database (e.g.,
users,businesses) to confirm that the data from the SQL file has been correctly imported.
Tip
This seeder is designed to be safe for PostgreSQL, which was the cause of the previous failure. It uses the
session_replication_rolesetting to handle foreign keys on PostgreSQL.