about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-03-07 18:06:20 +0100
committerEmile <hanemile@protonmail.com>2019-03-07 18:06:20 +0100
commit5d7a7c0c86457872ecc99743da5f827c8adf0793 (patch)
treeeb29e0459cd1c36cc1d990915a708a37711bbc18
parent878feaa600deb39ba2466e798e1092423f5f615c (diff)
updated the query, now only creating a new table if it does not exist yet
-rw-r--r--backend/init.go21
1 files changed, 11 insertions, 10 deletions
diff --git a/backend/init.go b/backend/init.go
index 351028b..4db3009 100644
--- a/backend/init.go
+++ b/backend/init.go
@@ -7,7 +7,8 @@ import (
 
 // InitStarsTable initialises the stars table
 func InitStarsTable(db *sql.DB) {
-	query := `CREATE TABLE public.stars
+	log.Println("Preparing the query")
+	var query = `CREATE TABLE IF NOT EXISTS public.stars
 (
   star_id bigserial,
   x numeric,
@@ -16,23 +17,24 @@ func InitStarsTable(db *sql.DB) {
   vy numeric,
   m numeric,
   PRIMARY KEY (star_id)
-)
-  WITH (
-    OIDS = FALSE
-  );
+) WITH (
+  OIDS = FALSE
+);
 
 ALTER TABLE public.stars
   OWNER to postgres;`
+	log.Println("Executing the query")
 	_, err := db.Exec(query)
 	if err != nil {
 		log.Fatalf("[ E ] InitNodesTable query: %v \n\t\t\tquery: %s\n", err, query)
 	}
+	log.Println("DONE")
 }
 
 // InitNodesTable initialises the nodes table
 func InitNodesTable(db *sql.DB) {
 	log.Println("creating the query")
-	query := `CREATE TABLE public.nodes
+	var query = `CREATE TABLE IF NOT EXISTS public.nodes
 (
   node_id bigserial NOT NULL,
   box_width numeric,
@@ -45,10 +47,9 @@ func InitNodesTable(db *sql.DB) {
   center_of_mass numeric[],
   subnodes bigint[],
   PRIMARY KEY (node_id)
-)
-  WITH (
-    OIDS = FALSE
-  );
+) WITH (
+  OIDS = FALSE
+);
 
 ALTER TABLE public.nodes
   OWNER to postgres;`