正确处理ON DELETE CASCADE

This commit is contained in:
Yu Sun
2022-08-22 11:06:31 +08:00
parent c4f38267e9
commit a447918f54
2 changed files with 26 additions and 28 deletions

View File

@@ -50,30 +50,13 @@ class EConfigure {
} }
function remove_server() { function remove_server() {
pg_query($this -> dbconn, "BEGIN"); $res = pg_delete(
$exists = pg_query($this -> dbconn, sprintf( $this -> dbconn,
"SELECT EXISTS( 'hf_mes_scada_edgeserver_controller_server',
SELECT 1 FROM hf_mes_scada_edgeserver_controller_device (array) $this -> post,
WHERE server_id = '%s' );
)", $this -> post -> id
)); return $res ? true : "REMAINING";
if (pg_fetch_assoc($exists)['exists'] === 't') {
pg_query($this -> dbconn, "ROLLBACK");
return "REMAINING";
} else {
$res = pg_delete(
$this -> dbconn,
'hf_mes_scada_edgeserver_controller_server',
(array) $this -> post,
);
if ($res) {
pg_query($this -> dbconn, "COMMIT");
return true;
} else {
pg_query($this -> dbconn, "ROLLBACK");
return false;
}
}
} }
function update_server() { function update_server() {

View File

@@ -12,8 +12,20 @@ function init_db($server_name, $port, $user, $password) {
pg_query($dbconn, "CREATE EXTENSION IF NOT EXISTS timescaledb"); pg_query($dbconn, "CREATE EXTENSION IF NOT EXISTS timescaledb");
pg_query( pg_query(
$dbconn, $dbconn,
"CREATE TYPE category "DO $$ BEGIN
AS ENUM ('DEVICE_STATUS', 'ITEM_ID', 'DEVICE_DATA', 'PROCESS_DATA', 'RESULT_DATA', 'STATISTICAL_DATA')" CREATE TYPE category
AS ENUM (
'DEVICE_STATUS',
'ITEM_ID',
'DEVICE_DATA',
'PROCESS_DATA',
'RESULT_DATA',
'STATISTICAL_DATA'
);
EXCEPTION
WHEN duplicate_object
THEN null;
END $$"
); );
pg_query($dbconn, "CREATE TABLE IF NOT EXISTS hf_mes_scada_data_capture_node_configure ( pg_query($dbconn, "CREATE TABLE IF NOT EXISTS hf_mes_scada_data_capture_node_configure (
id serial2 primary key, id serial2 primary key,
@@ -42,14 +54,17 @@ function init_db($server_name, $port, $user, $password) {
id serial2 primary key, id serial2 primary key,
code text, code text,
name text NOT NULL, name text NOT NULL,
server_id serial2 references hf_mes_scada_edgeserver_controller_server(id), server_id serial2
REFERENCES hf_mes_scada_edgeserver_controller_server(id),
conf json, conf json,
create_date timestamp NOT NULL DEFAULT NOW(), create_date timestamp NOT NULL DEFAULT NOW(),
note text note text
)"); )");
pg_query($dbconn, "CREATE TABLE IF NOT EXISTS hf_mes_scada_edgeserver_controller_command ( pg_query($dbconn, "CREATE TABLE IF NOT EXISTS hf_mes_scada_edgeserver_controller_command (
id serial8 primary key, id serial8 primary key,
server_id serial2 references hf_mes_scada_edgeserver_controller_server(id), server_id serial2
REFERENCES hf_mes_scada_edgeserver_controller_server(id)
ON DELETE CASCADE,
device_name text, device_name text,
command text, command text,
create_date timestamp NOT NULL DEFAULT NOW(), create_date timestamp NOT NULL DEFAULT NOW(),