Class: CouchTomato::JsViewSource

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_tomato/js_view_source.rb

Class Method Summary collapse

Class Method Details

.changed_views?(fs_doc, db_doc) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/couch_tomato/js_view_source.rb', line 39

def self.changed_views?(fs_doc, db_doc)
  return true if db_doc.nil?

  fs_doc['views'].each do |name, fs_view|
    db_view = db_doc['views'][name]
    %w(map reduce).each do |method|
      return true if db_view.nil? || db_view["sha1-#{method}"] != fs_view["sha1-#{method}"]
    end
  end

  return fs_doc['views'].length != db_doc['views'].length
end

.diffObject



52
53
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/couch_tomato/js_view_source.rb', line 52

def self.diff
  status_dict = {
    :NEW_DOC  => "new design:", :NEW_VIEW => "new view:", 
    :MOD_VIEW => "modified:",   :DEL_DOC  => "deleted:", 
    :DEL_VIEW => "deleted:",    :NEW_DB   => "new db:"
  }
  types = ["map", "reduce"]
  puts "# Changes with respect to the filesystem:"
  
  fs_database_names.each do |database_name|
    db = database(database_name)
    puts "#\t#{setw(status_dict[:NEW_DB], 14)}#{database_name}" unless is_db?(db)
    
    fs_docs = fs_design_docs(database_name)
    db_docs = is_db?(db) ? db_design_docs(db) : {}

    diff = []
    # design docs on fs but not in db
    (fs_docs.keys - db_docs.keys).each do |design_name|
      unless fs_docs[design_name]['views'].empty?
        diff.push [:NEW_DOC, "#{database_name}/_#{design_name}"]
        fs_docs[design_name]['views'].keys.each do |view| 
          (fs_docs[design_name]['views'][view].keys & types).each {|type|
            diff.push [:NEW_VIEW, "#{database_name}/_#{design_name}/#{view}-#{type}"]}
        end
      end
    end

    # design docs in db but not on fs
    (db_docs.keys - fs_docs.keys).each do |design_name|
      diff.push [:DEL_DOC, "#{database_name}/_#{design_name}"] unless (design_name.to_s.include? "migrations")
    end

    # design docs in both db and fs
    (fs_docs.keys & db_docs.keys).each do |design_name|
      common_view_keys = (fs_docs[design_name]['views'].keys & db_docs[design_name]['views'].keys)
      fs_only_view_keys = fs_docs[design_name]['views'].keys - common_view_keys
      db_only_view_keys = db_docs[design_name]['views'].keys - common_view_keys

      unless fs_only_view_keys.empty?
        methods = fs_only_view_keys.map do |key|
          %w(map reduce).map {|method| fs_docs[design_name]['views'][key][method].nil? ? nil : "#{key}-#{method}"}.compact
        end.flatten
        methods.each {|method| diff.push [:NEW_VIEW, "#{database_name}/_#{design_name}/#{method}"] }
      end

      unless db_only_view_keys.empty?
        methods = db_only_view_keys.map do |key|
          %w(map reduce).map {|method| db_docs[design_name]['views'][key][method] ? "#{key}-#{method}" : nil}.compact
        end
        methods.each {|method| diff.push [:DEL_VIEW, "#{database_name}/_#{design_name}/#{method}"] }
      end

      common_view_keys.each do |common_key|
        # are the sha's the same?
        # map reduce sha1
        fs_view = fs_docs[design_name]['views'][common_key]
        db_view = db_docs[design_name]['views'][common_key]

        # has either the map or reduce been added or removed
        %w(map reduce).each do |method|
          if db_view[method] && !fs_view[method]
            diff.push [:DEL_VIEW, "#{database_name}/_#{design_name}/#{common_key}-#{method}"]  and next
          end

          if fs_view[method] && !db_view[method]
            diff.push [:NEW_VIEW, "#{database_name}/_#{design_name}/#{common_key}-#{method}"]  and next
          end

          if fs_view["sha1-#{method}"] != db_view["sha1-#{method}"]
            diff.push [:MOD_VIEW, "#{database_name}/_#{design_name}/#{common_key}-#{method}"]  and next
          end
        end
      end
    end
    
    diff.uniq!
    diff.each do |status|
      puts "#\t#{setw(status_dict[status.first], 14)}#{status.last}"
    end
  end
end

.push(silent = false) ⇒ Object

todo: provide a ‘dirty?’ method that can be called in an initializer and warn the developer that view are out of sync todo: provide more granular information about which views are being modified todo: limitation (bug?) where if you remove a database’s views entirely from the file system, view’s will not be removed from the database as may be expected



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/couch_tomato/js_view_source.rb', line 10

def self.push(silent=false)
  fs_database_names.each do |database_name|
    db = database!(database_name)

    fs_docs = fs_design_docs(database_name)
    db_docs = db_design_docs(db)

    fs_docs.each do |design_name, fs_doc|
      db_doc = db_docs[design_name]

      if db_doc
        fs_doc['_id'] = db_doc['_id']
        fs_doc['_rev'] = db_doc['_rev']
      end

      if fs_doc['views'].empty?
        next unless fs_doc['_rev']
        puts "DELETE #{database_name}/#{fs_doc['_id']}" unless silent
        db.delete_doc(fs_doc)
      else
        if changed_views?(fs_doc, db_doc)
          puts "UPDATE #{database_name}/#{fs_doc['_id']}" unless silent
          db.save_doc(fs_doc)
        end
      end
    end
  end
end