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.

98 lines
3.4 KiB

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