Class: ActiveCouch::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_couch/migrations/migrator.rb

Class Method Summary collapse

Class Method Details

.create_database(site, name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_couch/migrations/migrator.rb', line 24

def create_database(site, name)
  conn = Connection.new(site)
  response = conn.put("/#{name}", "{}")

  case response.code
  when '201' # 201 = success
    true
  when '409' # 409 = database already exists
    raise ActiveCouch::MigrationError, 'Database exists'
  else
    raise ActiveCouch::MigrationError, "Error creating database - got HTTP response #{response.code}"
  end
end

.delete_database(site, name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_couch/migrations/migrator.rb', line 38

def delete_database(site, name)
  conn = Connection.new(site)
  response = conn.delete("/#{name}")

  case response.code
  when '202'
    true # 202 = success
  when '404'
    raise ActiveCouch::MigrationError, "Database '#{name}' does not exist" # 404 = database doesn't exist
  else
    raise ActiveCouch::MigrationError, "Error creating database - got HTTP response #{response.code}"
  end
end

.migrate(site, migration) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_couch/migrations/migrator.rb', line 7

def migrate(site, migration)
  if migration.view.nil? || migration.database.nil?
    raise ActiveCouch::MigrationError, "Both the view and the database need to be defined in your migration"
  end

  conn = Connection.new(site)
  # Migration for a view with name 'by_name' and database 'activecouch_test' should be PUT to
  # http://#{host}:#{port}/activecouch_test/_design/by_name.
  response = conn.put("/#{migration.database}/_design/#{migration.view}", migration.view_js)
  case response.code
  when '201'
    true # 201 = success
  else
    raise ActiveCouch::MigrationError, "Error migrating view - got HTTP response #{response.code}"
  end
end