Giter VIP home page Giter VIP logo

Comments (6)

ghiculescu avatar ghiculescu commented on June 6, 2024 2

Ahhh we just found this

if foreign_key_exists?(from_table, to_table, **options)
message = +"Foreign key was not created because it already exists " \
"(this can be due to an aborted migration or similar): from_table: #{from_table}, to_table: #{to_table}"
message << ", #{options.inspect}" if options.any?

image

from online_migrations.

camallen avatar camallen commented on June 6, 2024 1

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

online_migrations/README.md

Lines 863 to 876 in 38cee8c

Add the foreign key without validating existing rows, and then validate them in a separate transaction.
```ruby
class AddForeignKeyToProjectsUser < ActiveRecord::Migration[7.1]
disable_ddl_transaction!
def change
add_foreign_key :projects, :users, validate: false
validate_foreign_key :projects, :users
end
end
```
**Note**: If you forget `disable_ddl_transaction!`, the migration will fail.

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.

camallen avatar camallen commented on June 6, 2024 1

@fatkodima thanks for doing this work 💯

from online_migrations.

fatkodima avatar fatkodima commented on June 6, 2024

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.

camallen avatar camallen commented on June 6, 2024

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.

fatkodima avatar fatkodima commented on June 6, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.