Skip to content

Commit

Permalink
Merge pull request #249 from ifad/chore/squish-sql-heredocs
Browse files Browse the repository at this point in the history
Chore/squish sql heredocs
  • Loading branch information
tagliala authored Nov 1, 2023
2 parents 66bab95 + f22b91b commit 6f2ebb8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
7 changes: 0 additions & 7 deletions .rubocop_todo.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions lib/chrono_model/adapter/ddl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def chrono_history_table_ddl(table)
parent = "#{TEMPORAL_SCHEMA}.#{table}"
p_pkey = primary_key(parent)

execute <<-SQL
execute <<-SQL.squish
CREATE TABLE #{table} (
hid BIGSERIAL PRIMARY KEY,
validity tsrange NOT NULL,
Expand All @@ -82,7 +82,7 @@ def remove_history_validity_constraint(table, options = {})
# allow setting the PK to a specific value (think migration scenario).
#
def chrono_create_INSERT_trigger(table, pk, current, history, fields, values)
execute <<-SQL.strip_heredoc
execute <<-SQL.strip_heredoc # rubocop:disable Rails/SquishedSQLHeredocs
CREATE OR REPLACE FUNCTION chronomodel_#{table}_insert() RETURNS TRIGGER AS $$
BEGIN
#{insert_sequence_sql(pk, current)} INTO #{current} ( #{pk}, #{fields} )
Expand Down Expand Up @@ -135,7 +135,7 @@ def chrono_create_UPDATE_trigger(table, pk, current, history, fields, values, op

journal &= columns

execute <<-SQL.strip_heredoc
execute <<-SQL.strip_heredoc # rubocop:disable Rails/SquishedSQLHeredocs
CREATE OR REPLACE FUNCTION chronomodel_#{table}_update() RETURNS TRIGGER AS $$
DECLARE _now timestamp;
DECLARE _hid integer;
Expand Down Expand Up @@ -189,7 +189,7 @@ def chrono_create_UPDATE_trigger(table, pk, current, history, fields, values, op
# DELETEd in the same transaction.
#
def chrono_create_DELETE_trigger(table, pk, current, history)
execute <<-SQL.strip_heredoc
execute <<-SQL.strip_heredoc # rubocop:disable Rails/SquishedSQLHeredocs
CREATE OR REPLACE FUNCTION chronomodel_#{table}_delete() RETURNS TRIGGER AS $$
DECLARE _now timestamp;
BEGIN
Expand Down Expand Up @@ -226,7 +226,7 @@ def insert_sequence_sql(pk, current)
seq = pk_and_sequence_for(current)
return 'INSERT' if seq.blank?

<<-SQL.strip
<<-SQL.strip # rubocop:disable Rails/SquishedSQLHeredocs
IF NEW.#{pk} IS NULL THEN
NEW.#{pk} := nextval('#{seq.last}');
END IF;
Expand Down
6 changes: 3 additions & 3 deletions lib/chrono_model/adapter/indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def add_temporal_indexes(table, range, options = {})
temporal_index_names(table, range, options)

chrono_alter_index(table, options) do
execute <<-SQL
execute <<-SQL.squish
CREATE INDEX #{range_idx} ON #{table} USING gist ( #{range} )
SQL

Expand Down Expand Up @@ -53,7 +53,7 @@ def add_timeline_consistency_constraint(table, range, options = {})
id = options[:id] || primary_key(table)

chrono_alter_constraint(table, options) do
execute <<-SQL
execute <<-SQL.squish
ALTER TABLE #{table} ADD CONSTRAINT #{name}
EXCLUDE USING gist ( #{id} WITH =, #{range} WITH && )
SQL
Expand All @@ -64,7 +64,7 @@ def remove_timeline_consistency_constraint(table, options = {})
name = timeline_consistency_constraint_name(table)

chrono_alter_constraint(table, options) do
execute <<-SQL
execute <<-SQL.squish
ALTER TABLE #{table} DROP CONSTRAINT #{name}
SQL
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chrono_model/adapter/upgrade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def chrono_upgrade_from_postgres_v90(table_name)
p_pkey = primary_key(table_name)

execute "ALTER TABLE #{history_table} ADD COLUMN validity tsrange;"
execute <<-SQL
execute <<-SQL.squish
UPDATE #{history_table} SET validity = tsrange(valid_from,
CASE WHEN extract(year from valid_to) = 9999 THEN NULL
ELSE valid_to
Expand Down

0 comments on commit 6f2ebb8

Please sign in to comment.