initialise_database.py 2.0 KB

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