1
0

initialise_database.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. async def check_db(pg):
  2. await pg.fetchval("SELECT * FROM \"user\"")
  3. async def init_db(pg):
  4. queries = [
  5. "CREATE TABLE IF NOT EXISTS \
  6. guild (\
  7. id SERIAL PRIMARY KEY, \
  8. guild_id BIGINT UNIQUE NOT NULL, \
  9. report_deleted BOOL DEFAULT FALSE, \
  10. report_edited BOOL DEFAULT FALSE\
  11. )\
  12. ",
  13. "CREATE TABLE IF NOT EXISTS \
  14. channel (\
  15. id SERIAL PRIMARY KEY, \
  16. channel_id BIGINT UNIQUE NOT NULL, \
  17. guild BIGINT REFERENCES guild (guild_id), \
  18. interact BOOL DEFAULT FALSE, \
  19. games BOOL DEFAULT FALSE\
  20. )\
  21. ",
  22. "ALTER TABLE guild \
  23. ADD COLUMN output_channel BIGINT REFERENCES channel (channel_id) \
  24. ",
  25. "CREATE TABLE IF NOT EXISTS \
  26. settings (\
  27. id SERIAL PRIMARY KEY, \
  28. crew_channel_id BIGINT UNIQUE NOT NULL\
  29. )\
  30. ",
  31. # "CREATE TABLE IF NOT EXISTS \
  32. # channel_settings (\
  33. # id SERIAL PRIMARY KEY, \
  34. # channel BIGINT UNIQUE NOT NULL REFERENCES channel (channel_id), \
  35. # guild BIGINT REFERENCES guild (guild_id)\
  36. # )\
  37. # ",
  38. "CREATE TABLE IF NOT EXISTS \
  39. \"user\" (\
  40. id SERIAL PRIMARY KEY, \
  41. user_id BIGINT UNIQUE NOT NULL, \
  42. ignore BOOL DEFAULT FALSE, \
  43. level INT DEFAULT 0, \
  44. xp_spent INT DEFAULT 0, \
  45. invites_created INT DEFAULT 0, \
  46. integrations_created INT DEFAULT 0, \
  47. member_updated INT DEFAULT 0, \
  48. user_updated INT DEFAULT 0, \
  49. member_banned INT DEFAULT 0, \
  50. member_unbanned INT DEFAULT 0, \
  51. presence_updated INT DEFAULT 0, \
  52. messages_edited INT DEFAULT 0, \
  53. messages_deleted INT DEFAULT 0, \
  54. reacted INT DEFAULT 0, \
  55. events_created INT DEFAULT 0, \
  56. events_joined INT DEFAULT 0, \
  57. threads_created INT DEFAULT 0, \
  58. threads_joined INT DEFAULT 0, \
  59. ability_points_spent INT DEFAULT 0, \
  60. coin INT DEFAULT 0, \
  61. karma INT DEFAULT 0, \
  62. defence_skill INT DEFAULT 0, \
  63. attack_skill INT DEFAULT 0, \
  64. stealth_skill INT DEFAULT 0, \
  65. perception_skill INT DEFAULT 0, \
  66. theft_skill INT DEFAULT 0, \
  67. created TIMESTAMP NOT NULL DEFAULT now()\
  68. )\
  69. ",
  70. "CREATE TABLE IF NOT EXISTS \
  71. guild_access_token (\
  72. id SERIAL PRIMARY KEY, \
  73. guild BIGINT REFERENCES guild (guild_id), \
  74. \"user\" BIGINT NOT NULL REFERENCES \"user\" (user_id), \
  75. token varchar[40] UNIQUE NOT NULL, \
  76. created TIMESTAMP NOT NULL DEFAULT now()\
  77. )\
  78. ",
  79. "CREATE TABLE IF NOT EXISTS \
  80. channel_user (\
  81. id SERIAL PRIMARY KEY, \
  82. channel BIGINT NOT NULL REFERENCES channel (channel_id), \
  83. \"user\" BIGINT NOT NULL REFERENCES \"user\" (user_id), \
  84. total_messages BIGINT DEFAULT 1, \
  85. UNIQUE (channel, \"user\")\
  86. )\
  87. ",
  88. ]
  89. for query in queries:
  90. await pg.execute(query)