Module: DataMapper::Persevere::Migrations

Defined in:
lib/persevere_adapter/migrations.rb

Instance Method Summary collapse

Instance Method Details

#create_model_storage(model) ⇒ Object

Creates the persevere schema from the model.

Parameters:

  • model (DataMapper::Model)

    The model that corresponds to the storage schema that needs to be created.

[View source]

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/persevere_adapter/migrations.rb', line 27

def create_model_storage(model)
  model = Persevere.enhance(model)
  name       = self.name
  properties = model.properties_with_subclasses(name)
  
  return false if storage_exists?(model.storage_name(name))
  return false if properties.empty?

  # Make sure storage for referenced objects exists
  model.relationships.each_pair do |n, r|
    if ! storage_exists?(r.child_model.storage_name)
      put_schema({'id' => r.child_model.storage_name, 'properties' => {}})
    end
  end
  schema_hash = model.to_json_schema_hash()
  
  return true unless put_schema(schema_hash) == false
  false
end

#destroy_model_storage(model) ⇒ Object

Destroys the persevere schema from the model.

Parameters:

  • model (DataMapper::Model)

    The model that corresponds to the storage schema that needs to be destroyed.

[View source]

89
90
91
92
93
94
95
# File 'lib/persevere_adapter/migrations.rb', line 89

def destroy_model_storage(model)
  model = Persevere.enhance(model)
  return true unless storage_exists?(model.storage_name(name))
  schema_hash = model.to_json_schema_hash()
  return true unless delete_schema(schema_hash) == false
  false
end

#storage_exists?(storage_name) ⇒ Boolean

Returns whether the storage_name exists.

Parameters:

  • storage_name (String)

    a String defining the name of a storage, for example a table name.

Returns:

  • (Boolean)

    true if the storage exists

[View source]

14
15
16
17
18
# File 'lib/persevere_adapter/migrations.rb', line 14

def storage_exists?(storage_name)
  class_names = JSON.parse(@persevere.retrieve('/Class/[=id]').body)
  return true if class_names.include?("Class/"+storage_name)
  false
end

#upgrade_model_storage(model) ⇒ Object

Updates the persevere schema from the model.

Parameters:

  • model (DataMapper::Model)

    The model that corresponds to the storage schema that needs to be updated.

[View source]

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
# File 'lib/persevere_adapter/migrations.rb', line 54

def upgrade_model_storage(model)
  model = Persevere.enhance(model)
  name       = self.name
  properties = model.properties_with_subclasses(name)
  
  DataMapper.logger.debug("Upgrading #{model.name}")
  
  if success = create_model_storage(model)
    return properties
  end
  
  new_schema_hash = model.to_json_schema_hash()
  current_schema_hash = get_schema(new_schema_hash['id'])[0]
  # TODO: Diff of what is there and what will be added.

  new_properties = properties.map do |property|
    prop_name = property.name.to_s
    prop_type = property.type
    next if prop_name == 'id' || 
            (current_schema_hash['properties'].has_key?(prop_name) && 
            new_schema_hash['properties'][prop_name]['type'] == current_schema_hash['properties'][prop_name]['type'] )
    property
  end.compact
  
  return new_properties unless update_schema(new_schema_hash) == false
  return nil
end