Skip to content

Commit

Permalink
Add generator support for side by side
Browse files Browse the repository at this point in the history
Generators now support `--side_by_side` as an option to generate the
materialized view update migration with that option set. I also created
an alias so you can use `--side-by-side` as well.

While I was at it, I added `--no-data` as an alias for `--no_data` and
added test coverage for it too.
  • Loading branch information
derekprior committed Jan 2, 2025
1 parent db30b4f commit 4a9d527
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
28 changes: 27 additions & 1 deletion lib/generators/scenic/materializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ module Materializable
type: :boolean,
required: false,
desc: "Adds WITH NO DATA when materialized view creates/updates",
default: false
default: false,
aliases: ["--no-data"]
class_option :side_by_side,
type: :boolean,
required: false,
desc: "Uses side-by-side strategy to update materialized view",
default: false,
aliases: ["--side-by-side"]
class_option :replace,
type: :boolean,
required: false,
Expand All @@ -35,6 +42,25 @@ def replace_view?
def no_data?
options[:no_data]
end

def side_by_side?
options[:side_by_side]
end

def materialized_view_update_options
set_options = { no_data: no_data?, side_by_side: side_by_side? }

Check failure on line 51 in lib/generators/scenic/materializable.rb

View workflow job for this annotation

GitHub Actions / Lint with Standard

Layout/SpaceInsideHashLiteralBraces: Space inside { detected.

Check failure on line 51 in lib/generators/scenic/materializable.rb

View workflow job for this annotation

GitHub Actions / Lint with Standard

Layout/SpaceInsideHashLiteralBraces: Space inside } detected.
.select { |_, v| v }

if set_options.empty?
"true"
else
string_options = set_options.reduce("") do |memo, (key, value)|
memo + "#{key}: #{value}, "
end

"{ #{string_options.chomp(", ")} }"
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class <%= migration_class_name %> < <%= activerecord_migration_class %>
<%= method_name %> <%= formatted_plural_name %>,
version: <%= version %>,
revert_to_version: <%= previous_version %>,
materialized: <%= no_data? ? "{ no_data: true }" : true %>
materialized: <%= materialized_view_update_options %>
<%- else -%>
<%= method_name %> <%= formatted_plural_name %>, version: <%= version %>, revert_to_version: <%= previous_version %>
<%- end -%>
Expand Down
26 changes: 26 additions & 0 deletions spec/generators/scenic/view/view_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@
end
end

it "sets the no_data option when updating a materialized view" do
with_view_definition("aired_episodes", 1, "hello") do
allow(Dir).to receive(:entries).and_return(["aired_episodes_v01.sql"])

run_generator ["aired_episode", "--materialized", "--no-data"]
migration = migration_file(
"db/migrate/update_aired_episodes_to_version_2.rb"
)
expect(migration).to contain "materialized: { no_data: true }"
expect(migration).not_to contain "side_by_side"
end
end

it "sets the side-by-side option when updating a materialized view" do
with_view_definition("aired_episodes", 1, "hello") do
allow(Dir).to receive(:entries).and_return(["aired_episodes_v01.sql"])

run_generator ["aired_episode", "--materialized", "--side-by-side"]
migration = migration_file(
"db/migrate/update_aired_episodes_to_version_2.rb"
)
expect(migration).to contain "materialized: { side_by_side: true }"
expect(migration).not_to contain "no_data"
end
end

it "uses 'replace_view' instead of 'update_view' if replace flag is set" do
with_view_definition("aired_episodes", 1, "hello") do
allow(Dir).to receive(:entries).and_return(["aired_episodes_v01.sql"])
Expand Down

0 comments on commit 4a9d527

Please sign in to comment.