- Use InnoDB.
- Put version column on the current_* tables.
- Use transactions (untested).
node = Node.from_xml(request.raw_post, true)
if node
+ node.version = 0
node.user_id = @user.id
node.visible = true
node.save_with_history!
if !relation.preconditions_ok?
render :text => "", :status => :precondition_failed
else
+ relation.version = 0
relation.user_id = @user.id
relation.save_with_history!
if !way.preconditions_ok?
render :text => "", :status => :precondition_failed
else
+ way.version = 0
way.user_id = @user.id
way.save_with_history!
def save_with_history!
t = Time.now
-
- Node.transaction do
- # apply timestamp to the new node
+ Node.transaction do
+ self.version += 1
self.timestamp = t
self.save!
- end
- # Create a NodeTag
- NodeTag.transaction do
+ # Create a NodeTag
tags = self.tags
-
NodeTag.delete_all(['id = ?', self.id])
-
- sequence_id = 1
tags.each do |k,v|
- tag = NodeTag.new
- tag.k = k
- tag.v = v
- tag.id = self.id
- tag.sequence_id = sequence_id
- tag.save!
- sequence_id += 1
+ tag = NodeTag.new
+ tag.k = k
+ tag.v = v
+ tag.id = self.id
+ tag.save!
end
- end
- # Create an OldNode
- old_node = OldNode.from_node(self)
- old_node.timestamp = t
- old_node.save_with_dependencies!
+ # Create an OldNode
+ old_node = OldNode.from_node(self)
+ old_node.timestamp = t
+ old_node.save_with_dependencies!
+ end
end
def to_xml
old_node.timestamp = node.timestamp
old_node.user_id = node.user_id
old_node.id = node.id
+ old_node.version = node.version
return old_node
end
#ok from here
@attributes.update(OldNode.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp]).instance_variable_get('@attributes'))
- sequence_id = 1
self.tags.each do |k,v|
tag = OldNodeTag.new
tag.k = k
tag.v = v
tag.id = self.id
tag.version = self.version
- tag.sequence_id = sequence_id
tag.save!
- sequence_id += 1
end
end
old_relation.user_id = relation.user_id
old_relation.timestamp = relation.timestamp
old_relation.id = relation.id
+ old_relation.version = relation.version
old_relation.members = relation.members
old_relation.tags = relation.tags
return old_relation
old_way.user_id = way.user_id
old_way.timestamp = way.timestamp
old_way.id = way.id
+ old_way.version = way.version
old_way.nds = way.nds
old_way.tags = way.tags
return old_way
def save_with_history!
Relation.transaction do
t = Time.now
+ self.version += 1
self.timestamp = t
self.save!
tags = self.tags
-
RelationTag.delete_all(['id = ?', self.id])
-
tags.each do |k,v|
tag = RelationTag.new
tag.k = k
end
members = self.members
-
RelationMember.delete_all(['id = ?', self.id])
-
members.each do |n|
mem = RelationMember.new
mem.id = self.id
t = Time.now
Way.transaction do
+ self.version += 1
self.timestamp = t
self.save!
- end
- WayTag.transaction do
tags = self.tags
-
WayTag.delete_all(['id = ?', self.id])
-
tags.each do |k,v|
tag = WayTag.new
tag.k = k
tag.id = self.id
tag.save!
end
- end
- WayNode.transaction do
nds = self.nds
-
WayNode.delete_all(['id = ?', self.id])
-
sequence = 1
nds.each do |n|
nd = WayNode.new
nd.save!
sequence += 1
end
- end
- old_way = OldWay.from_way(self)
- old_way.timestamp = t
- old_way.save_with_dependencies!
+ old_way = OldWay.from_way(self)
+ old_way.timestamp = t
+ old_way.save_with_dependencies!
+ end
end
def preconditions_ok?
--- /dev/null
+class MoveToInnodb < ActiveRecord::Migration
+ @@conv_tables = ['nodes', 'ways', 'way_tags', 'way_nodes',
+ 'current_way_nodes', 'relation_members', 'relations',
+ 'relation_tags', 'current_relation_tags']
+
+ @@ver_tbl = ['nodes', 'ways', 'relations']
+
+ def self.up
+ execute 'DROP INDEX current_way_tags_v_idx ON current_way_tags'
+ execute 'DROP INDEX current_relation_tags_v_idx ON current_relation_tags'
+
+ @@ver_tbl.each { |tbl|
+ change_column tbl, "version", :bigint, :limit => 20, :null => false
+ }
+
+ @@conv_tables.each { |tbl|
+ execute "ALTER TABLE #{tbl} ENGINE = InnoDB"
+ }
+
+ @@ver_tbl.each { |tbl|
+ add_column "current_#{tbl}", "version", :bigint, :limit => 20, :null => false
+ execute "UPDATE current_#{tbl} SET version = " +
+ "(SELECT max(version)+1 FROM #{tbl} WHERE #{tbl}.id = current_#{tbl}.id)"
+ }
+ end
+
+ def self.down
+ end
+end