initialise_database.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. async def init_db(pg):
  2. queries = [
  3. "CREATE TABLE IF NOT EXISTS \
  4. guild (\
  5. id SERIAL PRIMARY KEY, \
  6. guild_id BIGINT UNIQUE NOT NULL, \
  7. output_channel BIGINT REFERENCES channel (channel_id), \
  8. report_deleted BOOL DEFAULT FALSE\
  9. )\
  10. ",
  11. "CREATE TABLE IF NOT EXISTS \
  12. channel (\
  13. id SERIAL PRIMARY KEY, \
  14. channel_id BIGINT UNIQUE NOT NULL, \
  15. guild BIGINT REFERENCES guild (guild_id), \
  16. interact BOOL DEFAULT FALSE, \
  17. games BOOL DEFAULT FALSE\
  18. )\
  19. ",
  20. "CREATE TABLE IF NOT EXISTS \
  21. settings (\
  22. id SERIAL PRIMARY KEY, \
  23. crew_channel_id BIGINT UNIQUE NOT NULL\
  24. )\
  25. ",
  26. # "CREATE TABLE IF NOT EXISTS \
  27. # channel_settings (\
  28. # id SERIAL PRIMARY KEY, \
  29. # channel BIGINT UNIQUE NOT NULL REFERENCES channel (channel_id), \
  30. # guild BIGINT REFERENCES guild (guild_id)\
  31. # )\
  32. # ",
  33. "CREATE TABLE IF NOT EXISTS \
  34. \"user\" (\
  35. id SERIAL PRIMARY KEY, \
  36. user_id BIGINT UNIQUE NOT NULL, \
  37. ignore BOOL DEFAULT FALSE\
  38. )\
  39. ",
  40. "CREATE TABLE IF NOT EXISTS \
  41. guild_access_token (\
  42. id SERIAL PRIMARY KEY, \
  43. guild BIGINT REFERENCES guild (guild_id), \
  44. \"user\" BIGINT NOT NULL REFERENCES \"user\" (user_id), \
  45. token varchar[40] UNIQUE NOT NULL, \
  46. created TIMESTAMP NOT NULL DEFAULT now()\
  47. )\
  48. ",
  49. "CREATE TABLE IF NOT EXISTS \
  50. channel_user (\
  51. id SERIAL PRIMARY KEY, \
  52. channel BIGINT NOT NULL REFERENCES channel (channel_id), \
  53. \"user\" BIGINT NOT NULL REFERENCES \"user\" (user_id), \
  54. total_messages BIGINT DEFAULT 1, \
  55. UNIQUE (channel, \"user\")\
  56. )\
  57. ",
  58. ]
  59. for query in queries:
  60. await pg.execute(query)