tables.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from sqlite3 import OperationalError
  2. def tables(cursor):
  3. print(' - Creating tables...')
  4. cursor.execute('''
  5. CREATE TABLE IF NOT EXISTS users(
  6. rowid INTEGER PRIMARY KEY,
  7. username VARCHAR(10) UNIQUE NOT NULL,
  8. password VARCHAR(200) NOT NULL)
  9. ''')
  10. cursor.execute('''
  11. CREATE TABLE IF NOT EXISTS ownables(
  12. rowid INTEGER PRIMARY KEY,
  13. name VARCHAR(10) UNIQUE NOT NULL)
  14. ''')
  15. cursor.execute('''
  16. CREATE TABLE IF NOT EXISTS ownership(
  17. rowid INTEGER PRIMARY KEY,
  18. user_id INTEGER NOT NULL,
  19. ownable_id INTEGER NOT NULL,
  20. amount CURRENCY NOT NULL DEFAULT 0,
  21. FOREIGN KEY (user_id) REFERENCES users(rowid),
  22. FOREIGN KEY (ownable_id) REFERENCES ownables(rowid),
  23. UNIQUE (user_id, ownable_id)
  24. )
  25. ''')
  26. cursor.execute('''
  27. CREATE TABLE IF NOT EXISTS sessions(
  28. rowid INTEGER PRIMARY KEY,
  29. user_id INTEGER NOT NULL,
  30. session_id STRING NOT NULL,
  31. FOREIGN KEY (user_id) REFERENCES users(rowid)
  32. )
  33. ''')
  34. cursor.execute('''
  35. CREATE TABLE IF NOT EXISTS orders(
  36. rowid INTEGER PRIMARY KEY,
  37. ownership_id INTEGER NOT NULL,
  38. buy BOOLEAN NOT NULL,
  39. "limit" CURRENCY,
  40. stop_loss BOOLEAN, -- TODO clarify what it means if this is NULL
  41. ordered_amount CURRENCY NOT NULL,
  42. executed_amount CURRENCY DEFAULT 0 NOT NULL,
  43. expiry_dt TIMESTAMP NOT NULL,
  44. ioc BOOLEAN NOT NULL,
  45. FOREIGN KEY (ownership_id) REFERENCES ownership(rowid)
  46. )
  47. ''')
  48. cursor.execute('''
  49. CREATE TABLE IF NOT EXISTS order_history(
  50. rowid INTEGER PRIMARY KEY,
  51. ownership_id INTEGER NOT NULL,
  52. buy BOOLEAN NOT NULL,
  53. "limit" CURRENCY,
  54. ordered_amount CURRENCY NOT NULL,
  55. executed_amount CURRENCY NOT NULL,
  56. expiry_dt TIMESTAMP NOT NULL,
  57. status VARCHAR(20) NOT NULL,
  58. order_id INTEGER NOT NULL, -- order_id is not a FOREIGN KEY since orders are deleted from order table afterwards
  59. archived_dt TIMESTAMP NOT NULL DEFAULT (CAST(strftime('%s', CURRENT_TIMESTAMP) AS INTEGER)),
  60. FOREIGN KEY (ownership_id) REFERENCES ownership(rowid)
  61. )
  62. ''')
  63. cursor.execute('''
  64. CREATE TABLE IF NOT EXISTS transactions(
  65. rowid INTEGER PRIMARY KEY,
  66. dt TIMESTAMP NOT NULL DEFAULT (CAST(strftime('%s', CURRENT_TIMESTAMP) AS INTEGER)),
  67. price CURRENCY NOT NULL,
  68. ownable_id INTEGER NOT NULL,
  69. amount CURRENCY NOT NULL,
  70. FOREIGN KEY (ownable_id) REFERENCES ownables(rowid)
  71. )
  72. ''')
  73. _add_column_if_not_exists(cursor, '''
  74. -- there is a not null constraint for new values that is watched by triggers
  75. ALTER TABLE transactions ADD COLUMN buyer_id INTEGER REFERENCES users(rowid)
  76. ''')
  77. _add_column_if_not_exists(cursor, '''
  78. -- there is a not null constraint for new values that is watched by triggers
  79. ALTER TABLE transactions ADD COLUMN seller_id INTEGER REFERENCES users(rowid)
  80. ''')
  81. cursor.execute('''
  82. CREATE TABLE IF NOT EXISTS news(
  83. rowid INTEGER PRIMARY KEY,
  84. dt TIMESTAMP NOT NULL DEFAULT (CAST(strftime('%s', CURRENT_TIMESTAMP) AS INTEGER)),
  85. title VARCHAR(50) NOT NULL
  86. )
  87. ''')
  88. cursor.execute('''
  89. CREATE TABLE IF NOT EXISTS banks(
  90. rowid INTEGER PRIMARY KEY,
  91. user_id INTEGER UNIQUE NOT NULL REFERENCES users(rowid),
  92. last_deposit_facility_pay_dt TIMESTAMP NOT NULL DEFAULT (CAST(strftime('%s', CURRENT_TIMESTAMP) AS INTEGER))
  93. )
  94. ''')
  95. cursor.execute('''
  96. CREATE TABLE IF NOT EXISTS global_control_values(
  97. rowid INTEGER PRIMARY KEY,
  98. dt TIMESTAMP NOT NULL DEFAULT (CAST(strftime('%s', CURRENT_TIMESTAMP) AS INTEGER)),
  99. value_name VARCHAR NOT NULL,
  100. value FLOAT NOT NULL,
  101. UNIQUE (value_name, dt)
  102. )
  103. ''')
  104. cursor.execute('''
  105. CREATE TABLE IF NOT EXISTS tender_calendar(
  106. rowid INTEGER PRIMARY KEY,
  107. dt TIMESTAMP UNIQUE NOT NULL,
  108. mro_interest CURRENCY NOT NULL,
  109. maturity_dt TIMESTAMP NOT NULL,
  110. executed BOOLEAN NOT NULL DEFAULT FALSE
  111. )
  112. ''')
  113. cursor.execute('''
  114. CREATE TABLE IF NOT EXISTS loans(
  115. rowid INTEGER PRIMARY KEY,
  116. user_id INTEGER NOT NULL REFERENCES users(rowid),
  117. total_amount CURRENCY NOT NULL CHECK(total_amount > 0),
  118. amount CURRENCY NOT NULL CHECK(amount > 0),
  119. last_interest_pay_dt TIMESTAMP NOT NULL DEFAULT (CAST(strftime('%s', CURRENT_TIMESTAMP) AS INTEGER)),
  120. interest_rate CURRENCY NOT NULL -- determined from the global value 'personal_loan_interest_rate'
  121. )
  122. ''')
  123. cursor.execute('''
  124. CREATE TABLE IF NOT EXISTS credits(
  125. rowid INTEGER PRIMARY KEY,
  126. issuer_id INTEGER NOT NULL REFERENCES users(rowid),
  127. ownable_id INTEGER UNIQUE NOT NULL REFERENCES ownables(rowid),
  128. last_interest_pay_dt TIMESTAMP NOT NULL DEFAULT (CAST(strftime('%s', CURRENT_TIMESTAMP) AS INTEGER)),
  129. maturity_dt TIMESTAMP NOT NULL DEFAULT (CAST(strftime('%s', CURRENT_TIMESTAMP) AS INTEGER)),
  130. coupon CURRENCY NOT NULL -- fancy word for interest rate
  131. )
  132. ''')
  133. _add_column_if_not_exists(cursor, '''
  134. -- there is a not null constraint for new values that is watched by triggers
  135. ALTER TABLE users ADD COLUMN salt BLOB NOT NULL DEFAULT 'orderer_is_a_cool_application_]{][{²$%WT§$%GV§$%SF$%&S$%FGGFHBDHJZIF254325'
  136. ''')
  137. def _add_column_if_not_exists(cursor, query):
  138. if 'ALTER TABLE' not in query.upper():
  139. raise ValueError('Only alter table queries allowed.')
  140. if 'ADD COLUMN' not in query.upper():
  141. raise ValueError('Only add column queries allowed.')
  142. try:
  143. cursor.execute(query)
  144. except OperationalError as e: # if the column already exists this will happen
  145. if 'duplicate column name' not in e.args[0]:
  146. raise
  147. else:
  148. pass