db_setup.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from game import CURRENCY_NAME
  2. def setup(cursor):
  3. print('Database setup...')
  4. replace = True
  5. if replace:
  6. print(' - Dropping old tables...')
  7. cursor.execute("DROP TABLE IF EXISTS users")
  8. cursor.execute("DROP TABLE IF EXISTS ownables")
  9. cursor.execute("DROP TABLE IF EXISTS ownership")
  10. cursor.execute("DROP TABLE IF EXISTS sessions")
  11. cursor.execute("DROP TABLE IF EXISTS orders")
  12. cursor.execute("DROP TABLE IF EXISTS transactions")
  13. print(' - Creating tables...')
  14. cursor.execute('''
  15. CREATE TABLE IF NOT EXISTS users(
  16. username VARCHAR(10) UNIQUE NOT NULL,
  17. password VARCHAR(6) NOT NULL)
  18. ''')
  19. cursor.execute('''
  20. CREATE TABLE IF NOT EXISTS ownables(
  21. name VARCHAR(10) UNIQUE NOT NULL,
  22. total_amount CURRENCY NOT NULL DEFAULT 0)
  23. ''')
  24. cursor.execute('''
  25. CREATE TABLE IF NOT EXISTS ownership(
  26. user_id INTEGER NOT NULL,
  27. ownable_id INTEGER NOT NULL,
  28. amount CURRENCY NOT NULL DEFAULT 0,
  29. FOREIGN KEY (user_id) REFERENCES users(rowid),
  30. FOREIGN KEY (ownable_id) REFERENCES ownables(rowid),
  31. UNIQUE (user_id, ownable_id)
  32. )
  33. ''')
  34. cursor.execute('''
  35. CREATE TABLE IF NOT EXISTS sessions(
  36. user_id INTEGER NOT NULL,
  37. session_id STRING NOT NULL,
  38. FOREIGN KEY (user_id) REFERENCES users(rowid)
  39. )
  40. ''')
  41. cursor.execute('''
  42. CREATE TABLE IF NOT EXISTS orders(
  43. ownership_id INTEGER NOT NULL,
  44. buy BOOLEAN NOT NULL,
  45. "limit" CURRENCY,
  46. stop_loss BOOLEAN,
  47. ordered_amount CURRENCY,
  48. executed_amount CURRENCY,
  49. FOREIGN KEY (ownership_id) REFERENCES ownership(rowid)
  50. )
  51. ''')
  52. cursor.execute('''
  53. CREATE TABLE IF NOT EXISTS transactions(
  54. dt DATETIME NOT NULL,
  55. price CURRENCY NOT NULL,
  56. ownable_id INTEGER NOT NULL,
  57. FOREIGN KEY (ownable_id) REFERENCES ownable(rowid)
  58. )
  59. ''')
  60. cursor.execute('''
  61. CREATE TABLE IF NOT EXISTS keys(
  62. key STRING UNIQUE NOT NULL,
  63. used_by_user_id INTEGER,
  64. FOREIGN KEY (used_by_user_id) REFERENCES user(rowid)
  65. )
  66. ''')
  67. print(' - Integrity checks...')
  68. cursor.execute('''
  69. CREATE TRIGGER IF NOT EXISTS owned_amount_not_negative_after_insert
  70. AFTER INSERT
  71. ON ownership
  72. WHEN NEW.amount < 0
  73. BEGIN
  74. SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.');
  75. END
  76. ''')
  77. cursor.execute('''
  78. CREATE TRIGGER IF NOT EXISTS owned_amount_not_negative_after_update
  79. AFTER UPDATE
  80. ON ownership
  81. WHEN NEW.amount < 0
  82. BEGIN
  83. SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.');
  84. END
  85. ''')
  86. if replace: # TODO also seed new databases
  87. print(' - Seeding initial data...')
  88. cursor.execute('''
  89. INSERT INTO ownables
  90. (name)
  91. VALUES (?)
  92. ''', (CURRENCY_NAME,))
  93. cursor.execute('''
  94. INSERT INTO users
  95. (username,password)
  96. VALUES ('bank','')
  97. ''')
  98. cursor.execute('''
  99. INSERT INTO ownership
  100. (user_id, ownable_id)
  101. VALUES ((SELECT rowid FROM users WHERE username = 'bank'),
  102. (SELECT rowid FROM ownables WHERE name = ?))
  103. ''', (CURRENCY_NAME,))