-
-
Notifications
You must be signed in to change notification settings - Fork 228
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
Support multiple db/views paths #237
base: main
Are you sure you want to change the base?
Changes from all 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,17 @@ def initialize(name, version) | |
end | ||
|
||
def to_sql | ||
File.read(full_path).tap do |content| | ||
File.read(definition_path).tap do |content| | ||
if content.empty? | ||
raise "Define view query in #{path} before migrating." | ||
end | ||
end | ||
end | ||
|
||
def definition_path | ||
resolve_definition || raise("Unable to locate #{filename} in #{views_paths}") | ||
end | ||
|
||
def full_path | ||
Rails.root.join(path) | ||
end | ||
|
@@ -28,6 +32,16 @@ def version | |
|
||
private | ||
|
||
def resolve_definition | ||
views_paths.flat_map do |directory| | ||
Dir.glob("#{directory}/**/#{filename}") | ||
end.first | ||
end | ||
|
||
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 | ||||||
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 "definition_path" do | ||
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.definition_path | ||
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.definition_path | ||
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.definition_path).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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,15 +22,15 @@ module Scenic | |
end | ||
|
||
it "creates a view from a text definition" do | ||
sql_definition = "a defintion" | ||
sql_definition = "a definition" | ||
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. |
||
|
||
connection.create_view(:views, sql_definition: sql_definition) | ||
|
||
expect(Scenic.database).to have_received(:create_view) | ||
.with(:views, sql_definition) | ||
end | ||
|
||
it "creates version 1 of the view if neither version nor sql_defintion are provided" do | ||
it "creates version 1 of the view if neither version nor sql_definition are provided" 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. |
||
version = 1 | ||
definition_stub = instance_double("Definition", to_sql: "foo") | ||
allow(Definition).to receive(:new). | ||
|
@@ -43,9 +43,9 @@ module Scenic | |
with(:views, definition_stub.to_sql) | ||
end | ||
|
||
it "raises an error if both version and sql_defintion are provided" do | ||
it "raises an error if both version and sql_definition are provided" 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. |
||
expect do | ||
connection.create_view :foo, version: 1, sql_definition: "a defintion" | ||
connection.create_view :foo, version: 1, sql_definition: "a definition" | ||
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.to raise_error ArgumentError | ||
end | ||
end | ||
|
@@ -108,7 +108,7 @@ module Scenic | |
end | ||
|
||
it "updates a view from a text definition" do | ||
sql_definition = "a defintion" | ||
sql_definition = "a definition" | ||
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. |
||
|
||
connection.update_view(:name, sql_definition: sql_definition) | ||
|
||
|
@@ -144,18 +144,18 @@ module Scenic | |
with(:name, definition.to_sql, no_data: true) | ||
end | ||
|
||
it "raises an error if not supplied a version or sql_defintion" do | ||
it "raises an error if not supplied a version or sql_definition" 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. |
||
expect { connection.update_view :views }.to raise_error( | ||
ArgumentError, | ||
/sql_definition or version must be specified/) | ||
end | ||
|
||
it "raises an error if both version and sql_defintion are provided" do | ||
it "raises an error if both version and sql_definition are provided" 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. |
||
expect do | ||
connection.update_view( | ||
:views, | ||
version: 1, | ||
sql_definition: "a defintion") | ||
sql_definition: "a definition") | ||
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.to raise_error ArgumentError, /cannot both be set/ | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/LineLength: Line is too long. [83/80]