db_setup.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from game import CURRENCY_NAME
  2. def setup(cursor):
  3. print('Database setup...')
  4. replace = False
  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(200) NOT NULL)
  18. ''')
  19. cursor.execute('''
  20. CREATE TABLE IF NOT EXISTS ownables(
  21. name VARCHAR(10) UNIQUE NOT NULL)
  22. ''')
  23. cursor.execute('''
  24. CREATE TABLE IF NOT EXISTS ownership(
  25. user_id INTEGER NOT NULL,
  26. ownable_id INTEGER NOT NULL,
  27. amount CURRENCY NOT NULL DEFAULT 0,
  28. FOREIGN KEY (user_id) REFERENCES users(rowid),
  29. FOREIGN KEY (ownable_id) REFERENCES ownables(rowid),
  30. UNIQUE (user_id, ownable_id)
  31. )
  32. ''')
  33. cursor.execute('''
  34. CREATE TABLE IF NOT EXISTS sessions(
  35. user_id INTEGER NOT NULL,
  36. session_id STRING NOT NULL,
  37. FOREIGN KEY (user_id) REFERENCES users(rowid)
  38. )
  39. ''')
  40. cursor.execute('''
  41. CREATE TABLE IF NOT EXISTS orders(
  42. ownership_id INTEGER NOT NULL,
  43. buy BOOLEAN NOT NULL,
  44. "limit" CURRENCY,
  45. stop_loss BOOLEAN,
  46. ordered_amount CURRENCY,
  47. executed_amount CURRENCY DEFAULT 0,
  48. FOREIGN KEY (ownership_id) REFERENCES ownership(rowid)
  49. )
  50. ''')
  51. cursor.execute('''
  52. CREATE TABLE IF NOT EXISTS transactions(
  53. dt DATETIME NOT NULL,
  54. price CURRENCY NOT NULL,
  55. ownable_id INTEGER NOT NULL,
  56. FOREIGN KEY (ownable_id) REFERENCES ownable(rowid)
  57. )
  58. ''')
  59. cursor.execute('''
  60. CREATE TABLE IF NOT EXISTS keys(
  61. key STRING UNIQUE NOT NULL,
  62. used_by_user_id INTEGER,
  63. FOREIGN KEY (used_by_user_id) REFERENCES user(rowid)
  64. )
  65. ''')
  66. cursor.execute('''
  67. CREATE TABLE IF NOT EXISTS news(
  68. dt DATETIME NOT NULL,
  69. title VARCHAR(50) NOT NULL
  70. )
  71. ''')
  72. print(' - Integrity checks...')
  73. cursor.execute('''
  74. CREATE TRIGGER IF NOT EXISTS owned_amount_not_negative_after_insert
  75. AFTER INSERT
  76. ON ownership
  77. WHEN NEW.amount < 0
  78. BEGIN
  79. SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.');
  80. END
  81. ''')
  82. cursor.execute('''
  83. CREATE TRIGGER IF NOT EXISTS owned_amount_not_negative_after_update
  84. AFTER UPDATE
  85. ON ownership
  86. WHEN NEW.amount < 0
  87. BEGIN
  88. SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.');
  89. END
  90. ''')
  91. cursor.execute('''
  92. CREATE TRIGGER IF NOT EXISTS order_limit_not_negative_after_insert
  93. AFTER INSERT
  94. ON orders
  95. WHEN "limit" IS NOT NULL AND "limit" < 0
  96. BEGIN
  97. SELECT RAISE(ROLLBACK, 'Can not set a limit less than 0.');
  98. END
  99. ''')
  100. cursor.execute('''
  101. CREATE TRIGGER IF NOT EXISTS order_limit_not_negative_after_update
  102. AFTER UPDATE
  103. ON orders
  104. WHEN "limit" IS NOT NULL AND "limit" < 0
  105. BEGIN
  106. SELECT RAISE(ROLLBACK, 'Can not set a limit less than 0.');
  107. END
  108. ''')
  109. cursor.execute('''
  110. CREATE TRIGGER IF NOT EXISTS order_amount_not_negative_after_insert
  111. AFTER INSERT
  112. ON orders
  113. WHEN ordered_amount <= 0 OR executed_amount < 0
  114. BEGIN
  115. SELECT RAISE(ROLLBACK, 'Can not order 0 or less.');
  116. END
  117. ''')
  118. cursor.execute('''
  119. CREATE TRIGGER IF NOT EXISTS order_amount_not_negative_after_update
  120. AFTER UPDATE
  121. ON orders
  122. WHEN ordered_amount <= 0 OR executed_amount < 0
  123. BEGIN
  124. SELECT RAISE(ROLLBACK, 'Can not order 0 or less.');
  125. END
  126. ''')
  127. if replace: # TODO also seed new databases
  128. print(' - Seeding initial data...')
  129. cursor.execute('''
  130. INSERT INTO ownables
  131. (name)
  132. VALUES (?)
  133. ''', (CURRENCY_NAME,))
  134. cursor.execute('''
  135. INSERT INTO users
  136. (username,password)
  137. VALUES ('bank','')
  138. ''')
  139. cursor.execute('''
  140. INSERT INTO ownership
  141. (user_id, ownable_id)
  142. VALUES ((SELECT rowid FROM users WHERE username = 'bank'),
  143. (SELECT rowid FROM ownables WHERE name = ?))
  144. ''', (CURRENCY_NAME,))