tables.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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,
  41. ordered_amount CURRENCY NOT NULL,
  42. executed_amount CURRENCY DEFAULT 0 NOT NULL,
  43. expiry_dt DATETIME NOT NULL,
  44. FOREIGN KEY (ownership_id) REFERENCES ownership(rowid)
  45. )
  46. ''')
  47. cursor.execute('''
  48. CREATE TABLE IF NOT EXISTS order_history(
  49. rowid INTEGER PRIMARY KEY,
  50. ownership_id INTEGER NOT NULL,
  51. buy BOOLEAN NOT NULL,
  52. "limit" CURRENCY,
  53. ordered_amount CURRENCY NOT NULL,
  54. executed_amount CURRENCY NOT NULL,
  55. expiry_dt DATETIME NOT NULL,
  56. status VARCHAR(20) NOT NULL,
  57. order_id INTEGER NOT NULL, -- order_id is not a FOREIGN KEY since orders are deleted from order table afterwards
  58. archived_dt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  59. FOREIGN KEY (ownership_id) REFERENCES ownership(rowid)
  60. )
  61. ''')
  62. cursor.execute('''
  63. CREATE TABLE IF NOT EXISTS transactions(
  64. rowid INTEGER PRIMARY KEY,
  65. dt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  66. price CURRENCY NOT NULL,
  67. ownable_id INTEGER NOT NULL,
  68. amount CURRENCY NOT NULL,
  69. FOREIGN KEY (ownable_id) REFERENCES ownables(rowid)
  70. )
  71. ''')
  72. _add_column_if_not_exists(cursor, '''
  73. -- there is a not null constraint for new values that is watched by triggers
  74. ALTER TABLE transactions ADD COLUMN buyer_id INTEGER REFERENCES users(rowid)
  75. ''')
  76. _add_column_if_not_exists(cursor, '''
  77. -- there is a not null constraint for new values that is watched by triggers
  78. ALTER TABLE transactions ADD COLUMN seller_id INTEGER REFERENCES users(rowid)
  79. ''')
  80. cursor.execute('''
  81. CREATE TABLE IF NOT EXISTS keys(
  82. rowid INTEGER PRIMARY KEY,
  83. key STRING UNIQUE NOT NULL,
  84. used_by_user_id INTEGER,
  85. FOREIGN KEY (used_by_user_id) REFERENCES users(rowid)
  86. )
  87. ''')
  88. cursor.execute('''
  89. CREATE TABLE IF NOT EXISTS news(
  90. rowid INTEGER PRIMARY KEY,
  91. dt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  92. title VARCHAR(50) NOT NULL
  93. )
  94. ''')
  95. cursor.execute('''
  96. CREATE TABLE IF NOT EXISTS banks(
  97. rowid INTEGER PRIMARY KEY,
  98. user_id NOT NULL REFERENCES users(rowid)
  99. )
  100. ''')
  101. cursor.execute('''
  102. CREATE TABLE IF NOT EXISTS global_control_values(
  103. rowid INTEGER PRIMARY KEY,
  104. dt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  105. value_name VARCHAR NOT NULL,
  106. value FLOAT NOT NULL,
  107. UNIQUE (value_name, dt)
  108. )
  109. ''')
  110. _add_column_if_not_exists(cursor, '''
  111. -- there is a not null constraint for new values that is watched by triggers
  112. ALTER TABLE users ADD COLUMN salt BLOB NOT NULL DEFAULT 'orderer_is_a_cool_application_]{][{²$%WT§$%GV§$%SF$%&S$%FGGFHBDHJZIF254325'
  113. ''')
  114. def _add_column_if_not_exists(cursor, query):
  115. if 'ALTER TABLE' not in query.upper():
  116. raise ValueError('Only alter table queries allowed.')
  117. if 'ADD COLUMN' not in query.upper():
  118. raise ValueError('Only add column queries allowed.')
  119. try:
  120. cursor.execute(query)
  121. except OperationalError as e: # if the column already exists this will happen
  122. if 'duplicate column name' not in e.args[0]:
  123. raise
  124. else:
  125. pass