-
-
Notifications
You must be signed in to change notification settings - Fork 240
Support multiple db/views paths #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
35ac4dc
7bd3077
97f40b2
b0c56b4
4bfafd8
168769f
ff63ad0
ff5382a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,13 +7,25 @@ def initialize(name, version) | |||||
| end | ||||||
|
|
||||||
| def to_sql | ||||||
| File.read(full_path).tap do |content| | ||||||
| File.read(find_definition).tap do |content| | ||||||
| if content.empty? | ||||||
| raise "Define view query in #{path} before migrating." | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| def find_definition | ||||||
|
derekprior marked this conversation as resolved.
Outdated
|
||||||
| definition = views_paths.flat_map do |directory| | ||||||
|
derekprior marked this conversation as resolved.
Outdated
|
||||||
| Dir.glob("#{directory}/**/#{filename}") | ||||||
| end.first | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be a find, but then it would only return the directory that the view exists under. This actually resolves the first matching file that is found under each view_path directory. Plus it could be possibly be nested. |
||||||
|
|
||||||
| unless definition | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be more consistent like this:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/IfUnlessModifier: Favor modifier unless usage when having a single-line body. Another good alternative is the usage of control flow &&/||. |
||||||
| raise "Unable to locate #{filename} in #{views_paths}" | ||||||
| end | ||||||
|
|
||||||
| definition | ||||||
| end | ||||||
|
|
||||||
| def full_path | ||||||
| Rails.root.join(path) | ||||||
| end | ||||||
|
|
@@ -28,6 +40,10 @@ def version | |||||
|
|
||||||
| private | ||||||
|
|
||||||
| def views_paths | ||||||
| Rails.application.config.paths["db/views"].expanded | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||||||
| end | ||||||
|
|
||||||
| def filename | ||||||
| "#{@name}_v#{version}.sql" | ||||||
| end | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ module Scenic | |
| # @see Scenic.load | ||
| class Railtie < Rails::Railtie | ||
| initializer "scenic.load" do | ||
| Rails.application.config.paths.add("db/views") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
|
|
||
| ActiveSupport.on_load :active_record do | ||
| Scenic.load | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1 @@ | ||||||
| SELECT text 'Hi' as greeting | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,22 +2,51 @@ | |
|
|
||
| module Scenic | ||
| describe Definition do | ||
| describe "find_definition" do | ||
|
derekprior marked this conversation as resolved.
Outdated
|
||
| it "raises an error if file cant be found" do | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| definition = Definition.new("not_valid", 1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
|
|
||
| expect do | ||
| definition.find_definition | ||
| end.to raise_error RuntimeError, /Unable to locate not_valid_v01.sql/ | ||
| end | ||
|
|
||
| it "looks inside Rails.root db/views" do | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| definition = Definition.new("not_valid", 1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
|
|
||
| expect do | ||
| definition.find_definition | ||
| end.to raise_error RuntimeError, /spec\/dummy\/db\/views/ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/RegexpLiteral: Use %r around regular expression. |
||
| end | ||
|
|
||
| it "looks inside configured additional paths" do | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| with_views_fixtures do | ||
| definition = Definition.new("empty_view", 1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
|
|
||
| expect(definition.find_definition).to be | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "to_sql" do | ||
| it "returns the content of a view definition" do | ||
| sql_definition = "SELECT text 'Hi' as greeting" | ||
| allow(File).to receive(:read).and_return(sql_definition) | ||
|
|
||
| definition = Definition.new("searches", 1) | ||
| with_views_fixtures do | ||
| definition = Definition.new("searches", 1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
|
|
||
| expect(definition.to_sql).to eq sql_definition | ||
| expect(definition.to_sql).to eq sql_definition | ||
| end | ||
| end | ||
|
|
||
| it "raises an error if the file is empty" do | ||
| allow(File).to receive(:read).and_return("") | ||
| definition = Definition.new("empty_view", 1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
|
|
||
| expect do | ||
| Definition.new("searches", 1).to_sql | ||
| end.to raise_error RuntimeError | ||
| with_views_fixtures do | ||
| expect do | ||
| definition.to_sql | ||
| end.to raise_error RuntimeError | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -52,5 +81,14 @@ module Scenic | |
| expect(definition.version).to eq "15" | ||
| end | ||
| end | ||
|
|
||
| def with_views_fixtures | ||
| original = Rails.application.config.paths["db/views"].to_a | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| fixtures_path = File.expand_path("../../fixtures/db_views", __FILE__) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/ExpandPathArguments: Use expand_path('../fixtures/db_views', dir) instead of expand_path('../../fixtures/db_views', FILE). |
||
| Rails.application.config.paths["db/views"] << fixtures_path | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| yield | ||
| ensure | ||
| Rails.application.config.paths["db/views"] = original | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| end | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.