Skip to content

Commit 34aa315

Browse files
committed
fix: do not drop functions in the upgrade migration
Fixes #258
1 parent b7382f1 commit 34aa315

File tree

4 files changed

+22
-52
lines changed

4 files changed

+22
-52
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## master (unreleased)
44

5+
- Don't drop functions in the upgrade migration. ([@palkan][])
6+
57
## 1.4.0 (2025-05-09) 🎇
68

79
- Support Rails 7.2 ([@atomaka][])

lib/generators/logidze/install/templates/migration.rb.erb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
class <%= @migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
22
def up
3-
<%- if update? -%>
4-
# Drop legacy functions (<1.0)
5-
execute <<~SQL
6-
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb);
7-
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb);
8-
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb, text[]);
9-
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb, text[]);
10-
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb, timestamp with time zone, text[]);
11-
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb, text, text[]);
12-
DROP FUNCTION IF EXISTS logidze_exclude_keys(jsonb, VARIADIC text[]);
13-
DROP FUNCTION IF EXISTS logidze_compact_history(jsonb);
14-
SQL
15-
16-
<%- end -%>
173
execute <<~SQL
184
<%- function_definitions.each do |f| -%>
195
<%= inject_sql("#{f.name}.sql", indent: 6) %>
Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,22 @@
11
class <%= @migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
22
def change
3-
<%- if update? -%>
4-
reversible do |dir|
5-
dir.up do
6-
# Drop legacy functions (<1.0)
7-
execute <<~SQL
8-
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb);
9-
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb);
10-
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb, text[]);
11-
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb, text[]);
12-
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb, timestamp with time zone, text[]);
13-
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb, text, text[]);
14-
DROP FUNCTION IF EXISTS logidze_exclude_keys(jsonb, VARIADIC text[]);
15-
DROP FUNCTION IF EXISTS logidze_compact_history(jsonb);
16-
SQL
17-
end
18-
end
19-
20-
<%- end -%>
213
<%- function_definitions.each do |f| -%>
224
<%- previous_version = previous_version_for(f.name) -%>
235
<%- if previous_version -%>
246
<%- if previous_version != f.version -%>
25-
update_function :<%= f.name %>, version: <%= f.version %>, revert_to_version: <%= previous_version %>
26-
<%- end -%>
27-
<%- else -%>
287
reversible do |dir|
298
dir.up do
309
create_function :<%= f.name %>, version: <%= f.version %>
3110
end
3211

3312
dir.down do
34-
execute "DROP FUNCTION IF EXISTS <%= f.name %>(<%= f.signature %>) CASCADE"
13+
create_function :<%= f.name %>, version: <%= previous_version %>
3514
end
3615
end
37-
38-
<%- end -%>
16+
<%- end %>
17+
<%- else -%>
18+
create_function :<%= f.name %>, version: <%= f.version %>
19+
<%- end %>
3920
<%- end -%>
4021
end
4122
end

spec/generators/install_generator_spec.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@
4949
is_expected.to contain("create_function :logidze_filter_keys, version: 1")
5050
is_expected.to contain("create_function :logidze_compact_history, version: 1")
5151
is_expected.to contain("create_function :logidze_capture_exception, version: 1")
52-
53-
is_expected.to contain("DROP FUNCTION IF EXISTS logidze_logger")
54-
is_expected.to contain("DROP FUNCTION IF EXISTS logidze_logger_after")
55-
is_expected.to contain("DROP FUNCTION IF EXISTS logidze_snapshot")
56-
is_expected.to contain("DROP FUNCTION IF EXISTS logidze_version")
57-
is_expected.to contain("DROP FUNCTION IF EXISTS logidze_filter_keys")
58-
is_expected.to contain("DROP FUNCTION IF EXISTS logidze_compact_history")
59-
is_expected.to contain("DROP FUNCTION IF EXISTS logidze_capture_exception")
6052
end
6153

6254
it "creates function files" do
@@ -145,12 +137,21 @@
145137
run_generator(args)
146138

147139
is_expected.to be_a_file
148-
is_expected.to contain("update_function :logidze_version, version: 2, revert_to_version: 3")
149-
is_expected.to contain("update_function :logidze_snapshot, version: 3, revert_to_version: 4")
150-
is_expected.not_to contain("update_function :logidze_filter_keys")
151-
is_expected.to contain("update_function :logidze_compact_history, version: 1, revert_to_version: 5")
152-
is_expected.to contain("update_function :logidze_logger, version: 5, revert_to_version: 7")
153-
is_expected.to contain("update_function :logidze_logger_after, version: 5, revert_to_version: 7")
140+
is_expected.to contain("create_function :logidze_version, version: 2")
141+
is_expected.to contain("create_function :logidze_version, version: 3")
142+
143+
is_expected.to contain("create_function :logidze_snapshot, version: 3")
144+
is_expected.to contain("create_function :logidze_snapshot, version: 4")
145+
146+
is_expected.not_to contain("create_function :logidze_filter_keys")
147+
is_expected.to contain("create_function :logidze_compact_history, version: 1")
148+
149+
is_expected.to contain("create_function :logidze_logger, version: 5")
150+
is_expected.to contain("create_function :logidze_logger, version: 7")
151+
152+
is_expected.to contain("create_function :logidze_logger_after, version: 5")
153+
is_expected.to contain("create_function :logidze_logger_after, version: 7")
154+
154155
is_expected.to contain("create_function :logidze_capture_exception")
155156
end
156157

0 commit comments

Comments
 (0)