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.

105 lines
3.7 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_queries ()
  20. add_constraints_to_answers ()
  21. add_constraints_to_query_answer_histories ()
  22. def create_queries (
  23. ) -> None:
  24. with SCHEMA.create ('queries') as table:
  25. table.big_increments ('id')
  26. table.big_integer ('user_id').nullable ().comment ('クエリ主')
  27. table.integer ('target_character').comment ('クエリ先キャラクタ')
  28. table.text ('content').comment ('クエリ内容')
  29. table.string ('image_url').nullable ().default (None).comment ('添附画像 URL')
  30. table.integer ('query_type').comment ('クエリ区分')
  31. table.integer ('model').comment ('GPT のモデル')
  32. table.datetime ('sent_at').comment ('送信日時')
  33. table.boolean ('answered').default (False).comment ('回答済')
  34. def add_constraints_to_queries (
  35. ) -> None:
  36. with SCHEMA.create ('queries') as table:
  37. table.foreign ('user_id').references ('user_id').on ('users').on_update ('cascade').on_delete ('restrict')
  38. def create_answers (
  39. ) -> None:
  40. with SCHEMA.create ('answers') as table:
  41. table.big_increments ('id')
  42. table.big_integer ('query_id').nullable ().comment ('クエリ')
  43. table.integer ('character').comment ('キャラクタ区分')
  44. table.text ('content').comment ('回答内容')
  45. table.integer ('answer_type').comment ('回答区分')
  46. table.datetime ('sent_at').comment ('送信日時')
  47. table.boolean ('answered').default (False).comment ('回答済')
  48. def add_constraints_to_answers (
  49. ) -> None:
  50. with SCHEMA.create ('answers') as table:
  51. table.foreign ('query_id').references ('query_id').on ('queries').on_update ('cascade').on_delete ('restrict')
  52. def create_users (
  53. ) -> None:
  54. with SCHEMA.create ('users') as table:
  55. table.big_increments ('id')
  56. table.integer ('platform').comment ('プラットフォーム区分')
  57. table.string ('code').comment ('ユーザ・コード(プラットフォーム依存)')
  58. table.string ('name').comment ('ユーザ名(プラットフォーム内)')
  59. table.binary ('icon').nullable ().comment ('アイコン')
  60. table.unique (['platform', 'code'])
  61. def create_query_answer_histories (
  62. ) -> None:
  63. with SCHEMA.create ('query_answer_histories') as table:
  64. table.big_increments ('id')
  65. table.big_integer ('query_id')
  66. table.big_integer ('answer_id')
  67. table.unique (['query_id', 'answer_id'])
  68. def add_constraints_to_query_answer_histories (
  69. ) -> None:
  70. with SCHEMA.create ('query_answer_histories') as table:
  71. table.foreign ('query_id').references ('query_id').on ('queries').on_update ('cascade').on_delete ('cascade')
  72. table.foreign ('answer_id').references ('answer_id').on ('answers').on_update ('cascade').on_delete ('cascade')
  73. class DbConfig (TypedDict):
  74. driver: str
  75. host: str
  76. database: str
  77. user: str
  78. password: str
  79. prefix: str
  80. if __name__ == '__main__':
  81. main ()