Class: DeployCouch::DbSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/deploy_couch/db_schema.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, repository) ⇒ DbSchema

Returns a new instance of DbSchema.



5
6
7
8
# File 'lib/deploy_couch/db_schema.rb', line 5

def initialize(schema,repository)
  @schema = schema
  @repository = repository
end

Class Method Details

.load_or_create(config, repository) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/deploy_couch/db_schema.rb', line 10

def self.load_or_create(config,repository)
  default_schema = {"_id"=>"schema__schema_document_key__",config.doc_type_field=>"__schema__", 'applied_deltas'=>[], "type_versions"=>{}}
  schema = repository.get_schema
  if (schema.nil?)
     repository.create_schema(default_schema)
     schema = repository.get_schema
  end
  DbSchema.new(schema,repository)
end

Instance Method Details

#applied_deltasObject



20
21
22
# File 'lib/deploy_couch/db_schema.rb', line 20

def applied_deltas
  @schema["applied_deltas"]
end

#completed(delta) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/deploy_couch/db_schema.rb', line 38

def completed(delta)
  @schema['applied_deltas'].push(delta.id)
  current_type_version = @schema['type_versions'].has_key?(delta.type) ? @schema['type_versions'][delta.type] : 0
  @schema['type_versions'][delta.type] = current_type_version + 1

  @repository.put_document(@schema)
  @schema = @repository.get_schema
end

#get_next_type_version_for(type) ⇒ Object



28
29
30
31
32
# File 'lib/deploy_couch/db_schema.rb', line 28

def get_next_type_version_for(type)
  current_version = type_versions[type]
  current_version = 0 if current_version.nil?    
  current_version += 1
end

#rollback(delta) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/deploy_couch/db_schema.rb', line 47

def rollback(delta)
  @schema['applied_deltas'].delete(delta.id)
  current_type_version = @schema['type_versions'].has_key?(delta.type) ? @schema['type_versions'][delta.type] : 0
  @schema['type_versions'][delta.type] = current_type_version - 1

  @repository.put_document(@schema)
  @schema = @repository.get_schema
end

#type_version_fieldObject



34
35
36
# File 'lib/deploy_couch/db_schema.rb', line 34

def type_version_field
  @schema["type_version_field"]
end

#type_versionsObject



24
25
26
# File 'lib/deploy_couch/db_schema.rb', line 24

def type_versions
  @schema["type_versions"]
end