Comments (6)
Ahhh we just found this
online_migrations/lib/online_migrations/schema_statements.rb
Lines 776 to 779 in 38cee8c
from online_migrations.
I think the docs could instead suggest distinct migrations without disable_ddl_transaction
. Suggesting to split the migrations would allow the add_foreign_key
migration to record as successful and the failing validate_foreign_key
to be the one that errors in isolation.
I'd be happy to PR something like the following if you think it improves the docs
Existing
Lines 863 to 876 in 38cee8c
Proposed
Add the foreign key without validating existing rows, and then validate them in a separate transaction.
class AddForeignKeyToProjectsUser < ActiveRecord::Migration[7.1]
def change
add_foreign_key :projects, :users, validate: false
end
end
class ValidateForeignKeyToProjectsUser < ActiveRecord::Migration[7.1]
def change
validate_foreign_key :projects, :users
end
end
Note: If you combine those into a single migration you must use disable_ddl_transaction!
, without it the migration will fail.
from online_migrations.
@fatkodima thanks for doing this work 💯
from online_migrations.
Suggesting to split the migrations would allow the add_foreign_key migration to record as successful and the failing validate_foreign_key to be the one that errors in isolation.
Can you point to the benefits of this approach?
from online_migrations.
Suggesting to split the migrations would allow the add_foreign_key migration to record as successful and the failing validate_foreign_key to be the one that errors in isolation.
Can you point to the benefits of this approach?
I think the benefit is to logically isolate the failing validate_foreign_key
step of the migration while allowing the add_foreign_key
step to proceed and register as completed. The system would then only reapply and report failures for non-completed migration steps.
I feel this isolation avoids the developer having to interpret the combined migration step logs to understand the add_foreign_key
statement is skipped and the validate_foreign_key
is failing, specifically the logged output from foreign_key_exists
method in #54 (comment)
I'd like to note that the migration functionality between the two options is mostly the same. The only time that separating the steps across migrations might be useful is when the validate_foreign_key
step errors and we fail / repeat the whole migration.
from online_migrations.
For me, having just everything in 1 migration and knowing that it will be idempotent is easier, because I can just copy paste the suggestion inside the already generated file (actually if the gem's suggestion happens after the migration was generated). If the gem suggests 2 files, we need to manually create a second file with proper timestamp etc. So some extra work.
But, if people think that this is more confusing, I am ok with the change, so PR welcome. You need to change the readme as well as the command_checker.rb
to suggest the 2 files migration.
from online_migrations.
Related Issues (20)
- Error at running the finalize_column_rename migration HOT 10
- `add_not_null_constraint` does not quote column name in check constraint expression HOT 1
- `add_reference_concurrently` does not properly check for existence of foreign keys HOT 2
- `CommandChecker` seems to fail on indexes using expressions rather than columns HOT 3
- Difference in results between update_columns_in_batches and update_column_in_batches HOT 4
- Running more than one migration on same table HOT 2
- Attempting to update `pg_catalog` when changing type of not null column w/o a default HOT 5
- Ability to run custom cast logic in column type change HOT 7
- understanding background migrations HOT 3
- `finalize_column_type_change` fails with JSONB indexes
- Copying index when a JSONB index is present on a table is still broken HOT 6
- Dropping support for older ruby/rails HOT 1
- Index name too long error on finalize_column_type_change migration HOT 9
- Question on 'Adding multiple foreign keys': why does adding multiple FKeys block reads on all involved tables? HOT 7
- `add_reference_concurrently` doesn't check for mismatching reference column types
- Clarity on lock retry behaviour inside and outside transactions HOT 4
- Bad background migration behaviour when backfilling primary key id's HOT 2
- Clean up does not remove temporary indexes created HOT 4
- `remove_column` triggers check even when wrapped in `safety_assured` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from online_migrations.