moved sql migration.

This commit is contained in:
didier amyot committed 2023-01-09 09:03:50 -05:00
1 parent 688d1005bf
commit d74d8b9d3e
4 files changed
+285 -2

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
CREATE TABLE IF NOT EXISTS users
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
public_key TEXT NOT NULL,
creation_time DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS security_types
(
name TEXT PRIMARY KEY
) STRICT;
INSERT INTO security_types(name)
VALUES ('currency'),
('bond');
CREATE TABLE IF NOT EXISTS securities
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
security_type TEXT NOT NULL,
name TEXT NOT NULL UNIQUE,
symbol TEXT NOT NULL UNIQUE,
creation_time DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (security_type) REFERENCES security_types (name)
);
CREATE TABLE IF NOT EXISTS account_types
(
name TEXT PRIMARY KEY
) STRICT;
INSERT INTO account_types(name)
VALUES ('checking'),
('security_emitter');
CREATE TABLE IF NOT EXISTS accounts
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
security INTEGER NOT NULL,
account_type TEXT NOT NULL,
amount INTEGER NOT NULL,
timestamp DATETIME NOT NULL,
creation_time DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (security) REFERENCES securities (id),
FOREIGN KEY (account_type) REFERENCES account_types (name)
);
CREATE TABLE IF NOT EXISTS account_user_permissions
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
account_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
creation_time DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (account_id) REFERENCES accounts (id),
FOREIGN KEY (user_id) REFERENCES users (id)
);
CREATE TABLE IF NOT EXISTS cashed_cheques
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
drawer_user_id INTEGER NOT NULL,
drawer_account_id INTEGER NOT NULL,
cheque_number INTEGER NOT NULL,
amount INTEGER NOT NULL,
underlying_security INTEGER NOT NULL,
payee_user_id INTEGER NOT NULL,
payee_account_id INTEGER NOT NULL,
creation_time DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (underlying_security) REFERENCES securities (id),
FOREIGN KEY (drawer_user_id) REFERENCES users (id),
FOREIGN KEY (drawer_account_id) REFERENCES accounts (id),
FOREIGN KEY (payee_user_id) REFERENCES users (id),
FOREIGN KEY (payee_account_id) REFERENCES accounts (id),
CONSTRAINT double_spending UNIQUE (drawer_user_id, cheque_number) -- no double spending
);