-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
148 lines (126 loc) · 5.32 KB
/
schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
-- MySQL Workbench Forward Engineering
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema asybalance
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema asybalance
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `asybalance` DEFAULT CHARACTER SET utf8mb4 ;
USE `asybalance` ;
-- -----------------------------------------------------
-- Table `asybalance`.`ab_providers`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `asybalance`.`ab_providers` ;
CREATE TABLE IF NOT EXISTS `asybalance`.`ab_providers` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`type` VARCHAR(255) CHARACTER SET 'latin1' NOT NULL,
`data` MEDIUMBLOB NOT NULL,
`version` INT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE INDEX `type_UNIQUE` (`type` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `asybalance`.`ab_executions`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `asybalance`.`ab_executions` ;
CREATE TABLE IF NOT EXISTS `asybalance`.`ab_executions` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`status` ENUM('INPROGRESS', 'SUCCESS', 'ERROR') NOT NULL DEFAULT 'INPROGRESS',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`finished_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`prefs` MEDIUMTEXT NULL,
`result` MEDIUMTEXT NULL,
`account_id` INT(11) NOT NULL,
`code_image` MEDIUMBLOB NULL,
`code_till` DATETIME NULL,
`code_params` TEXT NULL,
PRIMARY KEY (`id`),
INDEX `fk_execution_account1_idx` (`account_id` ASC),
CONSTRAINT `fk_execution_account1`
FOREIGN KEY (`account_id`)
REFERENCES `asybalance`.`ab_accounts` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `asybalance`.`ab_accounts`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `asybalance`.`ab_accounts` ;
CREATE TABLE IF NOT EXISTS `asybalance`.`ab_accounts` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`provider_id` INT NOT NULL,
`execution_id` BIGINT NULL,
`user_id` VARCHAR(64) CHARACTER SET 'latin1' NULL,
`name` VARCHAR(255) NULL,
`last_status` ENUM('INPROGRESS', 'SUCCESS', 'ERROR') NULL,
`last_result` MEDIUMTEXT NULL,
`last_result_time` DATETIME NULL,
`prefs` MEDIUMTEXT NULL,
`saved_data` MEDIUMTEXT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
INDEX `fk_account_provider_idx` (`provider_id` ASC),
INDEX `fk_account_execution1_idx` (`execution_id` ASC),
INDEX `user_id` (`user_id` ASC),
CONSTRAINT `fk_account_provider`
FOREIGN KEY (`provider_id`)
REFERENCES `asybalance`.`ab_providers` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_account_execution1`
FOREIGN KEY (`execution_id`)
REFERENCES `asybalance`.`ab_executions` (`id`)
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
-- -----------------------------------------------------
-- Table `asybalance`.`ab_execution_logs`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `asybalance`.`ab_execution_logs` ;
CREATE TABLE IF NOT EXISTS `asybalance`.`ab_execution_logs` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`content` MEDIUMTEXT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`execution_id` BIGINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_execution_log_execution1_idx` (`execution_id` ASC),
CONSTRAINT `fk_execution_log_execution1`
FOREIGN KEY (`execution_id`)
REFERENCES `asybalance`.`ab_executions` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
USE `asybalance`;
DELIMITER $$
USE `asybalance`$$
DROP TRIGGER IF EXISTS `asybalance`.`ab_executions_AFTER_INSERT` $$
USE `asybalance`$$
CREATE DEFINER = CURRENT_USER TRIGGER `asybalance`.`ab_executions_AFTER_INSERT` AFTER INSERT ON `ab_executions` FOR EACH ROW
BEGIN
UPDATE ab_accounts acc SET acc.last_status=NEW.status, acc.execution_id=NEW.id WHERE acc.id = NEW.account_id;
END$$
USE `asybalance`$$
DROP TRIGGER IF EXISTS `asybalance`.`ab_executions_AFTER_UPDATE` $$
USE `asybalance`$$
CREATE DEFINER = CURRENT_USER TRIGGER `asybalance`.`ab_executions_AFTER_UPDATE` AFTER UPDATE ON `ab_executions` FOR EACH ROW
BEGIN
IF (OLD.status <> NEW.status AND NEW.status <> 'INPROGRESS') THEN
UPDATE ab_accounts acc SET acc.last_status=NEW.status, acc.last_result=NEW.result, acc.last_result_time=NOW() WHERE acc.id=NEW.account_id AND acc.execution_id=NEW.id;
END IF;
END$$
DELIMITER ;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;