|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative '../scripts/copy_data' |
| 4 | + |
| 5 | +class MoveSolidQueueToDedicatedDatabase < ActiveRecord::Migration[7.1] |
| 6 | + include CopyData |
| 7 | + include ActiveRecord::Tasks |
| 8 | + |
| 9 | + class Queue < ApplicationRecord |
| 10 | + self.abstract_class = true |
| 11 | + connects_to database: {writing: :queue} |
| 12 | + end |
| 13 | + |
| 14 | + def up |
| 15 | + create_database(:queue) |
| 16 | + load_queue_schema(database: :queue) |
| 17 | + copy_data(source_connection: connection, target_connection: Queue.connection, operation: :matches, condition: 'solid_queue_%') |
| 18 | + drop_queue_tables(connection:) |
| 19 | + ensure |
| 20 | + Queue.connection&.disconnect! |
| 21 | + end |
| 22 | + |
| 23 | + def down |
| 24 | + load_queue_schema(database: :primary) |
| 25 | + copy_data(source_connection: Queue.connection, target_connection: connection, operation: :matches, condition: 'solid_queue_%') |
| 26 | + drop_queue_tables(connection: Queue.connection) |
| 27 | + rescue StandardError |
| 28 | + Queue.connection&.disconnect! |
| 29 | + else |
| 30 | + Queue.connection&.disconnect! |
| 31 | + drop_database(:queue) |
| 32 | + end |
| 33 | + |
| 34 | + def foreign_key_targets |
| 35 | + %w[ |
| 36 | + solid_queue_jobs |
| 37 | + ] |
| 38 | + end |
| 39 | + |
| 40 | + private |
| 41 | + |
| 42 | + def load_queue_schema(database:) |
| 43 | + with_connection(database:) do |
| 44 | + DatabaseTasks.load_schema(configs_for(:queue), ActiveRecord.schema_format, 'db/queue_schema.rb') |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + def drop_queue_tables(connection:) |
| 49 | + connection.drop_table :solid_queue_semaphores |
| 50 | + connection.drop_table :solid_queue_scheduled_executions |
| 51 | + connection.drop_table :solid_queue_recurring_tasks |
| 52 | + connection.drop_table :solid_queue_recurring_executions |
| 53 | + connection.drop_table :solid_queue_ready_executions |
| 54 | + connection.drop_table :solid_queue_processes |
| 55 | + connection.drop_table :solid_queue_pauses |
| 56 | + connection.drop_table :solid_queue_failed_executions |
| 57 | + connection.drop_table :solid_queue_claimed_executions |
| 58 | + connection.drop_table :solid_queue_blocked_executions |
| 59 | + connection.drop_table :solid_queue_jobs |
| 60 | + end |
| 61 | + |
| 62 | + def configs_for(name) |
| 63 | + ActiveRecord::Base.configurations.configs_for(name: name.to_s, env_name: Rails.env) |
| 64 | + end |
| 65 | + |
| 66 | + def create_database(name) |
| 67 | + database_name = configs_for(name).database |
| 68 | + new_primary_connection.create_database(database_name) |
| 69 | + rescue ActiveRecord::DatabaseAlreadyExists |
| 70 | + # Database already exists, do nothing |
| 71 | + end |
| 72 | + |
| 73 | + def drop_database(name) |
| 74 | + database_name = configs_for(name).database |
| 75 | + new_primary_connection.drop_database(database_name) |
| 76 | + end |
| 77 | + |
| 78 | + def new_primary_connection |
| 79 | + ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new(configs_for(:primary).configuration_hash) |
| 80 | + end |
| 81 | + |
| 82 | + def with_connection(database:) |
| 83 | + # We must not overwrite the `connection`, which is automatically overwritten by establishing a new connection. |
| 84 | + # However, we need to specify another connection, i.e. for loading the schema to the desired database. |
| 85 | + # Hence, we use this monkey patching workaround to change the connection temporary and then revert back. |
| 86 | + klass = database == :queue ? Queue : ApplicationRecord |
| 87 | + DatabaseTasks.alias_method :previous_migration_class, :migration_class |
| 88 | + DatabaseTasks.define_method(:migration_class) { klass } |
| 89 | + yield |
| 90 | + ensure |
| 91 | + DatabaseTasks.alias_method :migration_class, :previous_migration_class |
| 92 | + end |
| 93 | +end |
0 commit comments