laravel-migration
Repeatable, chunked data migrations from legacy databases into Laravel, with tracking and memory-safe queued jobs.
Install
composer require paper-leaf-tech/laravel-migrationFull documentation lives in the README.
Writing about laravel-migration
Changelog
Changelog
All notable changes to laravel-migration will be documented in this file
2.3.0
Any source driver now works with any destination driver: MySQL/MariaDB, PostgreSQL and SQLite on either side.
Fixed
- Chunk SQL no longer hard-codes MySQL backticks. Every identifier — the table, qualified key columns,
ORDER BYcolumns, selected columns and their aliases — is quoted by the source connection's own query grammar, so a PostgreSQL (or SQL Server) source no longer fails on its first query. SQLite only worked before because it tolerates backticks. - A schema-qualified table name works on PostgreSQL.
legacy.user_accountis read as tableuser_accountin schemalegacy, for the chunk queries, joins and primary-key detection alike; the alias prefix is built from the table name alone (U_A_*, notL_U_A_*). - Column aliases are quoted, so PostgreSQL no longer folds them to lower case. Aliases on a PostgreSQL source are upper-cased by default, so job code written against legacy
UPPER_CASEnames ($item->U_A_USER_ID) keeps working when the source's unquoted names have been folded to lower case. - Window-function support is judged per driver: MySQL 8.0+, MariaDB 10.2+ (under either the
mysqlormariadbdriver), SQLite 3.25+, PostgreSQL and SQL Server use keyset chunking. A driver the package does not know now falls back to ordered offset chunking instead of being assumed to accept the boundary SQL. BulkWriterrespects the destination's bound-parameter limit: 60,000 per statement on MySQL, MariaDB and PostgreSQL as before, 32,000 on SQLite 3.32+ (990 before it), and 2,000 on SQL Server. Id read-backs are capped the same way.
Added
- An
alias_caseconfig key (MIGRATION_ALIAS_CASE):upper,lowerorpreserve, ornullfor the driver default (upperon PostgreSQL,preserveelsewhere). An unknown value is reported before anything is dispatched. MigrationCommand::getTableNameExpression()takes an optional connection name and defaults to the configured source connection.- An optional, env-gated test that reads a real PostgreSQL table read-only (
MIGRATION_TEST_PGSQL_URL,MIGRATION_TEST_PGSQL_TABLE). It is skipped by default; the suite still needs nothing but SQLite.
Compatibility
- MySQL and MariaDB sources are unchanged: a dot in a table name is still part of one identifier, and aliases keep the catalog's case. The one difference is that the base table is now quoted (
`USERS`rather thanUSERS) and the source connection's table prefix, if it has one, is applied to it, as it already was to joined tables. - A SQLite source now reads a dot as
schema.table(an attached database) rather than as part of one table name. MigrationJobSpawner::qualifyColumn()is no longer static.
2.2.0
Performance
- The chunk boundary query no longer applies
DISTINCTwhen the table has no joins. Only a join can repeat the key, so an unjoined table was paying for a dedup pass over a column that is unique by definition. Measured against a real 941k-row MariaDB table: 0.431s to 0.350s, and one fewer derived table in the plan. BulkWriternow caps a statement by payload size as well as placeholder count. The placeholder ceiling alone does not bound bytes: a few hundred rows carrying a large text column could exceedmax_allowed_packet, which defaults to 16MB on some servers, and fail the whole statement.
Experience
- A
--syncrun now shows progress. It never reaches a queue, so nothing could be counted from the outside and the command sat silent for the length of the run; chunks now report completion as they finish. - Dependency group banners are no longer drawn as rows of asterisks. Groups and per-table counts use the standard Laravel console components, and per-table counts are pluralised (
1 job, not1 jobs). - A run ends with what actually moved — rows, tables, jobs and elapsed time — instead of just "Migration completed."
- A dry run no longer prints group dispatch headers, which claimed to be dispatching work it was not.
Fixed
- Per-run state (the dry-run plan, the start time) is reset when the command runs. Artisan resolves a command once and reuses it, so a second
Artisan::call('migration:run')in the same process reported doubled totals.
2.1.1
- The agent skill no longer tells developers to give migrations a dedicated queue. The wait counting delayed and reserved jobs is real but conditional, and on a development machine the default queue is usually idle — so it now describes the mechanism and the symptom, and offers
MIGRATION_QUEUE_NAMEas a remedy if a run actually stalls.
2.1.0
- Added an agent skill at
resources/boost/skills/writing-migration-jobs/, covering column projection,handleChunk()overhandleItem(), the plan/insert/resolve pattern, and the anti-patterns that make a migration slow. Laravel Boost discovers it automatically in any application requiring this package directly; other setups can point an agent at the file. A test pins its location and checks that every writer method it documents still exists. - Added
BulkWriterandBaseMigrationJob::writer(), for migration jobs that write a parent row so they can use its new id for child rows.insertAndMap()writes a level in one statement and returns[legacy id => new id], so the next level can be planned in memory and written in one statement too.add()/addMany()buffer into a shared writer that the chunk flushes when it finishes, so logic split across traits does not each write its own rows. BulkWritergives every row in a batch the same columns. Laravel'sinsert()takes its column list from the first row and sorts each row's values by key, so a row with a different key set lands in the wrong columns without raising anything; a missing value is now an explicit null.- Batches are split to stay under MySQL's 65,535 bound-parameter ceiling, and id read-backs are split into
IN ()lists of 5,000. - Dropped elapsed time, the estimated-time reading and the memory reading from the queue progress bar. The times rendered as
< 1 mswith ragged padding and told you nothing useful — progress here is driven by queue depth, which moves in bursts as workers pick chunks up, so a linear extrapolation is noise. The memory figure was the command process's own, not the workers' doing the migrating. The bar now reads1204/2409 jobs [=====>----] 49%. - The progress bar is redrawn after each poll rather than before it, so the figure shown is the depth just observed instead of the previous round's.
2.0.0
Correctness, performance and reporting work across the command and the chunk
spawner. Applications only using the migration:run / migration:new-job
commands should upgrade cleanly; read the breaking changes first if you call
the package's classes directly or run it unattended.
Breaking changes
MigrationCommand::waitForEmptyQueue()is nowawaitQueue()and returnsbool(whether the queue drained without failures) instead ofvoid.migrateTable(),migrateJobGroup()andmigrateAllTables()returnboolinstead ofvoid, reporting whether the work succeeded.migration:runexits non-zero when a table is unmapped, its job class is missing, a configured column does not exist, or any job failed while the run was waiting. It previously printed the error and exited0.- The queue-wait loop is scoped to the configured queue name and, on Redis, also counts the
:delayedset. If the migration queue is shared with the rest of the application, a delayed or released job on that queue will now hold the run until it clears.MIGRATION_QUEUE_NAMEgives the migration a queue of its own if that becomes a problem. - A
queue_connectionother thandatabaseorredisis rejected during environment verification rather than throwing partway through a run. - Chunk jobs are pushed with the queue driver's bulk API, which bypasses the Bus dispatcher. Job middleware,
ShouldBeUniqueandafter_commitdo not apply to chunk jobs. - Laravel 11 is the minimum. The package uses the Laravel 11 schema inspection API (
getIndexes()/getColumns()); on Laravel 10 those throw, get swallowed, and the package silently degrades to unordered chunking. - Protected members of
MigrationJobSpawnerwere renamed:getChunkBoundaries()→computeChunkBoundaries(),dispatchChunk()→dispatchChunks(),dispatchOffsetChunks()→offsetChunkQueries().
Fixed
- The published config no longer maps
USERStoApp\Jobs\Migration\UsersMigrationJob. That class does not exist in a freshly installed app, so environment verification failed and everymigration:runaborted until the entry was commented out by hand. - The queue-wait loop now counts only jobs on the configured migration queue, on the queue driver's own database connection and table (
queue.connections.*.connection/.table). It previously ranDB::table('jobs')->count()against the default connection, so unrelated application jobs kept it spinning and a renamed or relocated jobs table broke it outright. composer.jsondeclares itsilluminate/*constraints (^11.0|^12.0|^13.0), and the suite runs against all three. The package uses the Laravel 11 schema inspection API (getIndexes()/getColumns()); on Laravel 10 those throw, get swallowed, and the package silently degrades to unordered chunking.- Selected columns are now emitted as pre-quoted expressions, so a source table whose name contains a dot stays one identifier. Column aliases are unchanged (
USERS.FNAMEis stillU_FNAME). - Join definitions only require
table,firstandsecond.operatordefaults to=andtypedefaults toinner, matching what the config comments already promised; omitting either previously raised an undefined-key error. - The generated job stub no longer calls
fill()on an undefined$record, which made every freshly scaffolded job fatal on first run. --allwarns whentable_dependency_groupsis empty instead of reporting "Migration completed." having dispatched nothing.- Added a test suite (
composer test) covering the above, plus the 1.1.0 chunking behaviour.
Performance
- Chunks are now planned in the command process instead of in a queued spawner job. Planning a large table outran the worker's
--timeout(60s) and the connection'sretry_after(90s); a released spawner then re-dispatched chunks it had already dispatched, migrating those rows twice. - Chunk jobs are pushed through the queue driver's bulk API in batches of 500 — one multi-row insert for the database driver, one pipelined transaction for Redis — instead of one round trip per chunk. Dispatching 50 chunks went from 50 writes to 1.
- Added an optional
columnskey to the table mapping. Chunk queries previously selected every column of every table involved, on the source read and in each job's queue payload. Projecting 4 of 60 columns cut the queued payload for one table by 77%. - The keyset boundary query now reports the distinct key count via
COUNT(*) OVER (), removing the separatecount(distinct ...)scan. A table is walked once before dispatch, not twice. - Source schema (columns and indexes) is read once per table and cached, instead of once in the constructor and again when building the select. Per-table pre-flight queries went from 7 to 4.
BaseMigrationJob::handle()streams the chunk with a cursor rather than materialising it. A 5,000-row chunk handled row by row peaks at ~128 KB instead of ~14 MB.- The queue-wait loop backs off from 1s towards 10s while the queue is not moving and drops back to 1s as soon as it is, instead of counting rows every second for the length of the migration.
Experience
- Failures are reported. The command watches
failed_jobsfor the migration queue while a group runs, names the count at the end, and exits non-zero. Previously a run could lose every chunk and still print "Migration completed." - Added
--dry-run, which reports the tables, row counts and job counts a run would produce without dispatching anything. - Added
--chunk-size=N,--queue,--syncand--no-wait.--queuemakes the previously unreachable queued single-table path usable;--syncruns everything without a worker. - Added
BaseMigrationJob::handleChunk(), a per-chunk hook for jobs that can batch their destination writes.handleItem()still works unchanged. - Mapped tables missing from every dependency group are named at the start of
--allinstead of being silently skipped. - The progress bar shows elapsed time, an ETA and memory, and tracks completed jobs directly rather than inferring them from queue-depth deltas.
1.1.0
Deterministic chunking. This is a behavioural change, not a bugfix only patch — chunk composition changes and jobCount semantics shift from rows to distinct keys. Apply it between full migration runs, never mid-migration.
- Chunk jobs are now split by primary key range (keyset chunking) instead of
LIMIT/OFFSETwith noORDER BY. Offset chunking without a total order let the database return a different row sequence per chunk query, which could process a source row twice or skip it silently. Range chunks also drop theOFFSET mscan, whose cost grew quadratically across a full table walk. - Keyset chunking engages when the source table has a single column integer primary key and the engine supports window functions (MySQL 8.0+ / MariaDB 10.2+; every other supported driver qualifies).
- Tables that do not qualify keep offset chunking, but now carry an
ORDER BYover the primary key. Tables with no primary key at all keep the previous behaviour and log a warning naming the table. MigrationJobSpawner::$totalCountcounts distinct keys rather than joined rows when keyset chunking is active, so the progress bar andjobCountstay accurate under a one to many join.
1.0.0
- initial release