Skip to content

Commit

Permalink
Reproduce Rails PG adapter changes (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
owst authored Dec 5, 2024
1 parent 3a5477d commit 90a76e4
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
end
end

# Returns the current ID of a table's sequence.
def last_insert_id_result(sequence_name)
internal_exec_query("SELECT currval(#{quote(sequence_name)})", "SQL")
end

# Begins a transaction.
def begin_db_transaction
execute 'BEGIN'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,17 @@ def schema_search_path
end

# Returns the sequence name for a table's primary key or some other specified key.
def default_sequence_name(table_name, pk = nil) # :nodoc:
result = serial_sequence(table_name, pk || 'id')
def default_sequence_name(table_name, pk = "id") # :nodoc:
result = serial_sequence(table_name, pk)
return nil unless result

Utils.extract_schema_qualified_name(result).to_s
rescue ActiveRecord::StatementInvalid
Redshift::Name.new(nil, "#{table_name}_#{pk || 'id'}_seq").to_s
Redshift::Name.new(nil, "#{table_name}_#{pk}_seq").to_s
end

def serial_sequence(table, column)
select_value("SELECT pg_get_serial_sequence('#{table}', '#{column}')", 'SCHEMA')
select_value("SELECT pg_get_serial_sequence(#{quote(table)}, #{quote(column)})", 'SCHEMA')
end

def set_pk_sequence!(table, value); end
Expand Down
9 changes: 3 additions & 6 deletions lib/active_record/connection_adapters/redshift_7_0_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -616,10 +616,6 @@ def configure_connection
end
end

def last_insert_id_result(sequence_name) # :nodoc:
exec_query("SELECT currval('#{sequence_name}')", 'SQL')
end

# Returns the list of a table's column names, data types, and default values.
#
# The underlying query is roughly:
Expand Down Expand Up @@ -651,8 +647,9 @@ def column_definitions(table_name) # :nodoc:
end

def extract_table_ref_from_insert_sql(sql)
sql[/into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im]
Regexp.last_match(1)&.strip
if sql =~ /into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im
$1.delete('"').strip
end
end

def arel_visitor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,17 @@ def schema_search_path
end

# Returns the sequence name for a table's primary key or some other specified key.
def default_sequence_name(table_name, pk = nil) # :nodoc:
result = serial_sequence(table_name, pk || 'id')
def default_sequence_name(table_name, pk = 'id') # :nodoc:
result = serial_sequence(table_name, pk)
return nil unless result

Utils.extract_schema_qualified_name(result).to_s
rescue ActiveRecord::StatementInvalid
Redshift::Name.new(nil, "#{table_name}_#{pk || 'id'}_seq").to_s
Redshift::Name.new(nil, "#{table_name}_#{pk}_seq").to_s
end

def serial_sequence(table, column)
select_value("SELECT pg_get_serial_sequence('#{table}', '#{column}')", 'SCHEMA')
select_value("SELECT pg_get_serial_sequence(#{quote(table)}, #{quote(column)})", 'SCHEMA')
end

def set_pk_sequence!(table, value); end
Expand Down
10 changes: 0 additions & 10 deletions lib/active_record/connection_adapters/redshift_7_1_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,6 @@ def reconfigure_connection_timezone
end
end

def last_insert_id_result(sequence_name)
# :nodoc:
exec_query("SELECT currval('#{sequence_name}')", 'SQL')
end

# Returns the list of a table's column names, data types, and default values.
#
# The underlying query is roughly:
Expand Down Expand Up @@ -725,11 +720,6 @@ def column_definitions(table_name)
END_SQL
end

def extract_table_ref_from_insert_sql(sql)
sql[/into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im]
Regexp.last_match(1)&.strip
end

def arel_visitor
Arel::Visitors::PostgreSQL.new(self)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,17 @@ def schema_search_path
end

# Returns the sequence name for a table's primary key or some other specified key.
def default_sequence_name(table_name, pk = nil) # :nodoc:
result = serial_sequence(table_name, pk || 'id')
def default_sequence_name(table_name, pk = 'id') # :nodoc:
result = serial_sequence(table_name, pk)
return nil unless result

Utils.extract_schema_qualified_name(result).to_s
rescue ActiveRecord::StatementInvalid
Redshift::Name.new(nil, "#{table_name}_#{pk || 'id'}_seq").to_s
Redshift::Name.new(nil, "#{table_name}_#{pk}_seq").to_s
end

def serial_sequence(table, column)
select_value("SELECT pg_get_serial_sequence('#{table}', '#{column}')", 'SCHEMA')
select_value("SELECT pg_get_serial_sequence(#{quote(table)}, #{quote(column)})".tap { puts _1 }, 'SCHEMA')
end

def set_pk_sequence!(table, value); end
Expand Down
10 changes: 0 additions & 10 deletions lib/active_record/connection_adapters/redshift_7_2_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,6 @@ def reconfigure_connection_timezone
end
end

def last_insert_id_result(sequence_name)
# :nodoc:
exec_query("SELECT currval('#{sequence_name}')", 'SQL')
end

# Returns the list of a table's column names, data types, and default values.
#
# The underlying query is roughly:
Expand Down Expand Up @@ -725,11 +720,6 @@ def column_definitions(table_name)
END_SQL
end

def extract_table_ref_from_insert_sql(sql)
sql[/into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im]
Regexp.last_match(1)&.strip
end

def arel_visitor
Arel::Visitors::PostgreSQL.new(self)
end
Expand Down

0 comments on commit 90a76e4

Please sign in to comment.