Answer to the Core Question
Laravel’s db:dump and db:load commands do not ship with any built‑in mechanism to verify that a restore finished successfully. They simply invoke the underlying database client (mysqldump/pg_dump, etc.) and stream the SQL back into the target database. If the client aborts mid‑execution, the database is left in whatever state it had up to that point – there is no automatic rollback or atomicity guarantee.
Why No Native Verification Exists
- The commands are thin wrappers around the database’s bulk import utilities; the utilities themselves do not expose a post‑load checksum API.
- Laravel’s design philosophy keeps the ORM and migration tools separate from raw dump/restore operations, so no hook is provided for custom validation.
- Partial restores are treated as normal insert failures; the database engine may silently truncate, skip, or error‑log rows depending on its own error handling mode.
Typical Ways to Verify a Successful Restore
Although Laravel offers no automatic checks, you can reliably confirm that a db:load finished by running a handful of queries or using database‑specific utilities.
1. Compare Row Counts
# Source (dump file) – assuming you have a count stored in a SQL file or a separate table
SELECT COUNT(*) FROM source_table;
# Target after load
SELECT COUNT(*) FROM target_table;
Both counts should match exactly. If they differ, a partial load occurred.
2. Verify Referential Integrity
SELECT COUNT(*)
FROM target_table t
LEFT JOIN referenced_table r ON t.fk_id = r.id
WHERE r.id IS NULL;
A result of zero confirms that all foreign keys point to existing rows.
3. Use Database Checksums (if supported)
- MySQL –
CHECKSUM TABLE (works only for MyISAM/Aria, not InnoDB). Example:
CHECKSUM TABLE target_table;
- PostgreSQL –
pg_dump --schema-only plus a pg_dump --data-only checksum, or compute md5() over a deterministic column set:
SELECT md5(string_agg(col1 || col2, ',')) FROM target_table ORDER BY id;
- SQLite – compute an
md5 of the entire file or run a row‑count + checksum over a key column set.
4. Wrap the Load in a Transaction (where supported)
Some database clients support a --single-transaction flag (MySQL) or you can manually start a transaction before running db:load and commit afterward. If the load fails, you can roll back, preventing a half‑loaded state. Example for MySQL:
mysql -u user -p -e "START TRANSACTION; SOURCE /path/to/dump.sql; COMMIT;"
Laravel’s db:load does not expose this flag directly, so you would need to run the raw client command or write a custom Artisan command that wraps the load in a transaction.
Handling Partial Restores
When the client binary aborts (e.g., due to timeout, memory limit, or network drop), the database will contain whatever rows were successfully inserted before the failure. No automatic cleanup occurs; the next restore attempt will simply append more rows, potentially causing duplicates or constraint violations.
To mitigate this risk:
- Enable strict mode or
STRICT_TRANS_TABLES in MySQL to make the engine throw errors on invalid rows.
- Use
--replace or --ignore options in the dump client to control how duplicate keys are handled.
- Run a pre‑load idempotency check: delete or truncate the target table before loading if you know the dump is a full replacement.
- Monitor the client’s exit status and log the number of rows processed (most clients provide a summary line).
Next Steps and Missing Detail
Some of the commands and flags differ between database engines. To give you the most accurate guidance for your environment, could you confirm which database system you are using (MySQL/MariaDB, PostgreSQL, SQLite, etc.)? This will allow me to tailor the checksum or transaction‑wrapping instructions to your setup.