AI ニジカ綜合
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.

migration.py 3.1 KiB

2 days ago
2 days ago
2 days ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from __future__ import annotations
  2. from config import CONFIG
  3. from eloquent import DatabaseManager, Schema
  4. DB = DatabaseManager (CONFIG)
  5. SCHEMA = Schema (DB)
  6. def main (
  7. ) -> None:
  8. create_queries ()
  9. create_answers ()
  10. create_users ()
  11. create_query_answer_histories ()
  12. add_constraints_to_queries ()
  13. add_constraints_to_answers ()
  14. add_constraints_to_query_answer_histories ()
  15. def create_queries (
  16. ) -> None:
  17. with SCHEMA.create ('queries') as table:
  18. table.big_increments ('id')
  19. table.unsigned_big_integer ('user_id').nullable ().comment ('クエリ主')
  20. table.integer ('target_character').comment ('クエリ先キャラクタ')
  21. table.text ('content').comment ('クエリ内容')
  22. table.string ('image_url').nullable ().default (None).comment ('添附画像 URL')
  23. table.integer ('query_type').comment ('クエリ区分')
  24. table.integer ('model').comment ('GPT のモデル')
  25. table.datetime ('sent_at').comment ('送信日時')
  26. table.boolean ('answered').default (False).comment ('回答済')
  27. def add_constraints_to_queries (
  28. ) -> None:
  29. with SCHEMA.table ('queries') as table:
  30. table.foreign ('user_id').references ('id').on ('users').on_update ('cascade').on_delete ('restrict')
  31. def create_answers (
  32. ) -> None:
  33. with SCHEMA.create ('answers') as table:
  34. table.big_increments ('id')
  35. table.unsigned_big_integer ('query_id').nullable ().comment ('クエリ')
  36. table.integer ('character').comment ('キャラクタ区分')
  37. table.text ('content').comment ('回答内容')
  38. table.integer ('answer_type').comment ('回答区分')
  39. table.datetime ('sent_at').comment ('送信日時')
  40. table.boolean ('answered').default (False).comment ('回答済')
  41. def add_constraints_to_answers (
  42. ) -> None:
  43. with SCHEMA.table ('answers') as table:
  44. table.foreign ('query_id').references ('id').on ('queries').on_update ('cascade').on_delete ('restrict')
  45. def create_users (
  46. ) -> None:
  47. with SCHEMA.create ('users') as table:
  48. table.big_increments ('id')
  49. table.integer ('platform').comment ('プラットフォーム区分')
  50. table.string ('code').comment ('ユーザ・コード(プラットフォーム依存)')
  51. table.string ('name').comment ('ユーザ名(プラットフォーム内)')
  52. table.binary ('icon').nullable ().comment ('アイコン')
  53. table.unique (['platform', 'code'])
  54. def create_query_answer_histories (
  55. ) -> None:
  56. with SCHEMA.create ('query_answer_histories') as table:
  57. table.big_increments ('id')
  58. table.unsigned_big_integer ('query_id')
  59. table.unsigned_big_integer ('answer_id')
  60. table.unique (['query_id', 'answer_id'])
  61. def add_constraints_to_query_answer_histories (
  62. ) -> None:
  63. with SCHEMA.table ('query_answer_histories') as table:
  64. table.foreign ('query_id').references ('id').on ('queries').on_update ('cascade').on_delete ('cascade')
  65. table.foreign ('answer_id').references ('id').on ('answers').on_update ('cascade').on_delete ('cascade')
  66. if __name__ == '__main__':
  67. main ()