Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions lib/scenic/definition.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Scenic
# @api private
class Definition
def initialize(name, version)
def initialize(name, version, root_path: Rails.root)
@name = name
@version = version.to_i
@root_path = root_path.is_a?(String) ? Pathname.new(root_path) : root_path
end

def to_sql
Expand All @@ -15,7 +16,7 @@ def to_sql
end

def full_path
Rails.root.join(path)
@root_path.join(path)
end

def path
Expand Down
16 changes: 8 additions & 8 deletions lib/scenic/statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Statements
# SELECT * FROM users WHERE users.active = 't'
# SQL
#
def create_view(name, version: nil, sql_definition: nil, materialized: false)
def create_view(name, version: nil, sql_definition: nil, materialized: false, root_path: Rails.root)
Copy link
Copy Markdown

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. [104/80]

if version.present? && sql_definition.present?
raise(
ArgumentError,
Expand All @@ -34,7 +34,7 @@ def create_view(name, version: nil, sql_definition: nil, materialized: false)
version = 1
end

sql_definition ||= definition(name, version)
sql_definition ||= definition(name, version, root_path: root_path)

if materialized
Scenic.database.create_materialized_view(
Expand Down Expand Up @@ -88,7 +88,7 @@ def drop_view(name, revert_to_version: nil, materialized: false)
# @example
# update_view :engagement_reports, version: 3, revert_to_version: 2
#
def update_view(name, version: nil, sql_definition: nil, revert_to_version: nil, materialized: false)
def update_view(name, version: nil, sql_definition: nil, revert_to_version: nil, materialized: false, root_path: Rails.root)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Metrics/ParameterLists: Avoid parameter lists longer than 5 parameters. [6/5]
Lint/UnusedMethodArgument: Unused method argument - revert_to_version.
Metrics/LineLength: Line is too long. [128/80]

if version.blank? && sql_definition.blank?
raise(
ArgumentError,
Expand All @@ -103,7 +103,7 @@ def update_view(name, version: nil, sql_definition: nil, revert_to_version: nil,
)
end

sql_definition ||= definition(name, version)
sql_definition ||= definition(name, version, root_path: root_path)

if materialized
Scenic.database.update_materialized_view(
Expand Down Expand Up @@ -132,7 +132,7 @@ def update_view(name, version: nil, sql_definition: nil, revert_to_version: nil,
# @example
# replace_view :engagement_reports, version: 3, revert_to_version: 2
#
def replace_view(name, version: nil, revert_to_version: nil, materialized: false)
def replace_view(name, version: nil, revert_to_version: nil, materialized: false, root_path: Rails.root)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Lint/UnusedMethodArgument: Unused method argument - revert_to_version.
Metrics/LineLength: Line is too long. [108/80]

if version.blank?
raise ArgumentError, "version is required"
end
Expand All @@ -141,15 +141,15 @@ def replace_view(name, version: nil, revert_to_version: nil, materialized: false
raise ArgumentError, "Cannot replace materialized views"
end

sql_definition = definition(name, version)
sql_definition = definition(name, version, root_path: root_path)

Scenic.database.replace_view(name, sql_definition)
end

private

def definition(name, version)
Scenic::Definition.new(name, version).to_sql
def definition(name, version, root_path: Rails.root)
Scenic::Definition.new(name, version, root_path: root_path).to_sql
end

def no_data(materialized)
Expand Down
7 changes: 7 additions & 0 deletions spec/scenic/definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ module Scenic

expect(definition.full_path).to eq Rails.root.join(definition.path)
end

it "joins the path with whatever root you pass in" do
new_root = Pathname.new("component/engine/test/dummy")
definition = Definition.new("searches", 15, root_path: new_root)

expect(definition.full_path).to eq new_root.join(definition.path)
end
end

describe "version" do
Expand Down
52 changes: 46 additions & 6 deletions spec/scenic/statements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Scenic
version = 15
definition_stub = instance_double("Definition", to_sql: "foo")
allow(Definition).to receive(:new)
.with(:views, version)
.with(:views, version, root_path: Rails.root)
.and_return(definition_stub)

connection.create_view :views, version: version
Expand All @@ -21,6 +21,20 @@ module Scenic
.with(:views, definition_stub.to_sql)
end

it "creates a view from a file and alternate root_path" do
version = 15
root_path = Pathname.new("component/engine/test/dummy")
definition_stub = instance_double("Definition", to_sql: "foo")
allow(Definition).to receive(:new)
.with(:views, version, root_path: root_path)
.and_return(definition_stub)

connection.create_view :views, version: version, root_path: root_path

expect(Scenic.database).to have_received(:create_view)
.with(:views, definition_stub.to_sql)
end

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

Expand All @@ -34,7 +48,7 @@ module Scenic
version = 1
definition_stub = instance_double("Definition", to_sql: "foo")
allow(Definition).to receive(:new)
.with(:views, version)
.with(:views, version, root_path: Rails.root)
.and_return(definition_stub)

connection.create_view :views
Expand Down Expand Up @@ -98,7 +112,7 @@ module Scenic
it "updates the view in the database" do
definition = instance_double("Definition", to_sql: "definition")
allow(Definition).to receive(:new)
.with(:name, 3)
.with(:name, 3, root_path: Rails.root)
.and_return(definition)

connection.update_view(:name, version: 3)
Expand All @@ -107,6 +121,19 @@ module Scenic
.with(:name, definition.to_sql)
end

it "updates the view in the database with an alternate path" do
root_path = Pathname.new("component/engine/test/dummy")
definition = instance_double("Definition", to_sql: "definition")
allow(Definition).to receive(:new)
.with(:name, 3, root_path: root_path)
.and_return(definition)

connection.update_view(:name, version: 3, root_path: root_path)

expect(Scenic.database).to have_received(:update_view)
.with(:name, definition.to_sql)
end

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

Expand All @@ -119,7 +146,7 @@ module Scenic
it "updates the materialized view in the database" do
definition = instance_double("Definition", to_sql: "definition")
allow(Definition).to receive(:new)
.with(:name, 3)
.with(:name, 3, root_path: Rails.root)
.and_return(definition)

connection.update_view(:name, version: 3, materialized: true)
Expand All @@ -131,7 +158,7 @@ module Scenic
it "updates the materialized view in the database with NO DATA" do
definition = instance_double("Definition", to_sql: "definition")
allow(Definition).to receive(:new)
.with(:name, 3)
.with(:name, 3, root_path: Rails.root)
.and_return(definition)

connection.update_view(
Expand Down Expand Up @@ -166,7 +193,7 @@ module Scenic
it "replaces the view in the database" do
definition = instance_double("Definition", to_sql: "definition")
allow(Definition).to receive(:new)
.with(:name, 3)
.with(:name, 3, root_path: Rails.root)
.and_return(definition)

connection.replace_view(:name, version: 3)
Expand All @@ -175,6 +202,19 @@ module Scenic
.with(:name, definition.to_sql)
end

it "replaces the view in the database with an alternate path" do
root_path = Pathname.new("component/engine/test/dummy")
definition = instance_double("Definition", to_sql: "definition")
allow(Definition).to receive(:new)
.with(:name, 3, root_path: root_path)
.and_return(definition)

connection.replace_view(:name, version: 3, root_path: root_path)

expect(Scenic.database).to have_received(:replace_view)
.with(:name, definition.to_sql)
end

it "fails to replace the materialized view in the database" do
definition = instance_double("Definition", to_sql: "definition")
allow(Definition).to receive(:new)
Expand Down