-
Notifications
You must be signed in to change notification settings - Fork 120
insertOrSkip() for seeds. #942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! 👍🏻
I would also encapsulate some code, but that's out of the scope.
Co-authored-by: Yurii Zadryhun <[email protected]>
Co-authored-by: Yurii Zadryhun <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for an insertOrSkip() method that enables idempotent inserts by skipping rows that would cause duplicate key conflicts. The implementation uses database-specific syntax (INSERT IGNORE for MySQL, ON CONFLICT DO NOTHING for PostgreSQL, INSERT OR IGNORE for SQLite) while throwing an exception for SQL Server where this feature is not natively supported.
Key changes:
- Added
InsertModeenum withINSERTandIGNOREcases to control insert behavior - Implemented
insertOrSkip()method inTable,SeedInterface, andBaseSeedclasses - Modified adapter interfaces and implementations to support the new insert mode parameter
- Added comprehensive test coverage for all database adapters
Reviewed Changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Db/InsertMode.php | New enum defining insert modes (standard INSERT vs INSERT IGNORE) |
| src/Db/Table.php | Added insertOrSkip() method and insert mode property to support skip-on-duplicate behavior |
| src/SeedInterface.php | Added insertOrSkip() interface method for seeder classes |
| src/BaseSeed.php | Implemented insertOrSkip() method in base seeder class |
| src/Db/Adapter/AdapterInterface.php | Updated insert methods to accept optional InsertMode parameter |
| src/Db/Adapter/AbstractAdapter.php | Added getInsertPrefix() method to generate INSERT IGNORE syntax and updated insert methods |
| src/Db/Adapter/MysqlAdapter.php | Updated docblock type annotations for consistency |
| src/Db/Adapter/PostgresAdapter.php | Implemented PostgreSQL-specific ON CONFLICT DO NOTHING syntax via getConflictClause() |
| src/Db/Adapter/SqliteAdapter.php | Implemented SQLite-specific INSERT OR IGNORE syntax via getInsertPrefix() |
| src/Db/Adapter/SqlserverAdapter.php | Added exception throwing for unsupported INSERT IGNORE operation |
| src/Db/Adapter/AdapterWrapper.php | Updated wrapper methods to pass through InsertMode parameter |
| src/Db/Adapter/TimedOutputAdapter.php | Updated timed output methods to pass through InsertMode parameter |
| src/View/Helper/MigrationHelper.php | Enhanced docblock type annotation for better type safety |
| tests/TestCase/Db/Adapter/MysqlAdapterTest.php | Added three test cases covering duplicate handling, bulk inserts, and normal inserts |
| tests/TestCase/Db/Adapter/PostgresAdapterTest.php | Added three test cases covering duplicate handling, bulk inserts, and normal inserts |
| tests/TestCase/Db/Adapter/SqliteAdapterTest.php | Added three test cases covering duplicate handling, bulk inserts, and normal inserts |
| tests/TestCase/Db/Adapter/SqlserverAdapterTest.php | Added test case verifying exception is thrown for unsupported operation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
Resolves cakephp/phinx#2205
Follows #939 and in general helps to implement safer idempotent seeding.
Database Support:
The current implementation also allows other modes internally in the future, including update (upsert)
Out of scope: insertOrUpdate()
INSERT ... ON DUPLICATE UPDATE - Complex
Database Support Matrix
We can still add insertOrUpdate() later if needed
Example future use: