diff --git a/lib/pg_online_schema_change/orchestrate.rb b/lib/pg_online_schema_change/orchestrate.rb index 790f75f..1b8a62e 100644 --- a/lib/pg_online_schema_change/orchestrate.rb +++ b/lib/pg_online_schema_change/orchestrate.rb @@ -36,7 +36,10 @@ def setup_store Store.set(:trigger_time_column, "trigger_time_#{pgosc_identifier}") Store.set(:audit_table_pk, "at_#{pgosc_identifier}_id") Store.set(:shadow_table, "pgosc_st_#{client.table.downcase}_#{pgosc_identifier}") - Store.set(:primary_table_storage_parameters, Query.storage_parameters_for(client, client.table_name, true) || "") + Store.set( + :primary_table_storage_parameters, + Query.storage_parameters_for(client, client.table_name, true) || "", + ) Store.set( :referential_foreign_key_statements, @@ -109,7 +112,10 @@ def setup_audit_table! Query.run(client.connection, sql) - Store.set(:audit_table_pk_sequence, Query.get_sequence_name(client, audit_table, audit_table_pk)) + Store.set( + :audit_table_pk_sequence, + Query.get_sequence_name(client, audit_table, audit_table_pk), + ) end def setup_trigger! @@ -270,11 +276,12 @@ def run_analyze! end def validate_constraints! - logger.info("Validating constraints!") - - validate_statements = Query.get_foreign_keys_to_validate(client, client.table_name) - - Query.run(client.connection, validate_statements) + Query + .get_foreign_keys_to_validate(client, client.table_name) + .each do |statement| + logger.info("Validating constraints!", statement: statement) + Query.run(client.connection, statement) + end end def replace_views! diff --git a/lib/pg_online_schema_change/query.rb b/lib/pg_online_schema_change/query.rb index c790589..1218894 100644 --- a/lib/pg_online_schema_change/query.rb +++ b/lib/pg_online_schema_change/query.rb @@ -140,10 +140,9 @@ def get_sequence_name(client, table, column) SELECT pg_get_serial_sequence('#{table}', '#{column}'); SQL - run(client.connection, query) { |result| result.map { |row| - row["pg_get_serial_sequence"] - } - }.first + run(client.connection, query) do |result| + result.map { |row| row["pg_get_serial_sequence"] } + end.first end def get_triggers_for(client, table) @@ -238,11 +237,9 @@ def get_foreign_keys_to_validate(client, table) self_foreign_keys = constraints.select { |row| row["table_on"] == table && row["constraint_type"] == "f" } - [referential_foreign_keys, self_foreign_keys].flatten - .map do |row| - "ALTER TABLE #{row["table_on"]} VALIDATE CONSTRAINT #{row["constraint_name"]};" - end - .join + [referential_foreign_keys, self_foreign_keys].flatten.map do |row| + "ALTER TABLE #{row["table_on"]} VALIDATE CONSTRAINT #{row["constraint_name"]};" + end end def dropped_columns(client) @@ -327,7 +324,7 @@ def view_definitions_for(client, table) definitions = [] run(client.connection, query) do |result| - definitions = result.map { |row| {row["view_name"] => row["view_definition"].strip} } + definitions = result.map { |row| { row["view_name"] => row["view_definition"].strip } } end definitions diff --git a/spec/lib/query_spec.rb b/spec/lib/query_spec.rb index 379b99a..d2c238f 100644 --- a/spec/lib/query_spec.rb +++ b/spec/lib/query_spec.rb @@ -457,9 +457,13 @@ before { setup_tables(client) } it "returns drop and add statements" do - result = - "ALTER TABLE book_audits VALIDATE CONSTRAINT book_audits_book_id_fkey;ALTER TABLE chapters VALIDATE CONSTRAINT chapters_book_id_fkey;ALTER TABLE books VALIDATE CONSTRAINT books_seller_id_fkey;" - expect(described_class.get_foreign_keys_to_validate(client, "books")).to eq(result) + expect(described_class.get_foreign_keys_to_validate(client, "books")).to eq( + [ + "ALTER TABLE book_audits VALIDATE CONSTRAINT book_audits_book_id_fkey;", + "ALTER TABLE chapters VALIDATE CONSTRAINT chapters_book_id_fkey;", + "ALTER TABLE books VALIDATE CONSTRAINT books_seller_id_fkey;", + ], + ) end end @@ -693,11 +697,14 @@ ingest_dummy_data_into_dummy_table(client) described_class.run(client.connection, "reset search_path") result = described_class.view_definitions_for(client, "books") - expect(result).to eq([ - { - "books_view" =>"SELECT books.user_id,\n books.username,\n books.seller_id,\n books.password,\n books.email,\n books.\"createdOn\",\n books.last_login\n FROM test_schema.books\n WHERE (books.seller_id = 1);", - } - ]) + expect(result).to eq( + [ + { + "books_view" => + "SELECT books.user_id,\n books.username,\n books.seller_id,\n books.password,\n books.email,\n books.\"createdOn\",\n books.last_login\n FROM test_schema.books\n WHERE (books.seller_id = 1);", + }, + ], + ) end end