db_man.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from pprint import pprint
  2. import psycopg2
  3. import asyncio
  4. import datetime
  5. async def get_db_config_from_url():
  6. DB_URL = 'postgres://tkdhlpgcbebdlg:cd7765e4f10eded0fecddd174a1b19e21cbb8cf6957c21289d8fd774f63efd91@ec2-54-228-9-90.eu-west-1.compute.amazonaws.com:5432/d2gkvsrqqt4jjg'
  7. li = DB_URL.split('/')
  8. DB_NAME = li[-1]
  9. li_2 = li[2].split(':')
  10. DB_USER = li_2[0]
  11. DB_PASS, DB_HOST = li_2[1].split('@')
  12. db_settings = {
  13. 'dbname': DB_NAME,
  14. 'user': DB_USER,
  15. 'password': DB_PASS,
  16. 'host': DB_HOST
  17. }
  18. return db_settings
  19. async def db_management():
  20. settings = asyncio.create_task(get_db_config_from_url())
  21. await asyncio.gather(settings)
  22. conn = psycopg2.connect(**settings.result())
  23. cur = conn.cursor()
  24. # --- drop tables ---
  25. """
  26. cur.execute('DROP TABLE stats;')
  27. cur.execute('DROP TABLE users;')
  28. """
  29. # cur.execute('DROP TABLE time;')
  30. # --- create tables ---
  31. """
  32. cur.execute("create table users(id serial primary key, username VARCHAR, chat_id VARCHAR unique);")
  33. cur.execute("create table stats(id serial primary key, user_id INTEGER references users, task_num INTEGER, right_answers INTEGER, all_answers INTEGER);")
  34. """
  35. # cur.execute("create table activity(id serial primary key, user_id INTEGER references users, date DATE, right_answers INTEGER);")
  36. #cur.execute("create table feedbacks(id serial primary key, user_id INTEGER references users, date DATE, feedback VARCHAR);")
  37. #cur.execute("create table time(id serial primary key, user_id INTEGER references users, task_num INTEGER, max_time INTEGER, min_time INTEGER, sum_time INTEGER, count INTEGER);")
  38. # --- insert old users ---
  39. """
  40. cur.execute("INSERT INTO users (username, chat_id) VALUES ('@Joyin1211', '906136828');")
  41. cur.execute("INSERT INTO users (username, chat_id) VALUES ('@lelmoo', '614462421');")
  42. cur.execute("INSERT INTO users (username, chat_id) VALUES ('@Aleksandr_Zhd', '1294053049');")
  43. cur.execute("INSERT INTO users (username, chat_id) VALUES ('@AlGaRitm2020', '1830477841');")
  44. cur.execute("INSERT INTO users (username, chat_id) VALUES ('@YulLog19', '1244102957');")
  45. cur.execute("INSERT INTO users (username, chat_id) VALUES ('@albert_gareev', '1283628271');")
  46. """
  47. # cur.execute("INSERT INTO users (username, chat_id) VALUES ('@insanet2', '1283628271');")
  48. # print(type(datetime.date.today()))
  49. # cur.execute("INSERT INTO activity (user_id, date, right_answers) VALUES (8, '{}', 7);".format('2021-07-07'))
  50. # --- delete data from tables ---
  51. """
  52. cur.execute("DELETE FROM stats;")
  53. cur.execute("DELETE FROM users WHERE id = 4;")
  54. """
  55. #cur.execute("DELETE FROM stats WHERE user_id =3;")
  56. #cur.execute(f"DELETE FROM activity WHERE user_id =8 AND date = {datetime.date(2021, 6, 9)}")
  57. #.format(datetime.date(2021, 1, 8)))
  58. #cur.execute("DELETE FROM users WHERE id = 3;")
  59. # cur.execute("DELETE FROM stats WHERE user_id = 6;")
  60. # cur.execute("UPDATE stats SET (right_answers, all_answers) = (12, 20) WHERE user_id = 8;")
  61. # cur.execute("INSERT INTO stats (task_num, user_id) VALUES (3, 8);")
  62. # cur.execute("alter table users add column register_date DATE default '11.27.2021';")
  63. # --- select data from users ---
  64. cur.execute("SELECT * FROM users;")
  65. print('users', 'id, username, chat_id, register_date', sep='\n')
  66. pprint(cur.fetchall())
  67. """
  68. # --- select data from stats ---
  69. cur.execute("SELECT user_id, task_num, right_answers, all_answers FROM stats")
  70. print('stats', 'id, user_id, task, right_answers, all_answers', sep='\n')
  71. pprint(cur.fetchall())
  72. """
  73. # --- select data from users ---
  74. cur.execute("SELECT user_id, date FROM activity;")
  75. print('id', 'user_id, date', sep='\n')
  76. pprint(cur.fetchall())
  77. # --- select data from feedback---
  78. cur.execute("SELECT user_id, date, feedback FROM feedbacks;")
  79. print('user_id, date, feedbacks', sep='\n')
  80. pprint(cur.fetchall())
  81. # --- select data from stats ---
  82. cur.execute("SELECT * FROM time")
  83. #print('stats', 'id, user_id, task, right_answers, all_answers', sep='\n')
  84. pprint(cur.fetchall())
  85. conn.commit()
  86. cur.close()
  87. conn.close()
  88. if __name__ == '__main__':
  89. asyncio.run(db_management())