Skip to content

Commit

Permalink
Add flag --replace_view to migration generator (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
danhixon-copa authored Aug 18, 2022
1 parent f415260 commit a4f1c71
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
24 changes: 6 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,18 @@ a new version of it.
This is not desirable when you have complicated hierarchies of views, especially
when some of those views may be materialized and take a long time to recreate.

You can use `replace_view` to generate a CREATE OR REPLACE VIEW SQL statement.

See Postgres documentation on how this works:
http://www.postgresql.org/docs/current/static/sql-createview.html

To start replacing a view run the generator like for a regular change:
You can use `replace_view` to generate a CREATE OR REPLACE VIEW SQL statement instead by adding the `--replace_view` option to the generate command:

```sh
$ rails generate scenic:view search_results
$ rails generate scenic:view search_results --replace_view
create db/views/search_results_v02.sql
create db/migrate/[TIMESTAMP]_update_search_results_to_version_2.rb
```

Now, edit the migration. It should look something like:

```ruby
class UpdateSearchResultsToVersion2 < ActiveRecord::Migration
def change
update_view :search_results, version: 2, revert_to_version: 1
end
end
```
See Postgres documentation on how this works:
http://www.postgresql.org/docs/current/static/sql-createview.html

Update it to use replace view:
The migration will look something like this:

```ruby
class UpdateSearchResultsToVersion2 < ActiveRecord::Migration
Expand All @@ -125,7 +113,7 @@ class UpdateSearchResultsToVersion2 < ActiveRecord::Migration
end
```

Now you can run the migration like normal.
You can run the migration and the view will be replaced instead.

## Can I use this view to back a model?

Expand Down
9 changes: 9 additions & 0 deletions lib/generators/scenic/materializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ module Materializable
required: false,
desc: "Adds WITH NO DATA when materialized view creates/updates",
default: false
class_option :replace_view,
type: :boolean,
required: false,
desc: "Uses replace_view instead of update_view",
default: false
end

private
Expand All @@ -23,6 +28,10 @@ def materialized?
options[:materialized]
end

def replace_view?
options[:replace_view]
end

def no_data?
options[:no_data]
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
class <%= migration_class_name %> < <%= activerecord_migration_class %>
def change
<% method_name = replace_view? ? 'replace_view' : 'update_view' %>
<%- if materialized? -%>
update_view <%= formatted_plural_name %>,
<%= method_name %> <%= formatted_plural_name %>,
version: <%= version %>,
revert_to_version: <%= previous_version %>,
materialized: <%= no_data? ? "{ no_data: true }" : true %>
<%- else -%>
update_view <%= formatted_plural_name %>, version: <%= version %>, revert_to_version: <%= previous_version %>
<%= method_name %> <%= formatted_plural_name %>, version: <%= version %>, revert_to_version: <%= previous_version %>
<%- end -%>
end
end
12 changes: 12 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,18 @@
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"])

run_generator ["aired_episode", "--replace_view"]
migration = migration_file(
"db/migrate/update_aired_episodes_to_version_2.rb",
)
expect(migration).to contain "replace_view"
end
end

context "for views created in a schema other than 'public'" do
it "creates a view definition" do
view_definition = file("db/views/non_public_searches_v01.sql")
Expand Down

0 comments on commit a4f1c71

Please sign in to comment.