 
 
 
+(defn make-log-table-update-trigger-function []
+    (let [coils-trigger-fn-exists-result      (korma.core/exec-raw
+                                                  ["select exists(select * from pg_proc where proname = 'trigger_function_afterUpdate');" []]
+                                                  :results)
+
+          coils-trigger-fn-exists   (= "t"  coils-trigger-fn-exists-result)
+
+          sql-to-create-trigger-fn "
+CREATE OR REPLACE FUNCTION trigger_function_afterUpdate()
+      RETURNS trigger AS
+$BODY$
+      BEGIN
+           INSERT INTO coils_realtime_log
+                 (record_timestamp,  record_table_name,  record_id,  record_operation,  record_status)
+           VALUES
+                 ( now(),   TG_TABLE_NAME ,  NEW.id,  'UPDATE',  'WAITING');
+           RETURN NULL;
+      END;
+$BODY$
+LANGUAGE plpgsql;
+"
+          ]
+
+        (println "Coils trigger function exists: " coils-trigger-fn-exists)
+        (if (not coils-trigger-fn-exists )
+            (korma.core/exec-raw   [sql-to-create-trigger-fn []]   [])
+            nil
+            )
+        )
+    )
+
+
+
+
 
 
 (defn do-real [& {:keys [:table-name]}]
   (println "table name: " table-name)
   (make-admin-table )
   (make-log-table   )
-  ( make-log-table-trigger-function)
+  ( make-log-table-insert-trigger-function)
+  ( make-log-table-update-trigger-function)
   (create-realtime-trigger  :table-name  table-name)
 )
 

) values (?,?);
"

        sql-to-create-trigger "
CREATE TRIGGER trigger_afterInsert AFTER INSERT ON todo_items FOR EACH ROW EXECUTE PROCEDURE trigger_function_afterInsert();
"
        ]
        (println "Coils trigger table exists: " coils-trigger-exists)





(defn make-log-table-insert-trigger-function []
    (let [coils-trigger-fn-exists-result      (korma.core/exec-raw
                                                  ["select exists(select * from pg_proc where proname = 'trigger_function_afterinsert');" []]
                                                  :results)
