     (catch java.sql.SQLException e
       (log/with-logs ['pdok.featured.projectors :error :error] (j/print-sql-exception-chain e)))))
 
-(defn- flush-all [db proj-fn cache insert-batch update-batch delete-batch]
+(defn- flush-all [db proj-fn cache insert-batch update-batch delete-batch no-insert]
   "Used for flushing all batches, so entry order is alway new change close"
   (let [cached-collection-attributes (cached cache gs-collection-attributes db)]
+    
+    (process-batch update-batch (partial gs-update-feature db proj-fn))
     (process-batch delete-batch (partial gs-delete-feature db))
-    (flush-batch insert-batch (partial gs-add-feature db proj-fn cached-collection-attributes))
+    
+    (flush-batch insert-batch (partial gs-add-feature db proj-fn cached-collection-attributes no-insert))
     (flush-batch update-batch (partial gs-update-feature db proj-fn))
-    (flush-batch delete-batch (partial gs-delete-feature db))))
+    (flush-batch delete-batch (partial gs-delete-feature db))
+    
+    (dosync (ref-set no-insert #{}))
+    ))
+
+(defn- versions-in-batch [{:keys [dataset collection id]} batch]
+   (map :version 
+        (filter (fn [f] (and 
+                     (= (:dataset f) dataset)
+                     (= (:collection f) collection)
+                     (= (:id f) id))) 
+                batch)))
 
 (deftype GeoserverProjector [db cache insert-batch insert-batch-size
                              update-batch update-batch-size
-                             delete-batch delete-batch-size
+                             delete-batch delete-batch-size no-insert
                              flush-fn proj-fn no-visualization ndims srid]
   proj/Projector
   (proj/init [this] this)


(defn- gs-update-feature [db proj-fn features]
    (let [selector (juxt :dataset :collection)
         per-dataset-collection
           (group-by selector features)]
     (doseq [[[dataset collection] collection-features] per-dataset-collection]
        ;; group per key collection so we can batch every group

       " WHERE NOT EXISTS (SELECT 1 FROM " (pg/quoted schema) "." (pg/quoted table) " WHERE _id = ?)" ))

(defn- gs-add-feature
  ([db proj-fn all-attributes-fn no-insert features]
   (try
     (let [filtered-features (filter #(not (@no-insert (:version %))) features)
           selector (juxt :dataset :collection)
           per-dataset-collection (group-by selector filtered-features)]
        (doseq [[[dataset collection] grouped-features] per-dataset-collection]
         (let [all-attributes (all-attributes-fn dataset collection)
               records (filter (comp not nil?)
