The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20170918125918_ids_to_bigints.rb 3.8 KiB

Make IdsToBigints (mostly!) non-blocking (#5088) * Make IdsToBigints (mostly!) non-blocking This pulls in GitLab's MigrationHelpers, which include code to make column changes in ways that Postgres can do without locking. In general, this involves creating a new column, adding an index and any foreign keys as appropriate, adding a trigger to keep it populated alongside the old column, and then progressively copying data over to the new column, before removing the old column and replacing it with the new one. A few changes to GitLab's MigrationHelpers were necessary: * Some changes were made to remove dependencies on other GitLab code. * We explicitly wait for index creation before forging ahead on column replacements. * We use different temporary column names, to avoid running into index name length limits. * We rename the generated indices back to what they "should" be after replacing columns. * We rename the generated foreign keys to use the new column names when we had to create them. (This allows the migration to be rolled back without incident.) # Big Scary Warning There are two things here that may trip up large instances: 1. The change for tables' "id" columns is not concurrent. In particular, the stream_entries table may be big, and does not concurrently migrate its id column. (On the other hand, x_id type columns are all concurrent.) 2. This migration will take a long time to run, *but it should not lock tables during that time* (with the exception of the "id" columns as described above). That means this should probably be run in `screen` or some other session that can be run for a long time. Notably, the migration will take *longer* than it would without these changes, but the website will still be responsive during that time. These changes were tested on a relatively large statuses table (256k entries), and the service remained responsive during the migration. Migrations both forward and backward were tested. * Rubocop fixes * MigrationHelpers: Support ID columns in some cases This doesn't work in cases where the ID column is referred to as a foreign key by another table. * MigrationHelpers: support foreign keys for ID cols Note that this does not yet support foreign keys on non-primary-key columns, but Mastodon also doesn't yet have any that we've needed to migrate. This means we can perform fully "concurrent" migrations to change ID column types, and the IdsToBigints migration can happen with effectively no downtime. (A few operations require a transaction, such as renaming columns or deleting them, but these transactions should not block for noticeable amounts of time.) The algorithm for generating foreign key names has changed with this, and therefore all of those changed in schema.rb. * Provide status, allow for interruptions The MigrationHelpers now allow restarting the rename of a column if it was interrupted, by removing the old "new column" and re-starting the process. Along with this, they now provide status updates on the changes which are happening, as well as indications about when the changes can be safely interrupted (when there are at least 10 seconds estimated to be left before copying data is complete). The IdsToBigints migration now also sorts the columns it migrates by size, starting with the largest tables. This should provide administrators a worst-case scenario estimate for the length of migrations: each successive change will get faster, giving admins a chance to abort early on if they need to run the migration later. The idea is that this does not force them to try to time interruptions between smaller migrations. * Fix column sorting in IdsToBigints Not a significant change, but it impacts the order of columns in the database and db/schema.rb. * Actually pause before IdsToBigints
6 years ago
Make IdsToBigints (mostly!) non-blocking (#5088) * Make IdsToBigints (mostly!) non-blocking This pulls in GitLab's MigrationHelpers, which include code to make column changes in ways that Postgres can do without locking. In general, this involves creating a new column, adding an index and any foreign keys as appropriate, adding a trigger to keep it populated alongside the old column, and then progressively copying data over to the new column, before removing the old column and replacing it with the new one. A few changes to GitLab's MigrationHelpers were necessary: * Some changes were made to remove dependencies on other GitLab code. * We explicitly wait for index creation before forging ahead on column replacements. * We use different temporary column names, to avoid running into index name length limits. * We rename the generated indices back to what they "should" be after replacing columns. * We rename the generated foreign keys to use the new column names when we had to create them. (This allows the migration to be rolled back without incident.) # Big Scary Warning There are two things here that may trip up large instances: 1. The change for tables' "id" columns is not concurrent. In particular, the stream_entries table may be big, and does not concurrently migrate its id column. (On the other hand, x_id type columns are all concurrent.) 2. This migration will take a long time to run, *but it should not lock tables during that time* (with the exception of the "id" columns as described above). That means this should probably be run in `screen` or some other session that can be run for a long time. Notably, the migration will take *longer* than it would without these changes, but the website will still be responsive during that time. These changes were tested on a relatively large statuses table (256k entries), and the service remained responsive during the migration. Migrations both forward and backward were tested. * Rubocop fixes * MigrationHelpers: Support ID columns in some cases This doesn't work in cases where the ID column is referred to as a foreign key by another table. * MigrationHelpers: support foreign keys for ID cols Note that this does not yet support foreign keys on non-primary-key columns, but Mastodon also doesn't yet have any that we've needed to migrate. This means we can perform fully "concurrent" migrations to change ID column types, and the IdsToBigints migration can happen with effectively no downtime. (A few operations require a transaction, such as renaming columns or deleting them, but these transactions should not block for noticeable amounts of time.) The algorithm for generating foreign key names has changed with this, and therefore all of those changed in schema.rb. * Provide status, allow for interruptions The MigrationHelpers now allow restarting the rename of a column if it was interrupted, by removing the old "new column" and re-starting the process. Along with this, they now provide status updates on the changes which are happening, as well as indications about when the changes can be safely interrupted (when there are at least 10 seconds estimated to be left before copying data is complete). The IdsToBigints migration now also sorts the columns it migrates by size, starting with the largest tables. This should provide administrators a worst-case scenario estimate for the length of migrations: each successive change will get faster, giving admins a chance to abort early on if they need to run the migration later. The idea is that this does not force them to try to time interruptions between smaller migrations. * Fix column sorting in IdsToBigints Not a significant change, but it impacts the order of columns in the database and db/schema.rb. * Actually pause before IdsToBigints
6 years ago
Make IdsToBigints (mostly!) non-blocking (#5088) * Make IdsToBigints (mostly!) non-blocking This pulls in GitLab's MigrationHelpers, which include code to make column changes in ways that Postgres can do without locking. In general, this involves creating a new column, adding an index and any foreign keys as appropriate, adding a trigger to keep it populated alongside the old column, and then progressively copying data over to the new column, before removing the old column and replacing it with the new one. A few changes to GitLab's MigrationHelpers were necessary: * Some changes were made to remove dependencies on other GitLab code. * We explicitly wait for index creation before forging ahead on column replacements. * We use different temporary column names, to avoid running into index name length limits. * We rename the generated indices back to what they "should" be after replacing columns. * We rename the generated foreign keys to use the new column names when we had to create them. (This allows the migration to be rolled back without incident.) # Big Scary Warning There are two things here that may trip up large instances: 1. The change for tables' "id" columns is not concurrent. In particular, the stream_entries table may be big, and does not concurrently migrate its id column. (On the other hand, x_id type columns are all concurrent.) 2. This migration will take a long time to run, *but it should not lock tables during that time* (with the exception of the "id" columns as described above). That means this should probably be run in `screen` or some other session that can be run for a long time. Notably, the migration will take *longer* than it would without these changes, but the website will still be responsive during that time. These changes were tested on a relatively large statuses table (256k entries), and the service remained responsive during the migration. Migrations both forward and backward were tested. * Rubocop fixes * MigrationHelpers: Support ID columns in some cases This doesn't work in cases where the ID column is referred to as a foreign key by another table. * MigrationHelpers: support foreign keys for ID cols Note that this does not yet support foreign keys on non-primary-key columns, but Mastodon also doesn't yet have any that we've needed to migrate. This means we can perform fully "concurrent" migrations to change ID column types, and the IdsToBigints migration can happen with effectively no downtime. (A few operations require a transaction, such as renaming columns or deleting them, but these transactions should not block for noticeable amounts of time.) The algorithm for generating foreign key names has changed with this, and therefore all of those changed in schema.rb. * Provide status, allow for interruptions The MigrationHelpers now allow restarting the rename of a column if it was interrupted, by removing the old "new column" and re-starting the process. Along with this, they now provide status updates on the changes which are happening, as well as indications about when the changes can be safely interrupted (when there are at least 10 seconds estimated to be left before copying data is complete). The IdsToBigints migration now also sorts the columns it migrates by size, starting with the largest tables. This should provide administrators a worst-case scenario estimate for the length of migrations: each successive change will get faster, giving admins a chance to abort early on if they need to run the migration later. The idea is that this does not force them to try to time interruptions between smaller migrations. * Fix column sorting in IdsToBigints Not a significant change, but it impacts the order of columns in the database and db/schema.rb. * Actually pause before IdsToBigints
6 years ago
Make IdsToBigints (mostly!) non-blocking (#5088) * Make IdsToBigints (mostly!) non-blocking This pulls in GitLab's MigrationHelpers, which include code to make column changes in ways that Postgres can do without locking. In general, this involves creating a new column, adding an index and any foreign keys as appropriate, adding a trigger to keep it populated alongside the old column, and then progressively copying data over to the new column, before removing the old column and replacing it with the new one. A few changes to GitLab's MigrationHelpers were necessary: * Some changes were made to remove dependencies on other GitLab code. * We explicitly wait for index creation before forging ahead on column replacements. * We use different temporary column names, to avoid running into index name length limits. * We rename the generated indices back to what they "should" be after replacing columns. * We rename the generated foreign keys to use the new column names when we had to create them. (This allows the migration to be rolled back without incident.) # Big Scary Warning There are two things here that may trip up large instances: 1. The change for tables' "id" columns is not concurrent. In particular, the stream_entries table may be big, and does not concurrently migrate its id column. (On the other hand, x_id type columns are all concurrent.) 2. This migration will take a long time to run, *but it should not lock tables during that time* (with the exception of the "id" columns as described above). That means this should probably be run in `screen` or some other session that can be run for a long time. Notably, the migration will take *longer* than it would without these changes, but the website will still be responsive during that time. These changes were tested on a relatively large statuses table (256k entries), and the service remained responsive during the migration. Migrations both forward and backward were tested. * Rubocop fixes * MigrationHelpers: Support ID columns in some cases This doesn't work in cases where the ID column is referred to as a foreign key by another table. * MigrationHelpers: support foreign keys for ID cols Note that this does not yet support foreign keys on non-primary-key columns, but Mastodon also doesn't yet have any that we've needed to migrate. This means we can perform fully "concurrent" migrations to change ID column types, and the IdsToBigints migration can happen with effectively no downtime. (A few operations require a transaction, such as renaming columns or deleting them, but these transactions should not block for noticeable amounts of time.) The algorithm for generating foreign key names has changed with this, and therefore all of those changed in schema.rb. * Provide status, allow for interruptions The MigrationHelpers now allow restarting the rename of a column if it was interrupted, by removing the old "new column" and re-starting the process. Along with this, they now provide status updates on the changes which are happening, as well as indications about when the changes can be safely interrupted (when there are at least 10 seconds estimated to be left before copying data is complete). The IdsToBigints migration now also sorts the columns it migrates by size, starting with the largest tables. This should provide administrators a worst-case scenario estimate for the length of migrations: each successive change will get faster, giving admins a chance to abort early on if they need to run the migration later. The idea is that this does not force them to try to time interruptions between smaller migrations. * Fix column sorting in IdsToBigints Not a significant change, but it impacts the order of columns in the database and db/schema.rb. * Actually pause before IdsToBigints
6 years ago
Make IdsToBigints (mostly!) non-blocking (#5088) * Make IdsToBigints (mostly!) non-blocking This pulls in GitLab's MigrationHelpers, which include code to make column changes in ways that Postgres can do without locking. In general, this involves creating a new column, adding an index and any foreign keys as appropriate, adding a trigger to keep it populated alongside the old column, and then progressively copying data over to the new column, before removing the old column and replacing it with the new one. A few changes to GitLab's MigrationHelpers were necessary: * Some changes were made to remove dependencies on other GitLab code. * We explicitly wait for index creation before forging ahead on column replacements. * We use different temporary column names, to avoid running into index name length limits. * We rename the generated indices back to what they "should" be after replacing columns. * We rename the generated foreign keys to use the new column names when we had to create them. (This allows the migration to be rolled back without incident.) # Big Scary Warning There are two things here that may trip up large instances: 1. The change for tables' "id" columns is not concurrent. In particular, the stream_entries table may be big, and does not concurrently migrate its id column. (On the other hand, x_id type columns are all concurrent.) 2. This migration will take a long time to run, *but it should not lock tables during that time* (with the exception of the "id" columns as described above). That means this should probably be run in `screen` or some other session that can be run for a long time. Notably, the migration will take *longer* than it would without these changes, but the website will still be responsive during that time. These changes were tested on a relatively large statuses table (256k entries), and the service remained responsive during the migration. Migrations both forward and backward were tested. * Rubocop fixes * MigrationHelpers: Support ID columns in some cases This doesn't work in cases where the ID column is referred to as a foreign key by another table. * MigrationHelpers: support foreign keys for ID cols Note that this does not yet support foreign keys on non-primary-key columns, but Mastodon also doesn't yet have any that we've needed to migrate. This means we can perform fully "concurrent" migrations to change ID column types, and the IdsToBigints migration can happen with effectively no downtime. (A few operations require a transaction, such as renaming columns or deleting them, but these transactions should not block for noticeable amounts of time.) The algorithm for generating foreign key names has changed with this, and therefore all of those changed in schema.rb. * Provide status, allow for interruptions The MigrationHelpers now allow restarting the rename of a column if it was interrupted, by removing the old "new column" and re-starting the process. Along with this, they now provide status updates on the changes which are happening, as well as indications about when the changes can be safely interrupted (when there are at least 10 seconds estimated to be left before copying data is complete). The IdsToBigints migration now also sorts the columns it migrates by size, starting with the largest tables. This should provide administrators a worst-case scenario estimate for the length of migrations: each successive change will get faster, giving admins a chance to abort early on if they need to run the migration later. The idea is that this does not force them to try to time interruptions between smaller migrations. * Fix column sorting in IdsToBigints Not a significant change, but it impacts the order of columns in the database and db/schema.rb. * Actually pause before IdsToBigints
6 years ago
Make IdsToBigints (mostly!) non-blocking (#5088) * Make IdsToBigints (mostly!) non-blocking This pulls in GitLab's MigrationHelpers, which include code to make column changes in ways that Postgres can do without locking. In general, this involves creating a new column, adding an index and any foreign keys as appropriate, adding a trigger to keep it populated alongside the old column, and then progressively copying data over to the new column, before removing the old column and replacing it with the new one. A few changes to GitLab's MigrationHelpers were necessary: * Some changes were made to remove dependencies on other GitLab code. * We explicitly wait for index creation before forging ahead on column replacements. * We use different temporary column names, to avoid running into index name length limits. * We rename the generated indices back to what they "should" be after replacing columns. * We rename the generated foreign keys to use the new column names when we had to create them. (This allows the migration to be rolled back without incident.) # Big Scary Warning There are two things here that may trip up large instances: 1. The change for tables' "id" columns is not concurrent. In particular, the stream_entries table may be big, and does not concurrently migrate its id column. (On the other hand, x_id type columns are all concurrent.) 2. This migration will take a long time to run, *but it should not lock tables during that time* (with the exception of the "id" columns as described above). That means this should probably be run in `screen` or some other session that can be run for a long time. Notably, the migration will take *longer* than it would without these changes, but the website will still be responsive during that time. These changes were tested on a relatively large statuses table (256k entries), and the service remained responsive during the migration. Migrations both forward and backward were tested. * Rubocop fixes * MigrationHelpers: Support ID columns in some cases This doesn't work in cases where the ID column is referred to as a foreign key by another table. * MigrationHelpers: support foreign keys for ID cols Note that this does not yet support foreign keys on non-primary-key columns, but Mastodon also doesn't yet have any that we've needed to migrate. This means we can perform fully "concurrent" migrations to change ID column types, and the IdsToBigints migration can happen with effectively no downtime. (A few operations require a transaction, such as renaming columns or deleting them, but these transactions should not block for noticeable amounts of time.) The algorithm for generating foreign key names has changed with this, and therefore all of those changed in schema.rb. * Provide status, allow for interruptions The MigrationHelpers now allow restarting the rename of a column if it was interrupted, by removing the old "new column" and re-starting the process. Along with this, they now provide status updates on the changes which are happening, as well as indications about when the changes can be safely interrupted (when there are at least 10 seconds estimated to be left before copying data is complete). The IdsToBigints migration now also sorts the columns it migrates by size, starting with the largest tables. This should provide administrators a worst-case scenario estimate for the length of migrations: each successive change will get faster, giving admins a chance to abort early on if they need to run the migration later. The idea is that this does not force them to try to time interruptions between smaller migrations. * Fix column sorting in IdsToBigints Not a significant change, but it impacts the order of columns in the database and db/schema.rb. * Actually pause before IdsToBigints
6 years ago
Make IdsToBigints (mostly!) non-blocking (#5088) * Make IdsToBigints (mostly!) non-blocking This pulls in GitLab's MigrationHelpers, which include code to make column changes in ways that Postgres can do without locking. In general, this involves creating a new column, adding an index and any foreign keys as appropriate, adding a trigger to keep it populated alongside the old column, and then progressively copying data over to the new column, before removing the old column and replacing it with the new one. A few changes to GitLab's MigrationHelpers were necessary: * Some changes were made to remove dependencies on other GitLab code. * We explicitly wait for index creation before forging ahead on column replacements. * We use different temporary column names, to avoid running into index name length limits. * We rename the generated indices back to what they "should" be after replacing columns. * We rename the generated foreign keys to use the new column names when we had to create them. (This allows the migration to be rolled back without incident.) # Big Scary Warning There are two things here that may trip up large instances: 1. The change for tables' "id" columns is not concurrent. In particular, the stream_entries table may be big, and does not concurrently migrate its id column. (On the other hand, x_id type columns are all concurrent.) 2. This migration will take a long time to run, *but it should not lock tables during that time* (with the exception of the "id" columns as described above). That means this should probably be run in `screen` or some other session that can be run for a long time. Notably, the migration will take *longer* than it would without these changes, but the website will still be responsive during that time. These changes were tested on a relatively large statuses table (256k entries), and the service remained responsive during the migration. Migrations both forward and backward were tested. * Rubocop fixes * MigrationHelpers: Support ID columns in some cases This doesn't work in cases where the ID column is referred to as a foreign key by another table. * MigrationHelpers: support foreign keys for ID cols Note that this does not yet support foreign keys on non-primary-key columns, but Mastodon also doesn't yet have any that we've needed to migrate. This means we can perform fully "concurrent" migrations to change ID column types, and the IdsToBigints migration can happen with effectively no downtime. (A few operations require a transaction, such as renaming columns or deleting them, but these transactions should not block for noticeable amounts of time.) The algorithm for generating foreign key names has changed with this, and therefore all of those changed in schema.rb. * Provide status, allow for interruptions The MigrationHelpers now allow restarting the rename of a column if it was interrupted, by removing the old "new column" and re-starting the process. Along with this, they now provide status updates on the changes which are happening, as well as indications about when the changes can be safely interrupted (when there are at least 10 seconds estimated to be left before copying data is complete). The IdsToBigints migration now also sorts the columns it migrates by size, starting with the largest tables. This should provide administrators a worst-case scenario estimate for the length of migrations: each successive change will get faster, giving admins a chance to abort early on if they need to run the migration later. The idea is that this does not force them to try to time interruptions between smaller migrations. * Fix column sorting in IdsToBigints Not a significant change, but it impacts the order of columns in the database and db/schema.rb. * Actually pause before IdsToBigints
6 years ago
Make IdsToBigints (mostly!) non-blocking (#5088) * Make IdsToBigints (mostly!) non-blocking This pulls in GitLab's MigrationHelpers, which include code to make column changes in ways that Postgres can do without locking. In general, this involves creating a new column, adding an index and any foreign keys as appropriate, adding a trigger to keep it populated alongside the old column, and then progressively copying data over to the new column, before removing the old column and replacing it with the new one. A few changes to GitLab's MigrationHelpers were necessary: * Some changes were made to remove dependencies on other GitLab code. * We explicitly wait for index creation before forging ahead on column replacements. * We use different temporary column names, to avoid running into index name length limits. * We rename the generated indices back to what they "should" be after replacing columns. * We rename the generated foreign keys to use the new column names when we had to create them. (This allows the migration to be rolled back without incident.) # Big Scary Warning There are two things here that may trip up large instances: 1. The change for tables' "id" columns is not concurrent. In particular, the stream_entries table may be big, and does not concurrently migrate its id column. (On the other hand, x_id type columns are all concurrent.) 2. This migration will take a long time to run, *but it should not lock tables during that time* (with the exception of the "id" columns as described above). That means this should probably be run in `screen` or some other session that can be run for a long time. Notably, the migration will take *longer* than it would without these changes, but the website will still be responsive during that time. These changes were tested on a relatively large statuses table (256k entries), and the service remained responsive during the migration. Migrations both forward and backward were tested. * Rubocop fixes * MigrationHelpers: Support ID columns in some cases This doesn't work in cases where the ID column is referred to as a foreign key by another table. * MigrationHelpers: support foreign keys for ID cols Note that this does not yet support foreign keys on non-primary-key columns, but Mastodon also doesn't yet have any that we've needed to migrate. This means we can perform fully "concurrent" migrations to change ID column types, and the IdsToBigints migration can happen with effectively no downtime. (A few operations require a transaction, such as renaming columns or deleting them, but these transactions should not block for noticeable amounts of time.) The algorithm for generating foreign key names has changed with this, and therefore all of those changed in schema.rb. * Provide status, allow for interruptions The MigrationHelpers now allow restarting the rename of a column if it was interrupted, by removing the old "new column" and re-starting the process. Along with this, they now provide status updates on the changes which are happening, as well as indications about when the changes can be safely interrupted (when there are at least 10 seconds estimated to be left before copying data is complete). The IdsToBigints migration now also sorts the columns it migrates by size, starting with the largest tables. This should provide administrators a worst-case scenario estimate for the length of migrations: each successive change will get faster, giving admins a chance to abort early on if they need to run the migration later. The idea is that this does not force them to try to time interruptions between smaller migrations. * Fix column sorting in IdsToBigints Not a significant change, but it impacts the order of columns in the database and db/schema.rb. * Actually pause before IdsToBigints
6 years ago
Make IdsToBigints (mostly!) non-blocking (#5088) * Make IdsToBigints (mostly!) non-blocking This pulls in GitLab's MigrationHelpers, which include code to make column changes in ways that Postgres can do without locking. In general, this involves creating a new column, adding an index and any foreign keys as appropriate, adding a trigger to keep it populated alongside the old column, and then progressively copying data over to the new column, before removing the old column and replacing it with the new one. A few changes to GitLab's MigrationHelpers were necessary: * Some changes were made to remove dependencies on other GitLab code. * We explicitly wait for index creation before forging ahead on column replacements. * We use different temporary column names, to avoid running into index name length limits. * We rename the generated indices back to what they "should" be after replacing columns. * We rename the generated foreign keys to use the new column names when we had to create them. (This allows the migration to be rolled back without incident.) # Big Scary Warning There are two things here that may trip up large instances: 1. The change for tables' "id" columns is not concurrent. In particular, the stream_entries table may be big, and does not concurrently migrate its id column. (On the other hand, x_id type columns are all concurrent.) 2. This migration will take a long time to run, *but it should not lock tables during that time* (with the exception of the "id" columns as described above). That means this should probably be run in `screen` or some other session that can be run for a long time. Notably, the migration will take *longer* than it would without these changes, but the website will still be responsive during that time. These changes were tested on a relatively large statuses table (256k entries), and the service remained responsive during the migration. Migrations both forward and backward were tested. * Rubocop fixes * MigrationHelpers: Support ID columns in some cases This doesn't work in cases where the ID column is referred to as a foreign key by another table. * MigrationHelpers: support foreign keys for ID cols Note that this does not yet support foreign keys on non-primary-key columns, but Mastodon also doesn't yet have any that we've needed to migrate. This means we can perform fully "concurrent" migrations to change ID column types, and the IdsToBigints migration can happen with effectively no downtime. (A few operations require a transaction, such as renaming columns or deleting them, but these transactions should not block for noticeable amounts of time.) The algorithm for generating foreign key names has changed with this, and therefore all of those changed in schema.rb. * Provide status, allow for interruptions The MigrationHelpers now allow restarting the rename of a column if it was interrupted, by removing the old "new column" and re-starting the process. Along with this, they now provide status updates on the changes which are happening, as well as indications about when the changes can be safely interrupted (when there are at least 10 seconds estimated to be left before copying data is complete). The IdsToBigints migration now also sorts the columns it migrates by size, starting with the largest tables. This should provide administrators a worst-case scenario estimate for the length of migrations: each successive change will get faster, giving admins a chance to abort early on if they need to run the migration later. The idea is that this does not force them to try to time interruptions between smaller migrations. * Fix column sorting in IdsToBigints Not a significant change, but it impacts the order of columns in the database and db/schema.rb. * Actually pause before IdsToBigints
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. require Rails.root.join('lib', 'mastodon', 'migration_helpers')
  2. class IdsToBigints < ActiveRecord::Migration[5.1]
  3. include Mastodon::MigrationHelpers
  4. disable_ddl_transaction!
  5. def migrate_columns(to_type)
  6. included_columns = [
  7. [:account_domain_blocks, :account_id],
  8. [:account_domain_blocks, :id],
  9. [:accounts, :id],
  10. [:blocks, :account_id],
  11. [:blocks, :id],
  12. [:blocks, :target_account_id],
  13. [:conversation_mutes, :account_id],
  14. [:conversation_mutes, :id],
  15. [:domain_blocks, :id],
  16. [:favourites, :account_id],
  17. [:favourites, :id],
  18. [:favourites, :status_id],
  19. [:follow_requests, :account_id],
  20. [:follow_requests, :id],
  21. [:follow_requests, :target_account_id],
  22. [:follows, :account_id],
  23. [:follows, :id],
  24. [:follows, :target_account_id],
  25. [:imports, :account_id],
  26. [:imports, :id],
  27. [:media_attachments, :account_id],
  28. [:media_attachments, :id],
  29. [:mentions, :account_id],
  30. [:mentions, :id],
  31. [:mutes, :account_id],
  32. [:mutes, :id],
  33. [:mutes, :target_account_id],
  34. [:notifications, :account_id],
  35. [:notifications, :from_account_id],
  36. [:notifications, :id],
  37. [:oauth_access_grants, :application_id],
  38. [:oauth_access_grants, :id],
  39. [:oauth_access_grants, :resource_owner_id],
  40. [:oauth_access_tokens, :application_id],
  41. [:oauth_access_tokens, :id],
  42. [:oauth_access_tokens, :resource_owner_id],
  43. [:oauth_applications, :id],
  44. [:oauth_applications, :owner_id],
  45. [:reports, :account_id],
  46. [:reports, :action_taken_by_account_id],
  47. [:reports, :id],
  48. [:reports, :target_account_id],
  49. [:session_activations, :access_token_id],
  50. [:session_activations, :user_id],
  51. [:session_activations, :web_push_subscription_id],
  52. [:settings, :id],
  53. [:settings, :thing_id],
  54. [:statuses, :account_id],
  55. [:statuses, :application_id],
  56. [:statuses, :in_reply_to_account_id],
  57. [:stream_entries, :account_id],
  58. [:stream_entries, :id],
  59. [:subscriptions, :account_id],
  60. [:subscriptions, :id],
  61. [:tags, :id],
  62. [:users, :account_id],
  63. [:users, :id],
  64. [:web_settings, :id],
  65. [:web_settings, :user_id],
  66. ]
  67. included_columns << [:deprecated_preview_cards, :id] if table_exists?(:deprecated_preview_cards)
  68. # Print out a warning that this will probably take a while.
  69. if $stdout.isatty
  70. say ''
  71. say 'WARNING: This migration may take a *long* time for large instances'
  72. say 'It will *not* lock tables for any significant time, but it may run'
  73. say 'for a very long time. We will pause for 10 seconds to allow you to'
  74. say 'interrupt this migration if you are not ready.'
  75. say ''
  76. say 'This migration has some sections that can be safely interrupted'
  77. say 'and restarted later, and will tell you when those are occurring.'
  78. say ''
  79. say 'For more information, see https://github.com/tootsuite/mastodon/pull/5088'
  80. 10.downto(1) do |i|
  81. say "Continuing in #{i} second#{i == 1 ? '' : 's'}...", true
  82. sleep 1
  83. end
  84. end
  85. tables = included_columns.map(&:first).uniq
  86. table_sizes = {}
  87. # Sort tables by their size
  88. tables.each do |table|
  89. table_sizes[table] = estimate_rows_in_table(table)
  90. end
  91. ordered_columns = included_columns.sort_by do |col_parts|
  92. [-table_sizes[col_parts.first], col_parts.last]
  93. end
  94. ordered_columns.each do |column_parts|
  95. table, column = column_parts
  96. # Skip this if we're resuming and already did this one.
  97. next if column_for(table, column).sql_type == to_type.to_s
  98. change_column_type_concurrently table, column, to_type
  99. cleanup_concurrent_column_type_change table, column
  100. end
  101. end
  102. def up
  103. migrate_columns(:bigint)
  104. end
  105. def down
  106. migrate_columns(:integer)
  107. end
  108. end