1
0
Fork 0
forked from blue/pica

Changed database structure from default UTC_TIMESTAMP() which are not supposed to be supported to triggers

update transaction database method
This commit is contained in:
Blue 2024-04-14 21:16:36 -03:00
parent 973deaefd9
commit c2d4bf5ccb
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
3 changed files with 130 additions and 49 deletions

View file

@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS accounts (
`nick` VARCHAR(256),
`type` INTEGER UNSIGNED NOT NULL,
`password` VARCHAR(128),
`created` TIMESTAMP DEFAULT UTC_TIMESTAMP()
`created` TIMESTAMP
);
--creating role bindings table
@ -31,12 +31,12 @@ CREATE TABLE IF NOT EXISTS roleBindings (
FOREIGN KEY (role) REFERENCES roles(id)
);
--creating sessings table
--creating sessions table
CREATE TABLE IF NOT EXISTS sessions (
`id` INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`owner` INTEGER UNSIGNED NOT NULL,
`started` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
`latest` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
`started` TIMESTAMP,
`latest` TIMESTAMP,
`access` CHAR(32) NOT NULL UNIQUE,
`renew` CHAR(32),
`persist` BOOLEAN NOT NULL,
@ -51,7 +51,7 @@ CREATE TABLE IF NOT EXISTS currencies (
`code` VARCHAR(16) NOT NULL UNIQUE,
`title` VARCHAR(256),
`manual` BOOLEAN NOT NULL,
`added` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
`created` TIMESTAMP,
`type` INTEGER UNSIGNED NOT NULL,
`value` DECIMAL (20, 5) NOT NULL,
`source` TEXT,
@ -61,7 +61,7 @@ CREATE TABLE IF NOT EXISTS currencies (
INDEX manual_idx (manual)
);
--creating assests table
--creating assets table
CREATE TABLE IF NOT EXISTS assets (
`id` INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`owner` INTEGER UNSIGNED NOT NULL,
@ -72,7 +72,7 @@ CREATE TABLE IF NOT EXISTS assets (
`balance` DECIMAL (20, 5) DEFAULT 0,
`type` INTEGER UNSIGNED NOT NULL,
`archived` BOOLEAN DEFAULT FALSE,
`created` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
`created` TIMESTAMP,
INDEX owner_idx (owner),
INDEX archived_idx (archived),
@ -96,8 +96,8 @@ CREATE TABLE IF NOT EXISTS transactions (
`parent` INTEGER UNSIGNED,
`value` DECIMAL (20, 5) NOT NULL,
`state` INTEGER UNSIGNED DEFAULT 0,
`modified` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
`performed` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
`modified` TIMESTAMP,
`performed` TIMESTAMP,
`party` INTEGER UNSIGNED,
`notes` TEXT,
@ -114,7 +114,63 @@ CREATE TABLE IF NOT EXISTS transactions (
FOREIGN KEY (party) REFERENCES parties(id)
);
--creating defailt roles
--creating trigger before insert accounts
CREATE TRIGGER before_insert_accounts
BEFORE INSERT ON accounts
FOR EACH ROW
BEGIN
SET NEW.created = UTC_TIMESTAMP();
END;
--creating trigger before insert sessions
CREATE TRIGGER before_insert_sessions
BEFORE INSERT ON sessions
FOR EACH ROW
BEGIN
SET NEW.started = UTC_TIMESTAMP();
SET NEW.latest = UTC_TIMESTAMP();
END;
--creating trigger before insert currencies
CREATE TRIGGER before_insert_currencies
BEFORE INSERT ON currencies
FOR EACH ROW
BEGIN
IF NEW.created IS NULL THEN
SET NEW.created = UTC_TIMESTAMP();
END IF;
END;
--creating trigger before insert assets
CREATE TRIGGER before_insert_assets
BEFORE INSERT ON assets
FOR EACH ROW
BEGIN
IF NEW.created IS NULL THEN
SET NEW.created = UTC_TIMESTAMP();
END IF;
END;
--creating trigger before insert transactions
CREATE TRIGGER before_insert_transactions
BEFORE INSERT ON transactions
FOR EACH ROW
BEGIN
SET NEW.modified = UTC_TIMESTAMP();
IF NEW.performed IS NULL THEN
SET NEW.performed = UTC_TIMESTAMP();
END IF;
END;
--creating trigger before update transactions
CREATE TRIGGER before_update_transactions
BEFORE UPDATE ON transactions
FOR EACH ROW
BEGIN
SET NEW.modified = UTC_TIMESTAMP();
END;
--creating default roles
INSERT IGNORE INTO
roles (`name`)
VALUES ('root'),