 
 
 
+(defn does-table-exist [table-name]
+  (let [table-exists-result      (korma.core/exec-raw
+                                   [(cond
+                                      (= *database-type* "postgres" )
+                                      "select * from pg_tables where schemaname='public' and tablename=?"
+                                      (= *database-type* "oracle" )
+                                      "select count(*) from all_objects   where object_type in ('TABLE','VIEW')   and object_name =?"
+                                      ) [(cond (= *database-type* "postgres" ) table-name (= *database-type* "oracle" ) (.toUpperCase table-name))]]
+                                   :results)
+
+        table-exists?   (cond
+                          (= *database-type* "postgres" )
+                          (> (count table-exists-result) 0)
+
+                          (= *database-type* "oracle" )
+                          (> (first (vals (first table-exists-result)  )) 0))
+        ]
+
+    (println (str table-name " table-exists-result: " (count table-exists-result)))
+    (println (str table-name " table exists: " table-exists?))
+    table-exists?))
 
 
 



(defn make-todo-table []
  (let [coils-admin-tables      (korma.core/exec-raw
                                 [" select * from pg_tables where schemaname='public' and tablename='coils_todo_items'" []]
                                 :results)

        coils-admin-table-exists   (pos? (count coils-admin-tables))

        sql-to-create-admin-table "
        CREATE TABLE coils_todo_items
        (
        id                   serial NOT NULL,
        item                 character varying,
        item_status          character varying,

        CONSTRAINT todo_items_pkey PRIMARY KEY (id)
        );
        "
        ]

    (println "Coils todo table exists: " coils-admin-table-exists)
    (if (not coils-admin-table-exists )
      (korma.core/exec-raw   [sql-to-create-admin-table []]   [])
      nil)))





(defn make-users-table []
  (let [sql-to-create-users-table
        (cond
          (= *database-type* "postgres" )
          "
          CREATE TABLE coils_users
          (
          id character varying NOT NULL,
          user_name character varying,
          CONSTRAINT users_pkey PRIMARY KEY (id)
          );
          "

          (= *database-type* "oracle" )
          "
          CREATE TABLE coils_users
          (ID                 NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
          USER_NAME           VARCHAR2(200 BYTE),
          CONSTRAINT  COILS_USERS_PK PRIMARY KEY (ID)
          );
          ")
        ]

    (if (not (does-table-exist  "coils_users"))
      (korma.core/exec-raw   [sql-to-create-users-table []]   [])
      nil)))


