]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#90] dhcp6_server table defined
authorTomek Mrugalski <tomek@isc.org>
Mon, 17 May 2021 15:34:06 +0000 (17:34 +0200)
committerTomek Mrugalski <tomek@isc.org>
Thu, 28 Oct 2021 10:31:40 +0000 (12:31 +0200)
 My first table with a trigger. Woo hoo!

src/share/database/scripts/pgsql/upgrade_6.2_to_7.0.sh.in

index 4fc60209472a21c23c8b1ee603762a41ffd8af49..adb71bf0aa88b53c17c8af83abb6622dddfdaf8b 100644 (file)
@@ -54,6 +54,45 @@ CREATE TABLE parameter_data_type (
 INSERT INTO parameter_data_type VALUES (0,'integer'),(1,'real'),(2,'boolean'),(4,'string');
 
 
+DROP TABLE IF EXISTS `dhcp6_server`;
+CREATE TABLE dhcp6_server (
+  id SERIAL PRIMARY KEY NOT NULL,
+  tag varchar(256) UNIQUE NOT NULL,
+  description text DEFAULT NULL,
+  modification_ts timestamp NOT NULL DEFAULT Ccurrent_timestamp() ON UPDATE current_timestamp(),
+  KEY key_dhcp6_server_modification_ts (modification_ts)
+);
+
+
+DROP TABLE IF EXISTS dhcp6_server;
+CREATE TABLE dhcp6_server (
+  id SERIAL PRIMARY KEY NOT NULL,
+  tag varchar(256) NOT NULL,
+  description text DEFAULT NULL,
+  modification_ts timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  UNIQUE(tag)
+);
+
+CREATE INDEX dhcp6_server_modification_ts ON dhcp6_server (modification_ts);
+
+-- Adding on update trigger in MySQL is as easy as using this column definition in CREATE TABLE:
+-- modification_ts timestamp NOT NULL DEFAULT Ccurrent_timestamp() ON UPDATE current_timestamp(),
+-- Sadly, Postgres has its own convoluted way of doing this:
+
+-- First, we need to define a function that will do the actual job.
+CREATE OR REPLACE FUNCTION dhcp6_server_modification_ts_update()
+  RETURNS trigger AS $dhcp6_server_modification_ts_update$
+  BEGIN
+    new.modification_ts = CURRENT_TIMESTAMP;
+  END;
+
+-- Second, we need to specify which language it was written in.
+$dhcp6_server_modification_ts_update$ LANGUAGE plpgsql;
+
+-- Finally, we need to create a trigger that will kick off this function.
+CREATE TRIGGER dhcp6_server_modification_ts_update AFTER UPDATE ON dhcp6_server FOR EACH ROW EXECUTE PROCEDURE dhcp6_server_modification_ts_update();
+
+
 -- Update the schema version number
 UPDATE schema_version
     SET version = '7', minor = '0';