|
@@ -1,35 +1,25 @@
|
|
|
-from game import CURRENCY_NAME
|
|
|
-from debug import debug
|
|
|
+from game import CURRENCY_NAME, MINIMUM_ORDER_AMOUNT
|
|
|
|
|
|
-replace = True and debug
|
|
|
|
|
|
-
|
|
|
-def setup(cursor):
|
|
|
+def setup(cursor, seed_tables=False):
|
|
|
print('Database setup...')
|
|
|
|
|
|
- if replace:
|
|
|
- drop_database(cursor)
|
|
|
+ drop_triggers(cursor)
|
|
|
|
|
|
tables(cursor)
|
|
|
|
|
|
- integrity_checks(cursor)
|
|
|
+ create_triggers(cursor)
|
|
|
|
|
|
- if replace: # TODO also seed new databases
|
|
|
+ if seed_tables:
|
|
|
seed(cursor)
|
|
|
|
|
|
|
|
|
-def drop_database(cursor):
|
|
|
- print(' - Dropping old tables...')
|
|
|
- cursor.execute("DROP TABLE IF EXISTS users")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS ownables")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS ownership")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS sessions")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS orders")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS transactions")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS keys")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS news")
|
|
|
+def drop_triggers(cursor):
|
|
|
+ print(' - Dropping all triggers...')
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS owned_amount_not_negative_after_insert")
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS owned_amount_not_negative_after_update")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS amount_positive_after_insert")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS amount_positive_after_update")
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS order_limit_not_negative_after_insert")
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS order_limit_not_negative_after_update")
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS order_amount_positive_after_insert")
|
|
@@ -38,7 +28,23 @@ def drop_database(cursor):
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS not_more_executed_than_ordered_after_update")
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS expiry_dt_in_future_after_insert")
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS expiry_dt_in_future_after_update")
|
|
|
- cursor.execute("DROP TRIGGER IF EXISTS order_amount_positive_after_update")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS stop_loss_requires_limit_after_insert")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS stop_loss_requires_limit_after_update")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS limit_requires_stop_loss_after_insert")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS limit_requires_stop_loss_after_update")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS minimum_order_amount_after_insert")
|
|
|
+
|
|
|
+
|
|
|
+# def drop_database(cursor):
|
|
|
+# print(' - Dropping old tables...')
|
|
|
+# cursor.execute("DROP TABLE IF EXISTS users")
|
|
|
+# cursor.execute("DROP TABLE IF EXISTS ownables")
|
|
|
+# cursor.execute("DROP TABLE IF EXISTS ownership")
|
|
|
+# cursor.execute("DROP TABLE IF EXISTS sessions")
|
|
|
+# cursor.execute("DROP TABLE IF EXISTS orders")
|
|
|
+# cursor.execute("DROP TABLE IF EXISTS transactions")
|
|
|
+# cursor.execute("DROP TABLE IF EXISTS keys")
|
|
|
+# cursor.execute("DROP TABLE IF EXISTS news")
|
|
|
|
|
|
|
|
|
def seed(cursor):
|
|
@@ -61,152 +67,110 @@ def seed(cursor):
|
|
|
''', (CURRENCY_NAME,))
|
|
|
|
|
|
|
|
|
-def integrity_checks(cursor):
|
|
|
- print(' - Integrity checks...')
|
|
|
+def create_triggers(cursor):
|
|
|
+ print(' - Creating triggers...')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS owned_amount_not_negative_after_insert
|
|
|
- AFTER INSERT
|
|
|
- ON ownership
|
|
|
+ AFTER INSERT ON ownership
|
|
|
WHEN NEW.amount < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS owned_amount_not_negative_after_update
|
|
|
- AFTER UPDATE
|
|
|
- ON ownership
|
|
|
+ AFTER UPDATE ON ownership
|
|
|
WHEN NEW.amount < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS amount_positive_after_insert
|
|
|
- AFTER INSERT
|
|
|
- ON transactions
|
|
|
+ AFTER INSERT ON transactions
|
|
|
WHEN NEW.amount <= 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not perform empty transactions.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not perform empty transactions.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS amount_positive_after_update
|
|
|
- AFTER UPDATE
|
|
|
- ON transactions
|
|
|
+ AFTER UPDATE ON transactions
|
|
|
WHEN NEW.amount <= 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not perform empty transactions.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not perform empty transactions.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS order_limit_not_negative_after_insert
|
|
|
- AFTER INSERT
|
|
|
- ON orders
|
|
|
+ AFTER INSERT ON orders
|
|
|
WHEN NEW."limit" IS NOT NULL AND NEW."limit" < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not set a limit less than 0.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not set a limit less than 0.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS order_limit_not_negative_after_update
|
|
|
- AFTER UPDATE
|
|
|
- ON orders
|
|
|
+ AFTER UPDATE ON orders
|
|
|
WHEN NEW."limit" IS NOT NULL AND NEW."limit" < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not set a limit less than 0.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not set a limit less than 0.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS order_amount_positive_after_insert
|
|
|
- AFTER INSERT
|
|
|
- ON orders
|
|
|
+ AFTER INSERT ON orders
|
|
|
WHEN NEW.ordered_amount <= 0 OR NEW.executed_amount < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not order 0 or less.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not order 0 or less.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS order_amount_positive_after_update
|
|
|
- AFTER UPDATE
|
|
|
- ON orders
|
|
|
+ AFTER UPDATE ON orders
|
|
|
WHEN NEW.ordered_amount <= 0 OR NEW.executed_amount < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not order 0 or less.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not order 0 or less.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS not_more_executed_than_ordered_after_insert
|
|
|
- AFTER INSERT
|
|
|
- ON orders
|
|
|
+ AFTER INSERT ON orders
|
|
|
WHEN NEW.ordered_amount < NEW.executed_amount
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not execute more than ordered.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not execute more than ordered.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS not_more_executed_than_ordered_after_update
|
|
|
- AFTER UPDATE
|
|
|
- ON orders
|
|
|
+ AFTER UPDATE ON orders
|
|
|
WHEN NEW.ordered_amount < NEW.executed_amount
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not execute more than ordered.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not execute more than ordered.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS expiry_dt_in_future_after_insert
|
|
|
- AFTER INSERT
|
|
|
- ON orders
|
|
|
+ AFTER INSERT ON orders
|
|
|
WHEN NEW.expiry_dt <= datetime('now')
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Order is already expired.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Order is already expired.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS expiry_dt_in_future_after_update
|
|
|
- AFTER UPDATE
|
|
|
- ON orders
|
|
|
+ AFTER UPDATE ON orders
|
|
|
WHEN NEW.expiry_dt <= datetime('now')
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Order is already expired.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Order is already expired.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS stop_loss_requires_limit_after_insert
|
|
|
- AFTER INSERT
|
|
|
- ON orders
|
|
|
+ AFTER INSERT ON orders
|
|
|
WHEN NEW."limit" IS NULL AND NEW.stop_loss IS NOT NULL
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can only set `stop_loss` `for limit orders.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can only set `stop_loss` `for limit orders.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS stop_loss_requires_limit_after_update
|
|
|
- AFTER UPDATE
|
|
|
- ON orders
|
|
|
+ AFTER UPDATE ON orders
|
|
|
WHEN NEW."limit" IS NULL AND NEW.stop_loss IS NOT NULL
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can only set `stop_loss` `for limit orders.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can only set `stop_loss` `for limit orders.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS limit_requires_stop_loss_after_insert
|
|
|
- AFTER INSERT
|
|
|
- ON orders
|
|
|
+ AFTER INSERT ON orders
|
|
|
WHEN NEW."limit" IS NOT NULL AND NEW.stop_loss IS NULL
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Need to set stop_loss to either True or False for limit orders.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Need to set stop_loss to either True or False for limit orders.'); END
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS limit_requires_stop_loss_after_update
|
|
|
- AFTER UPDATE
|
|
|
- ON orders
|
|
|
+ AFTER UPDATE ON orders
|
|
|
WHEN NEW."limit" IS NOT NULL AND NEW.stop_loss IS NULL
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Need to set stop_loss to either True or False for limit orders.');
|
|
|
- END
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Need to set stop_loss to either True or False for limit orders.'); END
|
|
|
''')
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS minimum_order_amount_after_insert
|
|
|
+ AFTER INSERT ON orders
|
|
|
+ WHEN NEW.ordered_amount < ?
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'There is a minimum amount for new orders.'); END
|
|
|
+ '''.replace('?',str(MINIMUM_ORDER_AMOUNT)))
|
|
|
|
|
|
|
|
|
def tables(cursor):
|