Boolean values are imported as 0/1 to MySQL

This commit is contained in:
Alexey Verkhovsky 2006-03-11 23:52:31 +00:00
parent 04b75e1684
commit b1d01b274e

View file

@ -96,6 +96,7 @@ ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(di
require 'webrick' require 'webrick'
require 'wiki_service' require 'wiki_service'
# substitute an extremely expensive method with something cheap.
class Revision class Revision
alias :__display_content :display_content alias :__display_content :display_content
def display_content def display_content
@ -113,19 +114,23 @@ def sql_insert(table, hash)
columns = hash.keys columns = hash.keys
values = columns.map { |column| hash[column] } values = columns.map { |column| hash[column] }
values = values.map do |val| values = values.map do |value|
if val.nil? if value.nil?
'NULL' 'NULL'
else else
if (value == false or value == true) and OPTIONS[:database] == 'mysql'
value = value ? '1' : '0'
end
case OPTIONS[:database] case OPTIONS[:database]
when 'mysql', 'postgres' when 'mysql', 'postgres'
escaped_value = val.to_s.gsub("'", "\\\\'") value = value.to_s.gsub("'", "\\\\'")
when 'sqlite' when 'sqlite'
escaped_value = val.to_s.gsub("'", "''") value = value.to_s.gsub("'", "''")
else else
raise "Unsupported database option #{OPTIONS[:database]}" raise "Unsupported database option #{OPTIONS[:database]}"
end end
"'#{escaped_value.gsub("\r\n", "\n")}'" "'#{value.gsub("\r\n", "\n")}'"
end end
end end
@ -160,9 +165,13 @@ WikiService.storage_path = OPTIONS[:storage]
wiki = WikiService.instance wiki = WikiService.instance
File.open(OPTIONS[:outfile], 'w') { |outfile| File.open(OPTIONS[:outfile], 'w') { |outfile|
outfile.puts "BEGIN;"
delete_all(outfile) delete_all(outfile)
outfile.puts "COMMIT;"
wiki.webs.each_pair do |web_name, web| wiki.webs.each_pair do |web_name, web|
outfile.puts "BEGIN;"
outfile.puts sql_insert(:webs, { outfile.puts sql_insert(:webs, {
:id => next_id(:web), :id => next_id(:web),
:name => web.name, :name => web.name,
@ -180,6 +189,7 @@ File.open(OPTIONS[:outfile], 'w') { |outfile|
:created_at => web.pages.values.map { |p| p.revisions.first.created_at }.min.ansi, :created_at => web.pages.values.map { |p| p.revisions.first.created_at }.min.ansi,
:updated_at => web.pages.values.map { |p| p.revisions.last.created_at }.max.ansi :updated_at => web.pages.values.map { |p| p.revisions.last.created_at }.max.ansi
}) })
outfile.puts "COMMIT;"
puts "Web #{web_name} has #{web.pages.keys.size} pages" puts "Web #{web_name} has #{web.pages.keys.size} pages"
web.pages.each_pair do |page_name, page| web.pages.each_pair do |page_name, page|