Skip to content
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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/scenic/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Copy link

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]

end

def full_path
Rails.root.join(path)
end
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The 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
Expand Down
2 changes: 2 additions & 0 deletions lib/scenic/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Scenic
# @see Scenic.load
class Railtie < Rails::Railtie
initializer "scenic.load" do
Rails.application.config.paths.add("db/views")
Copy link

Choose a reason for hiding this comment

The 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
Expand Down
Empty file.
1 change: 1 addition & 0 deletions spec/fixtures/db_views/searches_v01.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT text 'Hi' as greeting
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SELECT text 'Hi' as greeting
SELECT text 'Hi' as greeting

52 changes: 45 additions & 7 deletions spec/scenic/definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,51 @@

module Scenic
describe Definition do
describe "definition_path" do
it "raises an error if file cant be found" do
Copy link

Choose a reason for hiding this comment

The 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)
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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)
Copy link

Choose a reason for hiding this comment

The 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/
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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)
Copy link

Choose a reason for hiding this comment

The 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)
Copy link

Choose a reason for hiding this comment

The 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)
Copy link

Choose a reason for hiding this comment

The 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

Expand Down Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The 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__)
Copy link

Choose a reason for hiding this comment

The 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).
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Rails.application.config.paths["db/views"] << fixtures_path
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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
16 changes: 8 additions & 8 deletions spec/scenic/statements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ module Scenic
end

it "creates a view from a text definition" do
sql_definition = "a defintion"
sql_definition = "a definition"
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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.
Metrics/LineLength: Line is too long. [94/80]

version = 1
definition_stub = instance_double("Definition", to_sql: "foo")
allow(Definition).to receive(:new).
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The 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"
Copy link

Choose a reason for hiding this comment

The 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.
Metrics/LineLength: Line is too long. [81/80]

end.to raise_error ArgumentError
end
end
Expand Down Expand Up @@ -108,7 +108,7 @@ module Scenic
end

it "updates a view from a text definition" do
sql_definition = "a defintion"
sql_definition = "a definition"
Copy link

Choose a reason for hiding this comment

The 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)

Expand Down Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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")
Copy link

Choose a reason for hiding this comment

The 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.
Layout/MultilineMethodCallBraceLayout: Closing method call brace must be on the line after the last argument when opening brace is on a separate line from the first argument.

end.to raise_error ArgumentError, /cannot both be set/
end
end
Expand Down