2023-12-20 (Wed) [長年日記]
_ MatzにっきでMongoDBを使うのをやめた
MatzにっきをHeroku化した際にMongoDBを使うようにしていたが、MongoDBが不自由なライセンスになってしまったのでDefaultIO(ファイル)に戻した。
移行用のスクリプトはなさそうだったのでtdiary-io-mongodbに同梱されているtdiary-mongodb-convertを以下のように書き換えて使用した。
#!/usr/bin/env ruby
require 'optparse'
require 'erb'
require 'tdiary/view_helper'
require 'tdiary/base'
require 'tdiary/comment'
require 'tdiary/comment_manager'
require 'tdiary/referer_manager'
require 'tdiary/style'
require 'tdiary/cache/file'
require 'ostruct'
require 'tdiary/core_ext'
require 'tdiary/io/default'
require 'tdiary/io/mongodb'
require 'tdiary/application'
def load_diaries(mongo_url, style_path)
options = {
'style.path' => style_path
}
conf = OpenStruct.new({
database_url: mongo_url,
options: options
})
tdiary = OpenStruct.new({conf: conf})
TDiary::IO::MongoDB.load_cgi_conf(conf)
mongo_io = TDiary::IO::MongoDB.new(tdiary)
mongo_io.calendar.each do |y, ms|
ms.each do |m|
puts "loading #{y}-#{m}"
month = Time.local(y.to_i, m.to_i)
mongo_io.transaction(month) do |diaries|
yield month, diaries
TDiary::TDiaryBase::DIRTY_NONE
end
end
end
end
def update_diaries(default_io, month, new_diaries)
default_io.transaction(month) do |diaries|
diaries.update(new_diaries)
TDiary::TDiaryBase::DIRTY_DIARY | TDiary::TDiaryBase::DIRTY_COMMENT
end
end
def store_diaries(data_path, style_path, mongo_url = nil)
options = {'style.path' => [style_path]}
conf = OpenStruct.new({options: options, data_path: "#{data_path}/"})
tdiary = OpenStruct.new({conf: conf})
TDiary::IO::Default.load_cgi_conf(conf)
default_io = TDiary::IO::Default.new(tdiary)
load_diaries(mongo_url, style_path) do |month, diaries|
p month
update_diaries(default_io, month, diaries)
end
end
def store_conf(conf_path, mongo_url = nil)
conf = OpenStruct.new({options: {}, database_url: mongo_url})
tdiary = OpenStruct.new({conf: conf})
s = TDiary::IO::MongoDB.load_cgi_conf(conf)
File.write(conf_path, s)
end
args = {}
OptionParser.new do |opts|
opts.banner = 'Usage: tdiary-mongodb-convert [options] <data_path>'
opts.on('-c CONF', '--conf=CONF', 'store only tdiary.conf'){|v| args[:conf] = v}
opts.on('-s PATH', '--style=STYLE', 'style path'){|v| args[:style] = v}
opts.on('-m URL', '--mongo=URL', 'URL of mongoDB'){|v| args[:mongo] = v}
opts.parse!(ARGV)
args[:data] = ARGV.shift
unless args[:data] || args[:conf]
$stderr.print opts.help
exit 1
end
end
if args[:conf]
store_conf(args[:conf], args[:mongo])
else
store_diaries(args[:data], args[:style], args[:mongo])
end
実行手順は以下のとおり(~/diary下のファイルが上書きされてしまうので注意)。
$ bundle exec tdiary_mongodb2default -s lib/tdiary/style -m <MongoDBのURL> ~/diary
$ bundle exec tdiary_mongodb2default -c ~/diary/tdiary.conf -m <MongoDBのURL>