Merge pull request #41 from 30x/detect-clusterid-change

detect apid_cluster_id change in config file and boot clean
diff --git a/apigee_sync_test.go b/apigee_sync_test.go
index 78fb1de..60652c8 100644
--- a/apigee_sync_test.go
+++ b/apigee_sync_test.go
@@ -198,6 +198,21 @@
 
 		}, 3)
 
+		It("should detect apid_cluster_id change in config yaml", func () {
+			Expect(apidInfo).ToNot(BeNil())
+			Expect(apidInfo.ClusterID).To(Equal("bootstrap"))
+			Expect(apidInfo.InstanceID).ToNot(BeEmpty())
+			previousInstanceId := apidInfo.InstanceID
+
+			config.Set(configApidClusterId, "new value")
+			apidInfo, err := getApidInstanceInfo()
+			Expect(err).NotTo(HaveOccurred())
+			Expect(apidInfo.LastSnapshot).To(BeEmpty())
+			Expect(apidInfo.InstanceID).ToNot(BeEmpty())
+			Expect(apidInfo.InstanceID).ToNot(Equal(previousInstanceId))
+			Expect(apidInfo.ClusterID).To(Equal("new value"))
+		})
+
 		It("should correctly identify non-proper subsets with respect to maps", func() {
 
 			//test b proper subset of a
diff --git a/data.go b/data.go
index 48f1ff7..4761373 100644
--- a/data.go
+++ b/data.go
@@ -35,6 +35,7 @@
 	_, err := db.Exec(`
 	CREATE TABLE IF NOT EXISTS APID (
 	    instance_id text,
+	    apid_cluster_id text,
 	    last_snapshot_info text,
 	    PRIMARY KEY (instance_id)
 	);
@@ -237,6 +238,8 @@
 	info.InstanceName = config.GetString(configName)
 	info.ClusterID = config.GetString(configApidClusterId)
 
+	var savedClusterId string
+
 	// always use default database for this
 	var db apid.DB
 	db, err = dataService.DB()
@@ -244,8 +247,8 @@
 		return
 	}
 
-	err = db.QueryRow("SELECT instance_id, last_snapshot_info FROM APID LIMIT 1").
-		Scan(&info.InstanceID, &info.LastSnapshot)
+	err = db.QueryRow("SELECT instance_id, apid_cluster_id, last_snapshot_info FROM APID LIMIT 1").
+		Scan(&info.InstanceID, &savedClusterId, &info.LastSnapshot)
 	if err != nil {
 		if err != sql.ErrNoRows {
 			log.Errorf("Unable to retrieve apidInstanceInfo: %v", err)
@@ -257,9 +260,18 @@
 			info.InstanceID = generateUUID()
 
 			log.Debugf("Inserting new apid instance id %s", info.InstanceID)
-			db.Exec("INSERT INTO APID (instance_id, last_snapshot_info) VALUES (?,?)",
-				info.InstanceID, "")
+			db.Exec("INSERT INTO APID (instance_id, apid_cluster_id, last_snapshot_info) VALUES (?,?,?)",
+				info.InstanceID, info.ClusterID, "")
 		}
+	} else if savedClusterId != info.ClusterID {
+		log.Debug("Detected apid cluster id change in config.  Apid will start clean")
+		err = nil
+		newInstanceID = true
+		info.InstanceID = generateUUID()
+
+		db.Exec("REPLACE INTO APID (instance_id, apid_cluster_id, last_snapshot_info) VALUES (?,?,?)",
+			info.InstanceID, info.ClusterID, "")
+		info.LastSnapshot = ""
 	}
 	return
 }
@@ -273,10 +285,10 @@
 	}
 
 	rows, err := db.Exec(`
-		INSERT OR REPLACE
-		INTO APID (instance_id, last_snapshot_info)
-		VALUES (?, ?)`,
-		apidInfo.InstanceID, apidInfo.LastSnapshot)
+		REPLACE
+		INTO APID (instance_id, apid_cluster_id, last_snapshot_info)
+		VALUES (?,?,?)`,
+		apidInfo.InstanceID, apidInfo.ClusterID, apidInfo.LastSnapshot)
 	if err != nil {
 		return err
 	}