Module: CouchRestRails::Database

Extended by:
Database
Included in:
Database
Defined in:
lib/couch_rest_rails/database.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) create(database_name = '*', opts = {})



6
7
8
9
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
38
39
40
41
42
43
44
# File 'lib/couch_rest_rails/database.rb', line 6

def create(database_name = '*', opts = {})
  
  CouchRestRails.process_database_method(database_name) do |db, response|
    
    # Setup up views directory
    database_views_path = File.join(RAILS_ROOT, CouchRestRails.views_path, db, 'views')
    unless File.exist?(database_views_path)
      FileUtils.mkdir_p(database_views_path)
      response << "Created #{File.join(CouchRestRails.views_path, db, 'views')} views directory"
    end
    
    # Setup the Lucene directory if enabled
    if CouchRestRails.use_lucene
      database_lucene_path = File.join(RAILS_ROOT, CouchRestRails.lucene_path, db, 'lucene')
      unless File.exist?(database_lucene_path)
        FileUtils.mkdir_p(database_lucene_path)
        response << "Created #{File.join(CouchRestRails.lucene_path, db, 'lucene')} Lucene directory"
      end
    end
    
    full_db_name = [COUCHDB_CONFIG[:db_prefix], File.basename(db), COUCHDB_CONFIG[:db_suffix]].join
    
    # Warn if no model uses the database
    unless CouchRestRails::Database.list.include?(db)
      response << "WARNING: there are no CouchRestRails::Document models using #{db}"
    end
    
    # Create the database
    if COUCHDB_SERVER.databases.include?(full_db_name)
      response << "Database #{db} (#{full_db_name}) already exists"
      next
    else
      COUCHDB_SERVER.create_db(full_db_name)
      response << "Created database #{db} (#{full_db_name})"
    end
    
  end

end

- (Object) delete(database_name = '*', opts = {})



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/couch_rest_rails/database.rb', line 46

def delete(database_name = '*', opts = {})
  
  CouchRestRails.process_database_method(database_name) do |db, response|
  
    full_db_name = [COUCHDB_CONFIG[:db_prefix], File.basename(db), COUCHDB_CONFIG[:db_suffix]].join
    if !COUCHDB_SERVER.databases.include?(full_db_name)
      response << "Database #{db} (#{full_db_name}) does not exist"
    else
      CouchRest.delete "#{COUCHDB_CONFIG[:host_path]}/#{full_db_name}"
      response << "Deleted database #{db} (#{full_db_name})"
    end
    
    # Warn if views path still present for database
    if File.exist?(File.join(RAILS_ROOT, CouchRestRails.views_path, db, 'views'))
      response << "WARNING: #{File.join(CouchRestRails.views_path, db, 'views')} views path still present"
    end
    
    # Warn if Lucene path still present for database
    if CouchRestRails.use_lucene
      if File.exist?(File.join(RAILS_ROOT, CouchRestRails.lucene_path, db, 'views'))
        response << "WARNING: #{File.join(CouchRestRails.lucene_path, db, 'views')} Lucene path still present"
      end
    end
    
  end

end

- (Object) list



74
75
76
77
78
79
80
81
82
83
# File 'lib/couch_rest_rails/database.rb', line 74

def list
  databases = []
  # Ensure models are loaded
  Dir.glob(File.join(RAILS_ROOT, 'app', 'models', '*.rb')).map { |m| require_dependency m }
  Object.subclasses_of(CouchRestRails::Document).collect do |doc|
    raise "#{doc.name} does not have a database defined" unless doc.database
    databases << doc.unadorned_database_name
  end
  databases.sort.uniq
end