You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

migrate.rb 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. require 'sequel'
  2. def migrate
  3. db = Sequel.connect(adapter: :postgres, database: 'vlv2')
  4. db.create_table :threads do
  5. primary_key :id
  6. String :title
  7. String :creator, index: true
  8. Integer :remote_id, index: true, unique: true
  9. String :last_post_creator
  10. DateTime :last_post_created_at
  11. DateTime :created_at, index: true
  12. end
  13. db.create_table :posts do
  14. primary_key :id
  15. String :body
  16. String :creator, index: true
  17. Integer :remote_id, index: true, unique: true
  18. foreign_key :thread_id, :threads
  19. DateTime :created_at, index: true
  20. end
  21. db.create_function(:update_last_post, <<-SQL, language: :plpgsql, returns: :trigger)
  22. BEGIN
  23. UPDATE threads
  24. SET
  25. last_post_created_at = NEW.created_at,
  26. last_post_creator = NEW.creator
  27. WHERE
  28. threads.id = NEW.thread_id;
  29. RETURN NEW;
  30. END
  31. SQL
  32. db.run(<<-SQL)
  33. CREATE TRIGGER update_last_post
  34. AFTER INSERT ON posts
  35. FOR EACH ROW
  36. EXECUTE PROCEDURE update_last_post();
  37. SQL
  38. end