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
# 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 = []
end

Instance Method Details

#dump_with_views(stream) ⇒ Object

Add views to the end of the dump stream



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails_sql_views/schema_dumper.rb', line 20

def dump_with_views(stream)
  dump_without_views(stream)
  begin
    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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rails_sql_views/schema_dumper.rb', line 83

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



15
16
17
# File 'lib/rails_sql_views/schema_dumper.rb', line 15

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

#view(view, stream) ⇒ Object

Add the specified view to the stream



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rails_sql_views/schema_dumper.rb', line 54

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails_sql_views/schema_dumper.rb', line 38

def views(stream)
  @connection.views.sort.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