src/hg/lib/jksql.c 1.130

1.130 2009/04/29 22:27:06 markd
fixed bug where wrong profile was used for hgcentral
Index: src/hg/lib/jksql.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/lib/jksql.c,v
retrieving revision 1.129
retrieving revision 1.130
diff -b -B -U 4 -r1.129 -r1.130
--- src/hg/lib/jksql.c	13 Apr 2009 22:34:11 -0000	1.129
+++ src/hg/lib/jksql.c	29 Apr 2009 22:27:06 -0000	1.130
@@ -96,16 +96,20 @@
 return sp;
 }
 
 static void sqlProfileAssocDb(struct sqlProfile *sp, char *db)
-/* associate a db with a profile */
+/* associate a db with a profile.  If it is already associated with this
+ * profile, don't do anything.*/
 {
 struct sqlProfile *sp2 = hashFindVal(dbToProfile, db);
-if (sp2 != NULL)
+if ((sp2 != NULL) && (sp2 != sp))
     errAbort("databases %s already associated with profile %s, trying to associated it with %s",
              db, sp2->name, sp->name);
-hashAdd(dbToProfile, db, sp);
-slSafeAddHead(&sp->dbs, slNameNew(db));
+if (sp2 == NULL)
+    {
+    hashAdd(dbToProfile, db, sp);
+    slSafeAddHead(&sp->dbs, slNameNew(db));
+    }
 }
 
 static void sqlProfileCreate(char *profileName, char *host, char *user,
                              char *password)
@@ -118,15 +122,15 @@
 }
 
 static void sqlProfileAddProfIf(char *profileName)
 /* check if a config prefix is a profile, and if so, add a
- * sqlProfile object for it */
+ * sqlProfile object for it if doesn't already exist. */
 {
 char *host = cfgOption2(profileName, "host");
 char *user = cfgOption2(profileName, "user");
 char *password = cfgOption2(profileName, "password");
 
-if ((host != NULL) && (user != NULL) && (password != NULL))
+if ((host != NULL) && (user != NULL) && (password != NULL) && (hashLookup(profiles, profileName) == NULL))
     {
     /* for the default profile, allow environment variable override */
     if (sameString(profileName, defaultProfileName))
         {
@@ -153,19 +157,24 @@
         }
     }
 }
 
-static void sqlProfileAddDb(char *db, char *profileName)
-/* add a mapping of db to profile */
+void sqlProfileAddDb(char *profileName, char *db)
+/* add a mapping of db to profile.  If database is already associated with
+ * this profile, it is ignored.  If it is associated with a different profile,
+ * it is an error. */
 {
 struct sqlProfile *sp = hashFindVal(profiles, profileName);
 if (sp == NULL)
     errAbort("can't find profile %s for database %s in hg.conf", profileName, db);
 sqlProfileAssocDb(sp, db);
 }
 
 static void sqlProfileAddDbs(struct slName *cnames)
-/* add mappings of db to profile from ${db}.${profile} entries */
+/* add mappings of db to profile from ${db}.${profile} entries.
+ * would have liked to have automatically added ${profile}.db
+ * entries, but backupcentral, etc, would map multiple profiles
+ * to a databases, so this is done manually in hdb.c. */
 {
 struct slName *cname;
 for (cname = cnames; cname != NULL; cname = cname->next)
     {
@@ -173,9 +182,9 @@
     if ((dot1 != NULL) && sameString(dot1, ".profile"))
         {
         char *profileName = cfgVal(cname->name);
         *dot1 = '\0';
-        sqlProfileAddDb(cname->name,  profileName);
+        sqlProfileAddDb(profileName, cname->name);
         *dot1 = '.';
         }
     }
 }