sqlite3 = require "lsqlite3"

db = assert sqlite3.open "../db/posts.db"

update_stmt = assert db\prepare "UPDATE Posts SET mdown=? WHERE id=?"

db\exec [[ BEGIN TRANSACTION; ]]

for post in db\nrows "SELECT * FROM Posts"
  with fout = io.open "../raw/posts/#{post.id}.textile.md", "r"
    unless fout
      db\exec [[ ROLLBACK; ]]
      error "can't open #{post.id}.textile.md"

    body = fout\read("*a")

    do
      update_stmt\bind_values body, tonumber(post.id)
      update_stmt\step!
      update_stmt\reset!

    fout\close!

db\exec [[ COMMIT; ]]
