    (reporting-partitioned-tables 500))
   ([batch-size]
    (jdbc/do-commands
-    "ALTER TABLE resource_events RENAME TO resource_events_premigrate"
-
-    "CREATE TABLE resource_events (
-       event_hash bytea NOT NULL PRIMARY KEY,
-       report_id bigint NOT NULL,
-       certname_id bigint NOT NULL,
-       status text NOT NULL,
-       \"timestamp\" timestamp with time zone NOT NULL,
-       resource_type text NOT NULL,
-       resource_title text NOT NULL,
-       property text,
-       new_value text,
-       old_value text,
-       message text,
-       file text DEFAULT NULL::character varying,
-       line integer,
-       name text,
-       containment_path text[],
-       containing_class text,
-       corrective_change boolean)"
-
-    "CREATE OR REPLACE FUNCTION resource_events_insert_trigger()
-    RETURNS TRIGGER AS $$
-    DECLARE
-      tablename varchar;
-    BEGIN
-      SELECT FORMAT('resource_events_%sZ',
-                    TO_CHAR(NEW.\"timestamp\" AT TIME ZONE 'UTC', 'YYYYMMDD')) INTO tablename;
-
-      EXECUTE 'INSERT INTO ' || tablename || ' SELECT ($1).*'
-      USING NEW;
-
-      RETURN NULL;
-    END;
-    $$
-    LANGUAGE plpgsql;"
-
-    "CREATE TRIGGER insert_resource_events_trigger
-    BEFORE INSERT ON resource_events
-    FOR EACH ROW EXECUTE PROCEDURE resource_events_insert_trigger();")
+     "ALTER TABLE resource_events RENAME TO resource_events_premigrate"
+
+     "CREATE TABLE resource_events (
+        event_hash bytea NOT NULL PRIMARY KEY,
+        report_id bigint NOT NULL,
+        certname_id bigint NOT NULL,
+        status text NOT NULL,
+        \"timestamp\" timestamp with time zone NOT NULL,
+        resource_type text NOT NULL,
+        resource_title text NOT NULL,
+        property text,
+        new_value text,
+        old_value text,
+        message text,
+        file text DEFAULT NULL::character varying,
+        line integer,
+        name text,
+        containment_path text[],
+        containing_class text,
+        corrective_change boolean)"
+
+     "CREATE OR REPLACE FUNCTION resource_events_insert_trigger()
+     RETURNS TRIGGER AS $$
+     DECLARE
+       tablename varchar;
+     BEGIN
+       SELECT FORMAT('resource_events_%sZ',
+                     TO_CHAR(NEW.\"timestamp\" AT TIME ZONE 'UTC', 'YYYYMMDD')) INTO tablename;
+
+       EXECUTE 'INSERT INTO ' || tablename || ' SELECT ($1).*'
+       USING NEW;
+
+       RETURN NULL;
+     END;
+     $$
+     LANGUAGE plpgsql;"
+
+     "CREATE TRIGGER insert_resource_events_trigger
+     BEFORE INSERT ON resource_events
+     FOR EACH ROW EXECUTE PROCEDURE resource_events_insert_trigger();"
+
+     "CREATE OR REPLACE FUNCTION find_resource_events_unique_dates()
+     RETURNS TABLE (rowdate DATE)
+     AS $$
+     DECLARE
+     BEGIN
+       EXECUTE 'SET local timezone to ''UTC''';
+       RETURN QUERY SELECT DISTINCT \"timestamp\"::DATE AS rowdate FROM resource_events_premigrate;
+     END;
+     $$ language plpgsql;")
 
   ;; create range of partitioned tables
 


   ;; pre-create partitions
   (log/info (trs "Creating partitions based on unique days in resource_events"))
   (jdbc/call-with-query-rows
    ["select distinct date_trunc('day', \"timestamp\") as rowdate
      from resource_events_premigrate"]
    (fn [rows]
      (doseq [row rows]
        (partitioning/create-resource-events-partition (-> (:rowdate row)
                                                           (.toInstant)
                                                           (ZonedDateTime/ofInstant (ZoneId/of "UTC"))
                                                           (.truncatedTo (ChronoUnit/DAYS)))))))

  (let [event-count (-> "select count(*) from resource_events_premigrate"
                        jdbc/query-to-vec first :count)


  (jdbc/do-commands
   ;; Indexes are created on the individual partitions, not on the base table
   "DROP TABLE resource_events_premigrate"
   "DROP FUNCTION find_resource_events_unique_dates()")))

(def migrations
  "The available migrations, as a map from migration version to migration function."
