Merge pull request #1 from 30x/newschema

Newschema
diff --git a/.gitignore b/.gitignore
index 54e1ced..f47e206 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,6 @@
 profile.out
 cover.html
 coverage.txt
+.idea
+*.lock
+*.iml
diff --git a/api.go b/api.go
index 1aae5b6..e31d841 100644
--- a/api.go
+++ b/api.go
@@ -6,6 +6,7 @@
 	"fmt"
 	"net/http"
 	"net/url"
+	"github.com/30x/apid"
 )
 
 type sucResponseDetail struct {
@@ -51,7 +52,7 @@
 	}
 
 	f := r.Form
-	elems := []string{"action", "key", "uriPath", "organization", "environment"}
+	elems := []string{"action", "key", "uriPath", "scopeuuid"}
 	for _, elem := range elems {
 		if f.Get(elem) == "" {
 			w.WriteHeader(http.StatusBadRequest)
@@ -75,30 +76,63 @@
 // returns []byte to be written to client
 func verifyAPIKey(f url.Values) ([]byte, error) {
 
-	db := getDB()
 
 	key := f.Get("key")
-	org := f.Get("organization")
+	scopeuuid := f.Get("scopeuuid")
 	path := f.Get("uriPath")
-	env := f.Get("environment")
 	action := f.Get("action")
 
-	if key == "" || org == "" || path == "" || env == "" || action != "verify" {
+	if key == "" || scopeuuid == "" || path == "" || action != "verify" {
 		log.Error("Input params Invalid/Incomplete")
 		reason := "Input Params Incomplete or Invalid"
 		errorCode := "INCORRECT_USER_INPUT"
 		return errorResponse(reason, errorCode)
 	}
 
-	sSql := "SELECT ap.api_resources, ap.environments, c.issued_at, c.status, a.callback_url, d.username, d.id FROM APP_CREDENTIAL AS c INNER JOIN APP AS a ON c.app_id = a.id INNER JOIN DEVELOPER AS d ON a.developer_id = d.id INNER JOIN APP_CREDENTIAL_APIPRODUCT_MAPPER as mp ON mp.appcred_id = c.id INNER JOIN API_PRODUCT as ap ON ap.id = mp.apiprdt_id WHERE (UPPER(d.status) = 'ACTIVE' AND mp.apiprdt_id = ap.id AND mp.app_id = a.id AND mp.appcred_id = c.id AND UPPER(mp.status) = 'APPROVED' AND UPPER(a.status) = 'APPROVED' AND c.id = '" + key + "' AND c._apid_scope = '" + org + "');"
+	var env, tenantId string
+	{
+		db, err := apid.Data().DB();
+		switch {
+		case err != nil:
+			reason := err.Error()
+			errorCode := "SEARCH_INTERNAL_ERROR"
+			return errorResponse(reason, errorCode)
+		}
+
+		error := db.QueryRow("SELECT env, scope FROM DATA_SCOPE WHERE id = ?;", scopeuuid).Scan(&env, &tenantId)
+
+		switch {
+		case error == sql.ErrNoRows:
+			reason := "ENV Validation Failed"
+			errorCode := "ENV_VALIDATION_FAILED"
+			return errorResponse(reason, errorCode)
+		case error != nil:
+			reason := error.Error()
+			errorCode := "SEARCH_INTERNAL_ERROR"
+			return errorResponse(reason, errorCode)
+		}
+	}
+
+	db := getDB()
+
+	log.Debug("Found tenant_id='", tenantId, "' with env='", env, "' for scopeuuid='", scopeuuid,"'")
+
+	sSql := "SELECT ap.api_resources, ap.environments, c.issued_at, c.status, a.callback_url, d.username, d.id " +
+		"FROM APP_CREDENTIAL AS c INNER JOIN APP AS a ON c.app_id = a.id " +
+		"INNER JOIN DEVELOPER AS d ON a.developer_id = d.id " +
+		"INNER JOIN APP_CREDENTIAL_APIPRODUCT_MAPPER as mp ON mp.appcred_id = c.id " +
+		"INNER JOIN API_PRODUCT as ap ON ap.id = mp.apiprdt_id " +
+		"WHERE (UPPER(d.status) = 'ACTIVE' AND mp.apiprdt_id = ap.id AND mp.app_id = a.id " +
+		"AND mp.appcred_id = c.id AND UPPER(mp.status) = 'APPROVED' AND UPPER(a.status) = 'APPROVED' " +
+		"AND c.id = $1 AND c.tenant_id = $2);"
 
 	var status, redirectionURIs, developerAppName, developerId, resName, resEnv string
 	var issuedAt int64
-	err := db.QueryRow(sSql).Scan(&resName, &resEnv, &issuedAt, &status,
+	err := db.QueryRow(sSql, key, tenantId).Scan(&resName, &resEnv, &issuedAt, &status,
 		&redirectionURIs, &developerAppName, &developerId)
 	switch {
 	case err == sql.ErrNoRows:
-		reason := "API Key verify failed for (" + key + ", " + org + ", " + path + ", " + env + ")"
+		reason := "API Key verify failed for (" + key + ", " + scopeuuid + ", " + path + ")"
 		errorCode := "REQ_ENTRY_NOT_FOUND"
 		return errorResponse(reason, errorCode)
 
diff --git a/api_test.go b/api_test.go
index 50e6107..369d540 100644
--- a/api_test.go
+++ b/api_test.go
@@ -19,8 +19,7 @@
 			v := url.Values{
 				"key": []string{"credential_x"},
 				"uriPath": []string{"/test"},
-				"environment": []string{"Env_0"},
-				"organization": []string{"Org_0"},
+				"scopeuuid": []string{"ABCDE"},
 				"action": []string{"verify"},
 			}
 			rsp, err := verifyAPIKey(v)
@@ -62,8 +61,7 @@
 				v := url.Values{
 					"key": []string{"app_credential_"+resulti},
 					"uriPath": []string{"/test"},
-					"environment": []string{"Env_0"},
-					"organization": []string{"Org_0"},
+					"scopeuuid": []string{"ABCDE"},
 					"action": []string{"verify"},
 				}
 				rsp, err := verifyAPIKey(v)
@@ -85,9 +83,8 @@
 			uri.Path = apiPath
 
 			v := url.Values{}
-			v.Add("organization", "Org_0")
 			v.Add("key", "credential_x")
-			v.Add("environment", "Env_0")
+			v.Add("scopeuuid", "ABCDE")
 			v.Add("uriPath", "/test")
 			v.Add("action", "verify")
 
@@ -113,9 +110,8 @@
 			uri.Path = apiPath
 
 			v := url.Values{}
-			v.Add("organization", "Org_0")
 			v.Add("key", "app_credential_1")
-			v.Add("environment", "Env_0")
+			v.Add("scopeuuid", "ABCDE")
 			v.Add("uriPath", "/test")
 			v.Add("action", "verify")
 
@@ -131,6 +127,7 @@
 			body, err := ioutil.ReadAll(res.Body)
 			Expect(err).ShouldNot(HaveOccurred())
 			json.Unmarshal(body, &respj)
+			log.Info("Result test",respj)
 			Expect(respj.Type).Should(Equal("APIKeyContext"))
 			Expect(respj.RspInfo.Key).Should(Equal("app_credential_1"))
 		})
diff --git a/apidVerifyAPIKey-api.yaml b/apidVerifyAPIKey-api.yaml
index 04f0335..f0a1841 100644
--- a/apidVerifyAPIKey-api.yaml
+++ b/apidVerifyAPIKey-api.yaml
@@ -88,6 +88,7 @@
       - action
       - key
       - uriPath
+      - scopeuuid
     properties:
       action:
         enum:
@@ -96,6 +97,8 @@
         type: string
       uriPath:
         type: string
+      scopeuuid:
+        type: string
 
   VerifyAPIKeyResponse:
     type: object
diff --git a/init.go b/init.go
index ad04f1c..a475420 100644
--- a/init.go
+++ b/init.go
@@ -59,7 +59,7 @@
     description text,
     api_resources text[],
     approval_type text,
-    _apid_scope text,
+    _change_selector text,
     proxies text[],
     environments text[],
     quota text,
@@ -81,7 +81,7 @@
     status text,
     encrypted_password text,
     salt text,
-    _apid_scope text,
+    _change_selector text,
     created_at int64,
     created_by text,
     updated_at int64,
@@ -98,7 +98,7 @@
     created_by text,
     updated_at int64,
     updated_by text,
-    _apid_scope text,
+    _change_selector text,
     PRIMARY KEY (tenant_id, id)
 );
 CREATE TABLE IF NOT EXISTS company_developer (
@@ -110,7 +110,7 @@
     created_by text,
     updated_at int64,
     updated_by text,
-    _apid_scope text,
+    _change_selector text,
     PRIMARY KEY (tenant_id, company_id,developer_id)
 );
 CREATE TABLE IF NOT EXISTS app (
@@ -129,7 +129,7 @@
     created_by text,
     updated_at int64,
     updated_by text,
-    _apid_scope text,
+    _change_selector text,
     PRIMARY KEY (tenant_id, id)
 );
 CREATE TABLE IF NOT EXISTS app_credential (
@@ -142,7 +142,7 @@
     issued_at int64,
     expires_at int64,
     app_status text,
-    _apid_scope text,
+    _change_selector text,
     PRIMARY KEY (tenant_id, id)
 );
 CREATE TABLE IF NOT EXISTS app_credential_apiproduct_mapper (
@@ -150,7 +150,7 @@
     appcred_id text,
     app_id text,
     apiprdt_id text,
-    _apid_scope text,
+    _change_selector text,
     status text,
     PRIMARY KEY (appcred_id, app_id, apiprdt_id,tenant_id)
 );
@@ -159,3 +159,41 @@
 		log.Panic("Unable to initialize DB", err)
 	}
 }
+
+
+func createApidClusterTables(db apid.DB) {
+	_, err := db.Exec(`
+CREATE TABLE apid_cluster (
+    id text,
+    instance_id text,
+    name text,
+    description text,
+    umbrella_org_app_name text,
+    created int64,
+    created_by text,
+    updated int64,
+    updated_by text,
+    _change_selector text,
+    snapshotInfo text,
+    lastSequence text,
+    PRIMARY KEY (id)
+);
+CREATE TABLE data_scope (
+    id text,
+    apid_cluster_id text,
+    scope text,
+    org text,
+    env text,
+    created int64,
+    created_by text,
+    updated int64,
+    updated_by text,
+    _change_selector text,
+    PRIMARY KEY (id)
+);
+`)
+	if err != nil {
+		log.Panic("Unable to initialize DB", err)
+	}
+}
+
diff --git a/kmsDataTest.sql b/kmsDataTest.sql
new file mode 100644
index 0000000..e73accd
--- /dev/null
+++ b/kmsDataTest.sql
@@ -0,0 +1,60 @@
+
+INSERT INTO "kms"."customer" (id,name,display_name,description,created_at,created_by,updated_at,updated_by) VALUES
+('f8c9ffd6-a234-4723-bd2a-68379df33ff0' /*not nullable*/,'s' /*not nullable*/,'s','s','2016-12-16 16:38:16.593',
+'s' /*not nullable*/,'2016-12-16 16:38:16.593','s' /*not nullable*/);
+
+
+INSERT INTO "kms"."organization" (id,name,tenant_id,customer_id,description,created_at,created_by,updated_at,updated_by) VALUES
+('f8c9ffd6-a234-4723-bd2a-68379df33ff1' /*not nullable*/,'5cfc6415' /*not nullable*/,'5cfc6415' /*not nullable*/,'
+f8c9ffd6-a234-4723-bd2a-68379df33ff0' /*not nullable*/,'5cfc6415',
+'2016-12-16 16:36:49.407','s','2016-12-16 16:36:49.407','s');
+
+INSERT INTO "kms"."developer"
+(id,tenant_id,
+username,first_name,last_name,password,email,
+status,encrypted_password,salt,
+created_at,created_by,updated_at,updated_by,_change_selector) VALUES
+('f8c9ffd6-a234-4723-bd2a-68379df33fff','5cfc6415','Dev1','Alex' /*not nullable*/,'Khimich' /*not nullable*/,
+'Passwd','alexkhimich+edgex@google.com' /*not nullable*/,
+'ACTIVE','s','s',now(),'sql',now(),'sql','5cfc6415');
+
+INSERT INTO "kms"."api_product"
+(id,tenant_id,name,display_name,description,
+api_resources,approval_type,scopes,proxies,environments,
+quota,quota_time_unit,quota_interval,
+created_at,created_by,updated_at,updated_by,_change_selector) VALUES
+('f8c9ffd6-a234-4723-bd2a-68379df33ff2' /*not nullable*/,'5cfc6415' /*not nullable*/,'Product1' /*not nullable*/,'Product1','s',
+'{/**}','AUTO','{""}','{iloveapis}','{prod,test}',
+'s','s',0,
+'2016-12-16 16:39:11.596','s' ,'2016-12-16 16:39:11.596','s' /*not nullable*/,'5cfc6415' /*not nullable*/);
+
+INSERT INTO "kms"."app"
+(id,tenant_id,name,display_name,
+access_type,callback_url,status,app_family,
+company_id,developer_id,parent_id,
+type,
+created_at,created_by,updated_at,updated_by,_change_selector) VALUES
+('f8c9ffd6-a234-4723-bd2a-68379df33ff3' /*not nullable*/,'5cfc6415' /*not nullable*/,'App1' /*not nullable*/,'App1',
+'s','s','APPROVED','default',
+NULL,'f8c9ffd6-a234-4723-bd2a-68379df33fff','f8c9ffd6-a234-4723-bd2a-68379df33fff' /*not nullable*/,
+'DEVELOPER',
+'2016-12-16 16:43:56.839','s' ,'2016-12-16 16:43:56.839','s' /*not nullable*/,'5cfc6415' /*not nullable*/);
+
+INSERT INTO "kms"."app_credential"
+(id,tenant_id,consumer_secret,app_id,
+method_type,status,
+issued_at,expires_at,app_status,scopes,
+created_at,created_by,updated_at,updated_by,_change_selector) VALUES
+('E1HbT0ecxK1M7CMTsJ4TvAzWLTrF3zB9', '5cfc6415', 'DER234','f8c9ffd6-a234-4723-bd2a-68379df33ff3',
+NULL,'APPROVED','2016-12-16 16:46:50.590',NULL,NULL,'{}','2016-12-16 16:46:50.590','s' ,'2016-12-16 16:46:50.590','s' ,'5cfc6415' );
+
+
+
+INSERT INTO "kms"."app_credential_apiproduct_mapper"
+(tenant_id,appcred_id,app_id,apiprdt_id,
+status,_change_selector) VALUES
+('5cfc6415','E1HbT0ecxK1M7CMTsJ4TvAzWLTrF3zB9','f8c9ffd6-a234-4723-bd2a-68379df33ff3','f8c9ffd6-a234-4723-bd2a-68379df33ff2'
+,'APPROVED','5cfc6415' );
+
+curl -s -d "key=E1HbT0ecxK1M7CMTsJ4TvAzWLTrF3zB9&scopeuuid=9356ba25-38df-4e72-bb4d-f974ce9683d6&uriPath=/&action=verify"
+"http://localhost:9090/verifiers/apikey" | python -m json.tool
diff --git a/listener.go b/listener.go
index e9398cd..b2e3f34 100644
--- a/listener.go
+++ b/listener.go
@@ -87,14 +87,14 @@
 	var scope, id, appId, consumerSecret, appstatus, status, tenantId string
 	var issuedAt int64
 
-	prep, err := txn.Prepare("INSERT INTO APP_CREDENTIAL (_apid_scope, id, app_id, consumer_secret, app_status, status, issued_at, tenant_id)VALUES($1,$2,$3,$4,$5,$6,$7,$8);")
+	prep, err := txn.Prepare("INSERT INTO APP_CREDENTIAL (_change_selector, id, app_id, consumer_secret, app_status, status, issued_at, tenant_id)VALUES($1,$2,$3,$4,$5,$6,$7,$8);")
 	if err != nil {
 		log.Error("INSERT Cred Failed: ", err)
 		return false
 	}
 	defer prep.Close()
 	for _, ele := range rows {
-		ele.Get("_apid_scope", &scope)
+		ele.Get("_change_selector", &scope)
 		ele.Get("id", &id)
 		ele.Get("app_id", &appId)
 		ele.Get("consumer_secret", &consumerSecret)
@@ -130,7 +130,7 @@
 	var scope, EntityIdentifier, DeveloperId, CallbackUrl, Status, AppName, AppFamily, tenantId, CreatedBy, LastModifiedBy string
 	var CreatedAt, LastModifiedAt int64
 
-	prep, err := txn.Prepare("INSERT INTO APP (_apid_scope, id, developer_id,callback_url,status, name, app_family, created_at, created_by,updated_at, updated_by,tenant_id) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);")
+	prep, err := txn.Prepare("INSERT INTO APP (_change_selector, id, developer_id,callback_url,status, name, app_family, created_at, created_by,updated_at, updated_by,tenant_id) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);")
 	if err != nil {
 		log.Error("INSERT APP Failed: ", err)
 		return false
@@ -139,7 +139,7 @@
 	defer prep.Close()
 	for _, ele := range rows {
 
-		ele.Get("_apid_scope", &scope)
+		ele.Get("_change_selector", &scope)
 		ele.Get("id", &EntityIdentifier)
 		ele.Get("developer_id", &DeveloperId)
 		ele.Get("callback_url", &CallbackUrl)
@@ -185,7 +185,7 @@
 	var scope, EntityIdentifier, Email, Status, UserName, FirstName, LastName, tenantId, CreatedBy, LastModifiedBy, Username string
 	var CreatedAt, LastModifiedAt int64
 
-	prep, err := txn.Prepare("INSERT INTO DEVELOPER (_apid_scope,email,id,tenant_id,status,username,first_name,last_name,created_at,created_by,updated_at,updated_by) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);")
+	prep, err := txn.Prepare("INSERT INTO DEVELOPER (_change_selector,email,id,tenant_id,status,username,first_name,last_name,created_at,created_by,updated_at,updated_by) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);")
 	if err != nil {
 		log.Error("INSERT DEVELOPER Failed: ", err)
 		return false
@@ -194,7 +194,7 @@
 	defer prep.Close()
 	for _, ele := range rows {
 
-		ele.Get("_apid_scope", &scope)
+		ele.Get("_change_selector", &scope)
 		ele.Get("email", &Email)
 		ele.Get("id", &EntityIdentifier)
 		ele.Get("tenant_id", &tenantId)
@@ -238,7 +238,7 @@
 
 	var scope, apiProduct, res, env, tenantId string
 
-	prep, err := txn.Prepare("INSERT INTO API_PRODUCT (id, api_resources, environments, tenant_id,_apid_scope) VALUES($1,$2,$3,$4,$5)")
+	prep, err := txn.Prepare("INSERT INTO API_PRODUCT (id, api_resources, environments, tenant_id,_change_selector) VALUES($1,$2,$3,$4,$5)")
 	if err != nil {
 		log.Error("INSERT API_PRODUCT Failed: ", err)
 		return false
@@ -247,7 +247,7 @@
 	defer prep.Close()
 	for _, ele := range rows {
 
-		ele.Get("_apid_scope", &scope)
+		ele.Get("_change_selector", &scope)
 		ele.Get("id", &apiProduct)
 		ele.Get("api_resources", &res)
 		ele.Get("environments", &env)
@@ -277,7 +277,7 @@
 
 	var ApiProduct, AppId, EntityIdentifier, tenantId, Scope, Status string
 
-	prep, err := txn.Prepare("INSERT INTO APP_CREDENTIAL_APIPRODUCT_MAPPER(apiprdt_id, app_id, appcred_id, tenant_id, _apid_scope, status) VALUES($1,$2,$3,$4,$5,$6);")
+	prep, err := txn.Prepare("INSERT INTO APP_CREDENTIAL_APIPRODUCT_MAPPER(apiprdt_id, app_id, appcred_id, tenant_id, _change_selector, status) VALUES($1,$2,$3,$4,$5,$6);")
 	if err != nil {
 		log.Error("INSERT APP_CREDENTIAL_APIPRODUCT_MAPPER Failed: ", err)
 		return false
@@ -290,7 +290,7 @@
 		ele.Get("app_id", &AppId)
 		ele.Get("appcred_id", &EntityIdentifier)
 		ele.Get("tenant_id", &tenantId)
-		ele.Get("_apid_scope", &Scope)
+		ele.Get("_change_selector", &Scope)
 		ele.Get("status", &Status)
 
 		/*
@@ -438,14 +438,14 @@
 func deleteObject(object string, ele common.Row, txn *sql.Tx) bool {
 
 	var scope, apiProduct string
-	ssql := "DELETE FROM " + object + " WHERE id = $1 AND _apid_scope = $2"
+	ssql := "DELETE FROM " + object + " WHERE id = $1 AND _change_selector = $2"
 	prep, err := txn.Prepare(ssql)
 	if err != nil {
 		log.Error("DELETE ", object, " Failed: ", err)
 		return false
 	}
 	defer prep.Close()
-	ele.Get("_apid_scope", &scope)
+	ele.Get("_change_selector", &scope)
 	ele.Get("id", &apiProduct)
 
 	_, err = txn.Stmt(prep).Exec(apiProduct, scope)
@@ -465,7 +465,7 @@
 func deleteAPIproductMapper(ele common.Row, txn *sql.Tx) bool {
 	var ApiProduct, AppId, EntityIdentifier, apid_scope string
 
-	prep, err := txn.Prepare("DELETE FROM APP_CREDENTIAL_APIPRODUCT_MAPPER WHERE apiprdt_id=$1 AND app_id=$2 AND appcred_id=$3 AND _apid_scope=$4;")
+	prep, err := txn.Prepare("DELETE FROM APP_CREDENTIAL_APIPRODUCT_MAPPER WHERE apiprdt_id=$1 AND app_id=$2 AND appcred_id=$3 AND _change_selector=$4;")
 	if err != nil {
 		log.Error("DELETE APP_CREDENTIAL_APIPRODUCT_MAPPER Failed: ", err)
 		return false
@@ -476,7 +476,7 @@
 	ele.Get("apiprdt_id", &ApiProduct)
 	ele.Get("app_id", &AppId)
 	ele.Get("appcred_id", &EntityIdentifier)
-	ele.Get("_apid_scope", &apid_scope)
+	ele.Get("_change_selector", &apid_scope)
 
 	_, err = txn.Stmt(prep).Exec(ApiProduct, AppId, EntityIdentifier, apid_scope)
 	if err != nil {
diff --git a/listener_test.go b/listener_test.go
index af8b78a..a8da554 100644
--- a/listener_test.go
+++ b/listener_test.go
@@ -34,7 +34,7 @@
 				Value: "tenant_id_0",
 				Type:  1,
 			},
-			"_apid_scope": {
+			"_change_selector": {
 				Value: "test_org0",
 				Type:  1,
 			},
@@ -54,7 +54,7 @@
 				Value: "tenant_id_0",
 				Type:  1,
 			},
-			"_apid_scope": {
+			"_change_selector": {
 				Value: "test_org0",
 				Type:  1,
 			},
@@ -78,7 +78,7 @@
 				Value: "tenant_id_0",
 				Type:  1,
 			},
-			"_apid_scope": {
+			"_change_selector": {
 				Value: "test_org0",
 				Type:  1,
 			},
@@ -102,7 +102,7 @@
 				Value: "Approved",
 				Type:  1,
 			},
-			"_apid_scope": {
+			"_change_selector": {
 				Value: "test_org0",
 				Type:  1,
 			},
@@ -126,7 +126,7 @@
 				Value: "Approved",
 				Type:  1,
 			},
-			"_apid_scope": {
+			"_change_selector": {
 				Value: "test_org0",
 				Type:  1,
 			},
@@ -200,8 +200,7 @@
 				v := url.Values{
 					"key": []string{"ch_app_credential_0"},
 					"uriPath": []string{"/test"},
-					"environment": []string{"Env_0"},
-					"organization": []string{"test_org0"},
+					"scopeuuid": []string{"XYZ"},
 					"action": []string{"verify"},
 				}
 				rsp, err := verifyAPIKey(v)
diff --git a/pluginData.go b/pluginData.go
index 8c3335d..b261d7c 100644
--- a/pluginData.go
+++ b/pluginData.go
@@ -4,8 +4,8 @@
 
 var pluginData = apid.PluginData{
 	Name: "apidVerifyAPIKey",
-	Version: "0.0.1",
+	Version: "0.0.2",
 	ExtraData: map[string]interface{}{
-		"schemaVersion": "0.0.1",
+		"schemaVersion": "0.0.2",
 	},
 }
diff --git a/validate_env.go b/validate_env.go
index d90e27c..9fede78 100644
--- a/validate_env.go
+++ b/validate_env.go
@@ -6,7 +6,9 @@
  * Ensure the ENV matches.
  */
 func validateEnv(envLocal string, envInPath string) bool {
-
+	if (envInPath == "") {
+		return false;
+	}
 	s := strings.TrimPrefix(envLocal, "{")
 	s = strings.TrimSuffix(s, "}")
 	fs := strings.Split(s, ",")
diff --git a/verifyAPIKey_suite_test.go b/verifyAPIKey_suite_test.go
index 610d941..870a376 100644
--- a/verifyAPIKey_suite_test.go
+++ b/verifyAPIKey_suite_test.go
@@ -37,6 +37,7 @@
 	Expect(err).NotTo(HaveOccurred())
 	setDB(db)
 	createTables(db)
+	createApidClusterTables(db)
 	insertTestData(db)
 	testServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 		if req.URL.Path == apiPath {
@@ -90,7 +91,7 @@
 			Value: "Org_0",
 			Type:  1,
 		}
-		srvItems["_apid_scope"] = scv
+		srvItems["_change_selector"] = scv
 
 		scv = &common.ColumnVal{
 			Value: "tenant_id_xxxx",
@@ -141,7 +142,7 @@
 			Value: "Org_0",
 			Type:  1,
 		}
-		srvItems["_apid_scope"] = scv
+		srvItems["_change_selector"] = scv
 
 		scv = &common.ColumnVal{
 			Value: "tenant_id_xxxx",
@@ -157,7 +158,7 @@
 	var j, k int
 	for i := 0; i < 10; i++ {
 		resulti := strconv.FormatInt(int64(i), 10)
-		for j = k; j < 10+k; j++ {
+		for j = k; j < 10 + k; j++ {
 			var rows []common.Row
 
 			srvItems := common.Row{}
@@ -191,7 +192,7 @@
 				Value: "Org_0",
 				Type:  1,
 			}
-			srvItems["_apid_scope"] = scv
+			srvItems["_change_selector"] = scv
 
 			scv = &common.ColumnVal{
 				Value: "tenant_id_xxxx",
@@ -232,7 +233,7 @@
 			Value: "Org_0",
 			Type:  1,
 		}
-		srvItems["_apid_scope"] = scv
+		srvItems["_change_selector"] = scv
 
 		scv = &common.ColumnVal{
 			Value: "tenant_id_xxxx",
@@ -275,7 +276,7 @@
 			Value: "Org_0",
 			Type:  1,
 		}
-		srvItems["_apid_scope"] = scv
+		srvItems["_change_selector"] = scv
 
 		scv = &common.ColumnVal{
 			Value: "tenant_id_xxxx",
@@ -286,6 +287,30 @@
 		res := insertAPIProductMappers(rows, txn)
 		Expect(res).Should(BeTrue())
 	}
-
+	txn.Exec("INSERT INTO DATA_SCOPE (id, _change_selector, apid_cluster_id, scope, org, env) " +
+		"VALUES" +
+		"($1,$2,$3,$4,$5,$6)",
+		"ABCDE",
+		"some_cluster_id",
+		"some_cluster_id",
+		"tenant_id_xxxx",
+		"test_org0",
+		"Env_0",
+	);
+	txn.Exec("INSERT INTO DATA_SCOPE (id, _change_selector, apid_cluster_id, scope, org, env) " +
+		"VALUES" +
+		"($1,$2,$3,$4,$5,$6)",
+		"XYZ",
+		"test_org0",
+		"somecluster_id",
+		"tenant_id_0",
+		"test_org0",
+		"Env_0",
+	);
+	log.Info("Inserted DATA_SCOPE for test")
 	txn.Commit()
+	var count int64
+	db.QueryRow("select count(*) from data_scope").Scan(&count);
+	log.Info("Found ", count)
+
 }