forked from secure-device-onboard/supply-chain-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmt_create.sql
More file actions
160 lines (131 loc) · 5.51 KB
/
mt_create.sql
File metadata and controls
160 lines (131 loc) · 5.51 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
-- Copyright 2020 Intel Corporation
-- SPDX-License-Identifier: Apache 2.0
-- 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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema sdo
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema sdo
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `sdo` DEFAULT CHARACTER SET utf8 ;
USE `sdo` ;
-- -----------------------------------------------------
-- Table `sdo`.`mt_device_state`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `sdo`.`mt_device_state` ;
CREATE TABLE IF NOT EXISTS `sdo`.`mt_device_state` (
`device_serial_no` VARCHAR(128) NOT NULL,
`di_start_datetime` DATETIME NOT NULL,
`di_end_datetime` DATETIME NULL,
`session_data` LONGTEXT NULL,
`status` INT NULL COMMENT 'In-process = 0, pass = 1, fail = -1, or timeout = -2.',
`details` VARCHAR(2048) NULL,
PRIMARY KEY (`device_serial_no`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `sdo`.`mt_server_settings`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `sdo`.`mt_server_settings` ;
CREATE TABLE IF NOT EXISTS `sdo`.`mt_server_settings` (
`id` INT NOT NULL,
`rendezvous_info` LONGTEXT NULL,
`certificate_validity_period` VARCHAR(128) NULL COMMENT 'ISO 8601 format',
PRIMARY KEY (`id`))
ENGINE = InnoDB;
USE `sdo` ;
-- -----------------------------------------------------
-- Placeholder table for view `sdo`.`v_mt_device_state`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sdo`.`v_mt_device_state` (`di_start_datetime` INT, `di_end_datetime` INT, `device_serial_no` INT, `details` INT, `Col_placeholder1` INT);
-- -----------------------------------------------------
-- Placeholder table for view `sdo`.`v_mt_version`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sdo`.`v_mt_version` (`0` INT);
-- -----------------------------------------------------
-- Placeholder table for view `sdo`.`v_mt_server_settings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sdo`.`v_mt_server_settings` (`rendezvous_info` INT, `certificate_validity_period` INT);
-- -----------------------------------------------------
-- procedure mt_timeout_device_sessions
-- -----------------------------------------------------
USE `sdo`;
DROP procedure IF EXISTS `sdo`.`mt_timeout_device_sessions`;
DELIMITER $$
USE `sdo`$$
create procedure mt_timeout_device_sessions(IN timeout_minutes int, out count int)
BEGIN
update device_state set status = -2 where timestampdiff(MINUTE, current_date(), di_start_datetime) > timeout_minutes;
set count = (select row_count());
END$$
DELIMITER ;
-- -----------------------------------------------------
-- procedure mt_purge_device_state
-- -----------------------------------------------------
USE `sdo`;
DROP procedure IF EXISTS `sdo`.`mt_purge_device_state`;
DELIMITER $$
USE `sdo`$$
CREATE PROCEDURE mt_purge_device_state (in older_than_minutes int, out count int)
BEGIN
delete from device_state where timestampdiff(MINUTE, current_date(), di_end_datetime) > older_than_minutes;
set count = (select row_count());
END$$
DELIMITER ;
-- -----------------------------------------------------
-- procedure mt_add_server_settings
-- -----------------------------------------------------
USE `sdo`;
DROP procedure IF EXISTS `sdo`.`mt_add_server_settings`;
DELIMITER $$
USE `sdo`$$
create procedure mt_add_server_settings
(
rendezvous_info mediumtext,
certificate_validity_period varchar(45)
)
BEGIN
replace into mt_server_settings (id, rendezvous_info, certificate_validity_period)
values (1, rendezvous_info, certificate_validity_period);
END$$
DELIMITER ;
-- -----------------------------------------------------
-- View `sdo`.`v_mt_device_state`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `sdo`.`v_mt_device_state`;
DROP VIEW IF EXISTS `sdo`.`v_mt_device_state` ;
USE `sdo`;
CREATE OR REPLACE VIEW `v_mt_device_state` AS
select
ds.di_start_datetime, ds.di_end_datetime,
ds.device_serial_no,
ds.details,
case when ds.status = 0 then 'IN-PROCESS'
when ds.status = 1 then 'SUCCESS'
when ds.status = -1 then 'FAIL'
when ds.status = -2 then 'TIMED OUT'
else 'Unknown' end AS 'Col_placeholder1'
from mt_device_state ds;
-- -----------------------------------------------------
-- View `sdo`.`v_mt_version`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `sdo`.`v_mt_version`;
DROP VIEW IF EXISTS `sdo`.`v_mt_version` ;
USE `sdo`;
CREATE OR REPLACE VIEW `v_mt_version` AS
# this simply returns the current version of the mt database
select 0;
-- -----------------------------------------------------
-- View `sdo`.`v_mt_server_settings`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `sdo`.`v_mt_server_settings`;
DROP VIEW IF EXISTS `sdo`.`v_mt_server_settings` ;
USE `sdo`;
CREATE OR REPLACE VIEW `v_mt_server_settings` AS
select rendezvous_info, certificate_validity_period
from mt_server_settings;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;