-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f02384
commit 6f1f5cb
Showing
10 changed files
with
175 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ PATH | |
specs: | ||
sql_view (0.1.0) | ||
rails | ||
scenic | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,16 @@ | ||
Description: | ||
Create a new database view for your application. This will create a new | ||
view definition file and the accompanying migration. | ||
|
||
If a view of the given name already exists, create a new version of the view | ||
and a migration to replace the old version with the new. | ||
Create a new database view in your application. | ||
|
||
To create a materialized view, pass the '--materialized' option. | ||
To create a materialized view with NO DATA, pass '--no-data' option. | ||
|
||
Examples: | ||
rails generate scenic:view searches | ||
rails generate sql_view:view ActiveUser 'User.where(active: true)' | ||
|
||
create: db/views/searches_v01.sql | ||
create: db/migrate/20140803191158_create_searches.rb | ||
create: app/sql_views/search_view.rb | ||
create: db/migrate/20140803191158_create_active_users_views.rb | ||
|
||
rails generate scenic:view searches | ||
Examples: | ||
rails generate sql_view:view ArchivedAccount Account.archived --materialized --view-name=inactive_accounts | ||
|
||
create: db/views/searches_v02.sql | ||
create: db/migrate/20140804191158_update_searches_to_version_2.rb | ||
create: app/sql_views/search_view.rb | ||
create: db/migrate/20140803191158_create_search_views.rb |
5 changes: 0 additions & 5 deletions
5
lib/generators/sql_view/view/templates/db/migrate/create_view.erb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
module SqlView | ||
class Railtie < ::Rails::Railtie | ||
initializer "sql_view.load" do | ||
ActiveSupport.on_load :active_record do | ||
ActiveRecord::ConnectionAdapters::AbstractAdapter.include SqlView::Statements | ||
ActiveRecord::SchemaDumper.prepend SqlView::SchemaDumper | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
require "rails" | ||
|
||
# Copy-pasted from scenic game. Scenic is a very nice gem | ||
|
||
module SqlView | ||
# @api private | ||
module SchemaDumper | ||
class DBView < OpenStruct | ||
|
||
def materialized? | ||
self.kind == "m" | ||
end | ||
|
||
def materialized_or_not | ||
materialized? ? " MATERIALIZED " : nil | ||
end | ||
|
||
def to_schema | ||
<<-DEFINITION | ||
create_sql_view "#{self.viewname}", sql: <<-\SQL | ||
CREATE #{materialized_or_not} VIEW "#{self.viewname}" AS | ||
#{escaped_definition.indent(2)} | ||
SQL | ||
DEFINITION | ||
end | ||
|
||
def escaped_definition | ||
definition.gsub("\\", "\\\\\\") | ||
end | ||
end | ||
|
||
def tables(stream) | ||
super | ||
views(stream) | ||
end | ||
|
||
def views(stream) | ||
if dumpable_views_in_database.any? | ||
stream.puts | ||
end | ||
|
||
dumpable_views_in_database.each do |viewname| | ||
view = DBView.new(get_view_info(viewname)) | ||
#puts view.to_schema | ||
stream.puts(view.to_schema) | ||
#indexes(view.name, stream) | ||
end | ||
end | ||
|
||
private | ||
|
||
def dumpable_views_in_database | ||
@dumpable_views_in_database ||= ActiveRecord::Base.connection.views.reject do |viewname| | ||
ignored?(viewname) | ||
end | ||
end | ||
|
||
def get_view_info(viewname) | ||
views_schema.detect{|e| e['viewname'] == viewname} | ||
end | ||
|
||
def views_schema | ||
@views_schema ||= ActiveRecord::Base.connection.execute(<<-SQL) | ||
SELECT | ||
c.relname as viewname, | ||
pg_get_viewdef(c.oid) AS definition, | ||
c.relkind AS kind, | ||
n.nspname AS namespace | ||
FROM pg_class c | ||
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace | ||
WHERE | ||
c.relkind IN ('m', 'v') | ||
AND c.relname NOT IN (SELECT extname FROM pg_extension) | ||
AND n.nspname = ANY (current_schemas(false)) | ||
ORDER BY c.oid | ||
SQL | ||
.to_a | ||
end | ||
|
||
unless ActiveRecord::SchemaDumper.private_instance_methods(false).include?(:ignored?) | ||
# This method will be present in Rails 4.2.0 and can be removed then. | ||
def ignored?(table_name) | ||
["schema_migrations", ignore_tables].flatten.any? do |ignored| | ||
case ignored | ||
when String then remove_prefix_and_suffix(table_name) == ignored | ||
when Regexp then remove_prefix_and_suffix(table_name) =~ ignored | ||
else | ||
raise StandardError, "ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values." | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module SqlView | ||
module Statements | ||
def create_sql_view(viewname, sql:) | ||
ActiveRecord::Base.connection.execute(sql) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters