From 0b573e50ead5813970dca16afaedbfbb0d91006e Mon Sep 17 00:00:00 2001
From: Ngoc Khuat <qn.khuat@gmail.com>
Date: Wed, 8 Jun 2022 22:06:41 +0700
Subject: [PATCH] add :updated-at-timestamped? property and use it on some
 models (#23220)

* add :updated-at-timestamped? property and use it on some models
---
 src/metabase/models/application_permissions_revision.clj    | 5 +----
 .../models/collection_permission_graph_revision.clj         | 6 +-----
 src/metabase/models/interface.clj                           | 4 ++++
 src/metabase/models/permissions_revision.clj                | 5 +----
 src/metabase/models/session.clj                             | 5 +++--
 5 files changed, 10 insertions(+), 15 deletions(-)

diff --git a/src/metabase/models/application_permissions_revision.clj b/src/metabase/models/application_permissions_revision.clj
index 4154daf9c91..cfa1256bd4c 100644
--- a/src/metabase/models/application_permissions_revision.clj
+++ b/src/metabase/models/application_permissions_revision.clj
@@ -6,15 +6,12 @@
 
 (models/defmodel ApplicationPermissionsRevision :application_permissions_revision)
 
-(defn- pre-insert [revision]
-  (assoc revision :created_at :%now))
-
 (u/strict-extend (class ApplicationPermissionsRevision)
   models/IModel
   (merge models/IModelDefaults
          {:types      (constantly {:before :json
                                    :after  :json})
-          :pre-insert pre-insert
+          :properties (constantly {:created-at-timestamped? true})
           :pre-update (fn [& _] (throw (Exception. (tru "You cannot update a ApplicationPermissionsRevision!"))))}))
 
 (defn latest-id
diff --git a/src/metabase/models/collection_permission_graph_revision.clj b/src/metabase/models/collection_permission_graph_revision.clj
index 0969d6ac7f1..ec33e4e4df6 100644
--- a/src/metabase/models/collection_permission_graph_revision.clj
+++ b/src/metabase/models/collection_permission_graph_revision.clj
@@ -6,18 +6,14 @@
 
 (models/defmodel CollectionPermissionGraphRevision :collection_permission_graph_revision)
 
-(defn- pre-insert [revision]
-  (assoc revision :created_at :%now))
-
 (u/strict-extend (class CollectionPermissionGraphRevision)
   models/IModel
   (merge models/IModelDefaults
          {:types      (constantly {:before :json
                                    :after  :json})
-          :pre-insert pre-insert
+          :properties (constantly {:created-at-timestamped? true})
           :pre-update (fn [& _] (throw (Exception. (tru "You cannot update a CollectionPermissionGraphRevision!"))))}))
 
-
 (defn latest-id
   "Return the ID of the newest `CollectionPermissionGraphRevision`, or zero if none have been made yet.
    (This is used by the collection graph update logic that checks for changes since the original graph was fetched)."
diff --git a/src/metabase/models/interface.clj b/src/metabase/models/interface.clj
index f4f2e7a9ff7..f8d81c34f23 100644
--- a/src/metabase/models/interface.clj
+++ b/src/metabase/models/interface.clj
@@ -227,6 +227,10 @@
   :insert (comp add-created-at-timestamp add-updated-at-timestamp)
   :update add-updated-at-timestamp)
 
+;; like `timestamped?`, but for models that only have an `:created_at` column
+(models/add-property! :created-at-timestamped?
+  :insert add-created-at-timestamp)
+
 ;; like `timestamped?`, but for models that only have an `:updated_at` column
 (models/add-property! :updated-at-timestamped?
   :insert add-updated-at-timestamp
diff --git a/src/metabase/models/permissions_revision.clj b/src/metabase/models/permissions_revision.clj
index 25d563615be..3ec6a0e25e6 100644
--- a/src/metabase/models/permissions_revision.clj
+++ b/src/metabase/models/permissions_revision.clj
@@ -6,15 +6,12 @@
 
 (models/defmodel PermissionsRevision :permissions_revision)
 
-(defn- pre-insert [revision]
-  (assoc revision :created_at :%now))
-
 (u/strict-extend (class PermissionsRevision)
   models/IModel
   (merge models/IModelDefaults
          {:types      (constantly {:before :json
                                    :after  :json})
-          :pre-insert pre-insert
+          :properties (constantly {:created-at-timestamped? true})
           :pre-update (fn [& _] (throw (Exception. (tru "You cannot update a PermissionsRevision!"))))}))
 
 (defn latest-id
diff --git a/src/metabase/models/session.clj b/src/metabase/models/session.clj
index fd27edbec74..a2388cc075e 100644
--- a/src/metabase/models/session.clj
+++ b/src/metabase/models/session.clj
@@ -17,7 +17,7 @@
   (throw (RuntimeException. "You cannot update a Session.")))
 
 (defn- pre-insert [session]
-  (cond-> (assoc session :created_at :%now)
+  (cond-> session
     (some-> mw.misc/*request* request.u/embedded?) (assoc :anti_csrf_token (random-anti-csrf-token))))
 
 (defn- post-insert [{anti-csrf-token :anti_csrf_token, :as session}]
@@ -30,4 +30,5 @@
    models/IModelDefaults
    {:pre-insert  pre-insert
     :post-insert post-insert
-    :pre-update  pre-update}))
+    :pre-update  pre-update
+    :properties  (constantly {:created-at-timestamped? true})}))
-- 
GitLab