Module: RailsSqlViews::SchemaDumper

Defined in:
lib/rails_sql_views/schema_dumper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rails_sql_views/schema_dumper.rb', line 3

def self.included(base)
  base.alias_method_chain :trailer, :views
  base.alias_method_chain :dump, :views
  base.alias_method_chain :tables, :views_excluded
  
  # A list of views which should not be dumped to the schema. 
  # Acceptable values are strings as well as regexp.
  # This setting is only used if ActiveRecord::Base.schema_format == :ruby
  base.cattr_accessor :ignore_views
  base.ignore_views = []
  # Optional: specify the order that in which views are created.
  # This allows views to depend on and include fields from other views.
  # It is not necessary to specify all the view names, just the ones that 
  # need to be created first
  base.cattr_accessor :view_creation_order
  base.view_creation_order = []
end

Instance Method Details

#dump_with_views(stream) ⇒ Object

Add views to the end of the dump stream



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rails_sql_views/schema_dumper.rb', line 26

def dump_with_views(stream)
  dump_without_views(stream)
  begin
    @connection.class.send(:public, :supports_views?) # this is a kluge, but I don't know why it's changing back to private before it gets here
    if @connection.supports_views?
      views(stream)
    end
  rescue => e
    if ActiveRecord::Base.logger
      ActiveRecord::Base.logger.error "Unable to dump views: #{e}"
    else
      raise e
    end
  end
  trailer_without_views(stream)
  stream
end

#tables_with_views_excluded(stream) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rails_sql_views/schema_dumper.rb', line 98

def tables_with_views_excluded(stream)
  @connection.base_tables.sort.each do |tbl|
    next if [ActiveRecord::Migrator.schema_migrations_table_name, ignore_tables].flatten.any? do |ignored|
      case ignored
      when String then tbl == ignored
      when Regexp then tbl =~ ignored
      else
        raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
      end
    end
    table(tbl, stream)
  end
end

#trailer_with_views(stream) ⇒ Object



21
22
23
# File 'lib/rails_sql_views/schema_dumper.rb', line 21

def trailer_with_views(stream)
  # do nothing...we'll call this later
end

#view(view, stream) ⇒ Object

Add the specified view to the stream



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rails_sql_views/schema_dumper.rb', line 69

def view(view, stream)
  columns = @connection.columns(view).collect { |c| c.name }
  begin
    v = StringIO.new

    v.print "  create_view #{view.inspect}"
    v.print ", #{@connection.view_select_statement(view).dump}"
    v.print ", :force => true"
    v.puts " do |v|"

    columns.each do |column|
      v.print "    v.column :#{column}"
      v.puts
    end

    v.puts "  end"
    v.puts
    
    v.rewind
    stream.print v.read
  rescue => e
    stream.puts "# Could not dump view #{view.inspect} because of following #{e.class}"
    stream.puts "#   #{e.message}"
    stream.puts
  end
  
  stream
end

#views(stream) ⇒ Object

Add views to the stream



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails_sql_views/schema_dumper.rb', line 45

def views(stream)
  if view_creation_order.empty?
    sorted_views = @connection.views.sort
  else
    # set union, merge by joining arrays, removing dups
    # this will float the view name sin view_creation_order to the top
    # without requiring all the views to be specified
    sorted_views = view_creation_order | @connection.views
  end
  sorted_views.each do |v|
    next if [ActiveRecord::Migrator.schema_migrations_table_name, ignore_views].flatten.any? do |ignored|
      case ignored
      when String then v == ignored
      when Symbol then v == ignored.to_s
      when Regexp then v =~ ignored
      else
        raise StandardError, 'ActiveRecord::SchemaDumper.ignore_views accepts an array of String and / or Regexp values.'
      end
    end 
    view(v, stream)
  end
end