Skip to content

Instantly share code, notes, and snippets.

@niku
Created February 28, 2019 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niku/b3361132e0d7cb5e5f6ab60bfb5a0560 to your computer and use it in GitHub Desktop.
Save niku/b3361132e0d7cb5e5f6ab60bfb5a0560 to your computer and use it in GitHub Desktop.
require "securerandom"
require "pg"
require "org-ruby"
require "unf"
module SaveLegacy
class Error < StandardError; end
db_setting = {
connect_timeout: 300,
host: "localhost",
user: "postgres",
dbname: "bindit_dev"
}
conn = PG.connect(db_setting)
time = Time.now.utc
encoder = PG::TextEncoder::Bytea.new
file_paths = Dir.glob("../nikulog/**/*").select { |path| File.file?(path) }
file_paths.each do |file_path|
File.open(file_path) do |file|
if File.extname(file_path) == ".org"
body = Orgmode::Parser.new(file.read, allow_include_files: true).to_html
url_path = file_path.sub(%r{^../nikulog}, "").sub(%r{\.org$}, ".html")
path = UNF::Normalizer.normalize(url_path, :nfkc)
conn.exec(<<SQL, [SecureRandom.uuid, path, encoder.encode(body), time, time])
INSERT INTO legacy_contents (id, path, body, inserted_at, updated_at) VALUES ($1, $2, $3, $4, $5);
SQL
else
body = file.read
body.force_encoding("US-ASCII")
url_path = file_path.sub(%r{^../nikulog}, "")
path = UNF::Normalizer.normalize(url_path, :nfkc)
conn.exec(<<SQL, [SecureRandom.uuid, path, encoder.encode(body), time, time])
INSERT INTO legacy_contents (id, path, body, inserted_at, updated_at) VALUES ($1, $2, $3, $4, $5);
SQL
end
end
print "."
rescue PG::UniqueViolation => _e
print ","
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment