departurerb / departure

Percona's pt-online-schema-change runner for ActiveRecord migrations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Errors when dropping FK

zwippie opened this issue · comments

I'm having great troubles removing foreign keys from a table when using departure: the FK to remove is never found.

For investigation purposes, I have created a new Rails project with two simple models with the following migrations:

    create_table :users do |t|
      t.string :name
      t.string :email

      t.timestamps
    end

    create_table :posts do |t|
      t.string :name
      t.text :body
      t.references :user, foreign_key: true

      t.timestamps
    end

After running these migrations and adding some data, add another migration to remove the user column from the posts table:

    remove_reference :posts, :user, index: true, foreign_key: true

This raises the following error:

rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Departure::Error: No foreign keys reference departure_test_development.posts; ignoring --alter-foreign-keys-method.
Error altering new table departure_test_development._posts_new: DBD::mysql::db do failed: Can't DROP 'fk_rails_5b5ddfd518'; check that column/key exists [for Statement "ALTER TABLE departure_test_development._posts_new DROP FOREIGN KEY fk_rails_5b5ddfd518"] at /usr/local/Cellar/percona-toolkit/3.0.4/libexec/bin/pt-online-schema-change line 9142.

: ALTER TABLE posts DROP FOREIGN KEY fk_rails_5b5ddfd518

I have no real clue why this is failing, but according to this article the FK in the temporary table should be prefixed with an extra underscore. Could that be the problem?

Hi @zwippie, I won't be able to dig deeper until next week. Have tried running the ALTER TABLE statement by hand? What's the result?

Hi @sauloperez, I've tried this query by hand (from the db console):

ALTER TABLE `departure_test_development`.`posts` DROP FOREIGN KEY fk_rails_5b5ddfd518;

and that is working as expected, the FK is dropped.

Just to be sure, does the equivalent plain pt-online-schema-change command succeed? You can copy it from the migration's output.

This is the output from the pt-online-schema-change command:

bundle exec rake db:migrate 1 ↵
== 20170928141723 RemoveUserFromPosts: migrating ==============================
-- remove_reference(:posts, :user, {:index=>true, :foreign_key=>true})

-> Running pt-online-schema-change -h localhost -P 3306 -u root [filtered_password] --execute --statistics --alter-foreign-keys-method=auto --no-check-alter D=departure_test_development,t=posts --alter "DROP FOREIGN KEY fk_rails_5b5ddfd518"

No slaves found. See --recursion-method if host Arnolds-iMac.local has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
Operation, tries, wait:
analyze_table, 10, 1
copy_rows, 10, 0.25
create_triggers, 10, 1
drop_triggers, 10, 1
swap_tables, 10, 1
update_foreign_keys, 10, 1
Altering departure_test_development.posts...
Creating new table...
Created new table departure_test_development._posts_new OK.
Altering new table...
2017-09-28T16:20:02 Dropping new table...
2017-09-28T16:20:02 Dropped new table OK.

Event Count

====== =====

INSERT 0

departure_test_development.posts was not altered.

After that, there's the error as logged in the first post.

@zwippie can you check if the fix proposed in #25 work for your case?