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.

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