initialise_database.py 1.9 KB

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