Skip to content

Commit

Permalink
Fixed migration for initial migrate. Fixed serialization with links (…
Browse files Browse the repository at this point in the history
…slow query).
  • Loading branch information
gaspard committed Jan 4, 2011
1 parent 5c39fed commit 954c324
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 68 deletions.
2 changes: 1 addition & 1 deletion db/migrate/001_create_base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$migrating_new_site = true
$migrating_new_db = true
class CreateBase < ActiveRecord::Migration
def self.up
create_table("users", :options => Zena::Db.table_options) do |t|
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/024_correct_vclass_kpath.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CorrectVclassKpath < ActiveRecord::Migration
def self.up
if !$migrating_new_site
unless $migrating_new_db
klasses = VirtualClass.find(:all)
klasses.each do |vclass|
Node.connection.execute "UPDATE nodes SET kpath = #{Node.connection.quote(vclass.kpath)} WHERE vclass_id = #{vclass[:id]}"
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/025_move_tag_into_vclass.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class MoveTagIntoVclass < ActiveRecord::Migration
def self.up
if !$migrating_new_site
unless $migrating_new_db
# create a new virtual class for each site and assign all tags to this class.
Site.find(:all).each do |site|
VirtualClass.connection.execute("INSERT INTO virtual_classes (name,kpath,real_class,icon,allowed_attributes,create_group_id,site_id) VALUES ('Tag','NPT','Page',NULL,NULL,#{site[:site_group_id]},#{site[:id]})")
Expand Down
70 changes: 36 additions & 34 deletions db/migrate/026_rename_templates.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
class RenameTemplates < ActiveRecord::Migration
def self.up
mode_change = [ ['search', '*search'],
['admin_layout', '*adminLayout'],
['index', '*index'],
['not_found', '*notFound'],
['popup_layout', '*popupLayout'],
['login', '*login']
]
mode_translation = Hash[*mode_change.flatten]
mode_change.each do |old_name, new_name|
execute "UPDATE #{TemplateContent.table_name} SET mode = '#{new_name}' WHERE mode = '#{old_name}'"
end
Template.find(:all).each do |t|
content = TemplateContent.find(:first, :conditions => "node_id = #{t[:id]}")
if content.klass
# update name
content.format ||= 'html'
format = content.format == 'html' ? '' : "-#{content.format}"
mode = (content.mode || format != '') ? "-#{content.mode}" : ''
new_name = "#{content.klass}#{mode}#{format}"
execute "UPDATE #{Node.table_name} SET name = #{quote(new_name)} WHERE id = #{t[:id]}"
unless $migrating_new_db
mode_change = [ ['search', '*search'],
['admin_layout', '*adminLayout'],
['index', '*index'],
['not_found', '*notFound'],
['popup_layout', '*popupLayout'],
['login', '*login']
]
mode_translation = Hash[*mode_change.flatten]
mode_change.each do |old_name, new_name|
execute "UPDATE #{TemplateContent.table_name} SET mode = '#{new_name}' WHERE mode = '#{old_name}'"
end
Version.find_all_by_node_id(t[:id]).each do |v|
text = v.text
new_text = text.gsub(/template\s*=\s*("|')([^\1]+?)\1/) do
sep, template_name = $1, $2
if template_name =~ /\A(\w+)(_(.+)|)(\.(\w+)|)/
base, mode, format = $1, $3, $5
format = (format && format != 'html') ? "-#{format}" : ''
if mode
mode = mode_translation[mode] || mode
template_name = "#{base}-#{mode}#{format}"
else
template_name = "#{base}#{format}"
Template.find(:all).each do |t|
content = TemplateContent.find(:first, :conditions => "node_id = #{t[:id]}")
if content.klass
# update name
content.format ||= 'html'
format = content.format == 'html' ? '' : "-#{content.format}"
mode = (content.mode || format != '') ? "-#{content.mode}" : ''
new_name = "#{content.klass}#{mode}#{format}"
execute "UPDATE #{Node.table_name} SET name = #{quote(new_name)} WHERE id = #{t[:id]}"
end
Version.find_all_by_node_id(t[:id]).each do |v|
text = v.text
new_text = text.gsub(/template\s*=\s*("|')([^\1]+?)\1/) do
sep, template_name = $1, $2
if template_name =~ /\A(\w+)(_(.+)|)(\.(\w+)|)/
base, mode, format = $1, $3, $5
format = (format && format != 'html') ? "-#{format}" : ''
if mode
mode = mode_translation[mode] || mode
template_name = "#{base}-#{mode}#{format}"
else
template_name = "#{base}#{format}"
end
end
"template='#{template_name}'"
end
"template='#{template_name}'"
execute "UPDATE #{Version.table_name} SET text = #{quote(new_text)} WHERE id = #{v[:id]}"
end
execute "UPDATE #{Version.table_name} SET text = #{quote(new_text)} WHERE id = #{v[:id]}"
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions db/migrate/045_avoid_star_in_templates.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class AvoidStarInTemplates < ActiveRecord::Migration
def self.up
execute "UPDATE #{TemplateContent.table_name} SET mode = REPLACE(mode, '*', '+')"
execute "UPDATE #{Node.table_name} SET name = REPLACE(name, '*', '+')"
execute "UPDATE #{Version.table_name} SET title = REPLACE(title, '*', '+')"
unless $migrating_new_db
execute "UPDATE #{TemplateContent.table_name} SET mode = REPLACE(mode, '*', '+')"
execute "UPDATE #{Node.table_name} SET name = REPLACE(name, '*', '+')"
execute "UPDATE #{Version.table_name} SET title = REPLACE(title, '*', '+')"
end
end

def self.down
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/046_fix_zazen_image_tag.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class FixZazenImageTag < ActiveRecord::Migration
def self.up
if !$migrating_new_site
unless $migrating_new_db
# Change all zazen image tags from !34.pv! to !34_pv! for better consistency with 'modes'.
{
Version.table_name => [:text, :summary],
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/047_change_default_link_id_to_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# make sure there exists a link with NULL content and id == -1 (used in queries using query order sorting)
class ChangeDefaultLinkIdToZero < ActiveRecord::Migration
def self.up
if !$migrating_new_site
unless $migrating_new_db
Link.connection.execute "UPDATE #{Link.table_name} SET id = 0 WHERE id = -1"
end
Zena::Db.insert_dummy_ids
Expand Down
6 changes: 4 additions & 2 deletions db/migrate/049_fix_publish_from_is_null.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class FixPublishFromIsNull < ActiveRecord::Migration
def self.up
execute "UPDATE #{Version.table_name} SET publish_from = updated_at WHERE status=50 AND publish_from IS NULL"
execute "UPDATE #{Node.table_name} SET publish_from = updated_at WHERE max_status=50 AND publish_from IS NULL"
unless $migrating_new_db
execute "UPDATE #{Version.table_name} SET publish_from = updated_at WHERE status=50 AND publish_from IS NULL"
execute "UPDATE #{Node.table_name} SET publish_from = updated_at WHERE max_status=50 AND publish_from IS NULL"
end
end

def self.down
Expand Down
12 changes: 7 additions & 5 deletions db/migrate/20090924141459_zafu_fix_sept09.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class ZafuFixSept09 < ActiveRecord::Migration
def self.up
# Update zafu from zena 0.13 to 0.14 (2009-09-24 changes)
select_all("SELECT id, text FROM #{TemplateVersion.table_name}", "#{TemplateVersion.table_name} Load").each do |record|
old_zafu = record['text']
new_zafu = old_zafu.gsub(%r{<r:uses_calendar\s*/>}, '<r:uses_datebox/>')
execute "UPDATE #{TemplateVersion.table_name} SET text = #{quote(new_zafu)} WHERE id = #{record['id']}" if new_zafu != old_zafu
unless $migrating_new_db
# Update zafu from zena 0.13 to 0.14 (2009-09-24 changes)
select_all("SELECT id, text FROM #{TemplateVersion.table_name}", "#{TemplateVersion.table_name} Load").each do |record|
old_zafu = record['text']
new_zafu = old_zafu.gsub(%r{<r:uses_calendar\s*/>}, '<r:uses_datebox/>')
execute "UPDATE #{TemplateVersion.table_name} SET text = #{quote(new_zafu)} WHERE id = #{record['id']}" if new_zafu != old_zafu
end
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class RenamePublishGroupToDriveGroup < ActiveRecord::Migration
def self.up
if Node.column_names.include?('dgroup_id')
unless $migrating_new_db
remove_column :nodes, :dgroup_id # old stuff
end
rename_column :nodes, :pgroup_id, :dgroup_id
Expand Down
17 changes: 9 additions & 8 deletions db/migrate/20091124161608_rebuild_fullpath.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
class RebuildFullpath < ActiveRecord::Migration
def self.up
unless $migrating_new_db
# Reset column information (used when running all migrations at once)
[User, Node, Version, Site, Group].each do |klass|
klass.reset_column_information
end

# Reset column information (used when running all migrations at once)
[User, Node, Version, Site, Group].each do |klass|
klass.reset_column_information
end

Site.all.each do |site|
puts "===== rebuilding fullpath for #{site.host} (#{Node.count(:conditions => "site_id = #{site.id}")} nodes)"
site.rebuild_fullpath
Site.all.each do |site|
puts "===== rebuilding fullpath for #{site.host} (#{Node.count(:conditions => "site_id = #{site.id}")} nodes)"
site.rebuild_fullpath
end
end
end

Expand Down
8 changes: 5 additions & 3 deletions db/migrate/20100115134729_rebuild_fullpath_after_fix.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class RebuildFullpathAfterFix < ActiveRecord::Migration
def self.up
Site.all.each do |site|
puts "===== rebuilding fullpath for #{site.host} (#{Node.count(:conditions => "site_id = #{site.id}")} nodes)"
site.rebuild_fullpath
unless $migrating_new_db
Site.all.each do |site|
puts "===== rebuilding fullpath for #{site.host} (#{Node.count(:conditions => "site_id = #{site.id}")} nodes)"
site.rebuild_fullpath
end
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class RenamedContactModelToBaseContact < ActiveRecord::Migration
def self.up
unless $migrating_new_site
unless $migrating_new_db
execute "UPDATE nodes SET type = 'BaseContact' WHERE type = 'Contact'"
execute "UPDATE roles SET real_class = 'BaseContact' WHERE real_class = 'Contact'"
end
Expand Down
11 changes: 5 additions & 6 deletions lib/zena/acts/serializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,15 @@ def all_link_ids
res = {}
roles = {}

sql = [%Q{SELECT nodes.zip, links.relation_id, links.source_id FROM nodes,links WHERE ((nodes.id = links.target_id AND links.source_id = ?) OR (nodes.id = links.source_id AND links.target_id = ?) OR (nodes.id = ? AND links.id = 0)) AND #{secure_scope('nodes')}}, self.id, self.id, self.parent_id]
# Yes, UNION is the fastest way to do this (54k data, 600 links). UNION = 0.02s, OR = 124s !!
sql = [%Q{SELECT nodes.zip, links.relation_id, links.source_id FROM nodes INNER JOIN links ON nodes.id = links.target_id AND links.source_id = ? WHERE #{secure_scope('nodes')} UNION SELECT nodes.zip, links.relation_id, links.source_id FROM nodes INNER JOIN links ON nodes.id = links.source_id AND links.target_id = ? WHERE #{secure_scope('nodes')}}, self.id, self.id]

Zena::Db.select_all(sql).each do |record|
if record['relation_id'].to_i > 0
(roles[record['relation_id']] ||= []) << record
else
res["parent_id"] = record['zip']
end
(roles[record['relation_id']] ||= []) << record
end

res["parent_id"] = parent_zip.to_s

roles.each do |relation_id, records|
relation = secure(Relation) { Relation.find(relation_id) }
records.each do |record|
Expand Down

0 comments on commit 954c324

Please sign in to comment.