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.

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