Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INSERT's are failing for long table names 115 #116

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/pg_online_schema_change/orchestrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def setup_store
Store.set(:operation_type_column, "operation_type_#{pgosc_identifier}")
Store.set(:trigger_time_column, "trigger_time_#{pgosc_identifier}")
Store.set(:audit_table_pk, "at_#{pgosc_identifier}_id")
Store.set(:audit_table_pk_sequence, "#{audit_table}_#{audit_table_pk}_seq")
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) || "")

Expand Down Expand Up @@ -109,6 +108,8 @@ def setup_audit_table!
SQL

Query.run(client.connection, sql)

Store.set(:audit_table_pk_sequence, Query.get_sequence_name(client, audit_table, audit_table_pk))
end

def setup_trigger!
Expand Down
12 changes: 12 additions & 0 deletions lib/pg_online_schema_change/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ def get_indexes_for(client, table)
indexes
end

# fetches the sequence name of a table and column combination
def get_sequence_name(client, table, column)
query = <<~SQL
SELECT pg_get_serial_sequence('#{table}', '#{column}');
SQL

run(client.connection, query) { |result| result.map { |row|
row["pg_get_serial_sequence"]
}
}.first
end

def get_triggers_for(client, table)
query = <<~SQL
SELECT pg_get_triggerdef(oid) as tdef FROM pg_trigger
Expand Down
23 changes: 23 additions & 0 deletions spec/lib/orchestrate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@
assertions: [{ count: 1, data: [{ "reloptions" => "{autovacuum_enabled=false}" }] }],
)
end

describe "when table has a long name" do
let(:client) do
options =
client_options.to_h.merge(alter_statement: "ALTER TABLE this_is_a_table_with_a_very_long_name ADD COLUMN \"user_id\" int;")
client_options = Struct.new(*options.keys).new(*options.values)
PgOnlineSchemaChange::Client.new(client_options)
end

before do
allow(PgOnlineSchemaChange::Client).to receive(:new).and_return(client)
described_class.setup!(client_options)
setup_tables(client)
end

it "successfully" do
described_class.setup_audit_table!

sequence_name = PgOnlineSchemaChange::Query.get_sequence_name(client, described_class.audit_table, described_class.audit_table_pk)
expect(described_class.audit_table_pk_sequence).to eq(sequence_name)
end
end

end

describe ".setup_trigger!" do
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@
"constraint_validated" => "t",
"definition" => "FOREIGN KEY (book_id) REFERENCES books(user_id)",
},
{
"table_on" => "this_is_a_table_with_a_very_long_name",
"table_from" => "-",
"constraint_type" => "p",
"constraint_name" => "this_is_a_table_with_a_very_long_name_pkey",
"constraint_validated" => "t",
"definition" => "PRIMARY KEY (id)",
},
]

expect(described_class.get_all_constraints_for(client)).to eq(result)
Expand Down
5 changes: 5 additions & 0 deletions spec/support/database_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def new_dummy_table_sql
"createdOn" TIMESTAMP NOT NULL,
last_login TIMESTAMP
);

CREATE TABLE IF NOT EXISTS #{schema}.this_is_a_table_with_a_very_long_name (
id serial PRIMARY KEY,
"createdOn" TIMESTAMP NOT NULL
);

ALTER ROLE jamesbond SET statement_timeout = '60s';
ALTER ROLE jamesbond SET lock_timeout = '60s';
Expand Down