20151109000001_create_chat.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. class CreateChat < ActiveRecord::Migration
  2. def up
  3. =begin
  4. ActiveRecord::Migration.drop_table :chats
  5. ActiveRecord::Migration.drop_table :chat_topics
  6. ActiveRecord::Migration.drop_table :chat_sessions
  7. ActiveRecord::Migration.drop_table :chat_messages
  8. ActiveRecord::Migration.drop_table :chat_agents
  9. =end
  10. create_table :chats do |t|
  11. t.string :name, limit: 250, null: true
  12. t.integer :max_queue, null: false, default: 5
  13. t.string :note, limit: 250, null: true
  14. t.boolean :active, null: false, default: true
  15. t.integer :updated_by_id, null: false
  16. t.integer :created_by_id, null: false
  17. t.timestamps null: false
  18. end
  19. add_index :chats, [:name], unique: true
  20. create_table :chat_topics do |t|
  21. t.integer :chat_id, null: false
  22. t.string :name, limit: 250, null: false
  23. t.string :note, limit: 250, null: true
  24. t.integer :updated_by_id, null: false
  25. t.integer :created_by_id, null: false
  26. t.timestamps null: false
  27. end
  28. add_index :chat_topics, [:name], unique: true
  29. create_table :chat_sessions do |t|
  30. t.integer :chat_id, null: false
  31. t.string :session_id, null: false
  32. t.string :name, limit: 250, null: true
  33. t.string :state, limit: 50, null: false, default: 'waiting' # running, closed
  34. t.integer :user_id, null: true
  35. t.text :preferences, limit: 100.kilobytes + 1, null: true
  36. t.integer :updated_by_id, null: true
  37. t.integer :created_by_id, null: true
  38. t.timestamps null: false
  39. end
  40. add_index :chat_sessions, [:state]
  41. add_index :chat_sessions, [:user_id]
  42. create_table :chat_messages do |t|
  43. t.integer :chat_session_id, null: false
  44. t.string :content, limit: 5000, null: false
  45. t.integer :created_by_id, null: true
  46. t.timestamps null: false
  47. end
  48. create_table :chat_agents do |t|
  49. t.boolean :active, null: false, default: true
  50. t.integer :concurrent, null: false, default: 5
  51. t.integer :updated_by_id, null: false
  52. t.integer :created_by_id, null: false
  53. t.timestamps null: false
  54. end
  55. add_index :chat_agents, [:created_by_id], unique: true
  56. # return if it's a new setup
  57. return if !Setting.find_by(name: 'system_init_done')
  58. Role.create_if_not_exists(
  59. name: 'Chat',
  60. note: 'Access to chat feature.',
  61. updated_by_id: 1,
  62. created_by_id: 1
  63. )
  64. chat = Chat.create(
  65. name: 'default',
  66. max_queue: 5,
  67. note: '',
  68. active: true,
  69. updated_by_id: 1,
  70. created_by_id: 1,
  71. )
  72. chat_topic = Chat::Topic.create(
  73. chat_id: chat.id,
  74. name: 'default',
  75. updated_by_id: 1,
  76. created_by_id: 1,
  77. )
  78. end
  79. def down
  80. drop_table :chat_topics
  81. drop_table :chat_sessions
  82. drop_table :chat_messages
  83. drop_table :chat_agents
  84. drop_table :chats
  85. end
  86. end