From b671b5bd307563203208c6442e46d2af62ca388f Mon Sep 17 00:00:00 2001
From: Danny Auble <da@llnl.gov>
Date: Fri, 28 Mar 2008 20:49:43 +0000
Subject: [PATCH] ok removes work for everything.  No updates for removes of
 associations yet though

---
 .../mysql/accounting_storage_mysql.c          | 67 ++++---------------
 src/sacctmgr/account_functions.c              | 44 +++++++-----
 src/sacctmgr/cluster_functions.c              |  8 +--
 src/sacctmgr/user_functions.c                 | 41 ++++++++----
 4 files changed, 73 insertions(+), 87 deletions(-)

diff --git a/src/plugins/accounting_storage/mysql/accounting_storage_mysql.c b/src/plugins/accounting_storage/mysql/accounting_storage_mysql.c
index 7d86f9a1ace..95630f361a1 100644
--- a/src/plugins/accounting_storage/mysql/accounting_storage_mysql.c
+++ b/src/plugins/accounting_storage/mysql/accounting_storage_mysql.c
@@ -263,7 +263,8 @@ static int _remove_common(mysql_conn_t *mysql_conn,
 			list_create(destroy_acct_update_object);
 		
 		return SLURM_ERROR;
-	}
+	} else if(mysql_conn->rollback) 
+		mysql_conn->trans_started = 1;
 
 	if(table == assoc_table || !assoc_char)
 		return SLURM_SUCCESS;
@@ -304,56 +305,9 @@ static int _remove_common(mysql_conn_t *mysql_conn,
 	}
 	mysql_free_result(result);
  
-	if(rc == SLURM_SUCCESS && mysql_conn->rollback) 
-		mysql_conn->trans_started = 1;
-	
 	return rc;
 }
 
-/* static int _remove_assoc_common(mysql_conn_t *mysql_conn, */
-/* 				uint16_t type, */
-/* 				time_t now, */
-/* 				char *user_name, */
-/* 				char *assoc_char)  */
-/* { */
-/* 	MYSQL_RES *result = NULL; */
-/* 	MYSQL_ROW row; */
-/* 	int lft = 0, rgt = 0; */
-/* 	char *query = xstrdup_printf("SELECT lft, rgt FROM %s WHERE %s;", */
-/* 				     assoc_table, assoc_char); */
-/* 	int rc = SLURM_SUCCESS; */
-
-/* 	debug3("query\n%s", query); */
-/* 	if(!(result = mysql_db_query_ret(mysql_conn->acct_mysql_db, query))) { */
-/* 		xfree(query); */
-/* 		return rc; */
-/* 	} */
-/* 	xfree(query); */
-
-/* 	while((row = mysql_fetch_row(result))) { */
-/* 		query = xstrdup_printf( */
-/* 			"update %s set mod_time=%d, deleted=1 " */
-/* 			"where deleted=0 && lft>=%s && rgt<=%s;", */
-/* 			assoc_table, now, */
-/* 			row[0], row[1]); */
-/* 		debug3("query\n%s", query); */
-/* 		rc = mysql_db_query(mysql_conn->acct_mysql_db, query); */
-/* 		xfree(query); */
-/* 		if(rc != SLURM_SUCCESS) { */
-/* 			if(mysql_conn->rollback) { */
-/* 				mysql_db_rollback(mysql_conn->acct_mysql_db); */
-/* 			} */
-/* 			list_destroy(mysql_conn->update_list); */
-/* 			mysql_conn->update_list = */
-/* 				list_create(destroy_acct_update_object); */
-/* 			break; */
-/* 		} */
-/* 	} */
-/* 	mysql_free_result(result); */
-
-/* 	return rc; */
-
-/* } */
 static int _get_db_index(MYSQL *acct_mysql_db, 
 			 time_t submit, uint32_t jobid, uint32_t associd)
 {
@@ -2773,33 +2727,40 @@ extern List acct_storage_p_remove_associations(mysql_conn_t *mysql_conn,
 		xstrfmtcat(extra, " && parent_acct='%s'", assoc_q->parent_acct);
 	}
 
-	query = xstrdup_printf("select id from %s %s;", assoc_table, extra);
+	query = xstrdup_printf("select id, lft, rgt from %s %s;",
+			       assoc_table, extra);
 	xfree(extra);
 	if(!(result = mysql_db_query_ret(mysql_conn->acct_mysql_db, query))) {
 		xfree(query);
 		return NULL;
 	}
-	xfree(query);
 
 	rc = 0;
 	ret_list = list_create(slurm_destroy_char);
 	while((row = mysql_fetch_row(result))) {
 		char *object = xstrdup(row[0]);
 		list_append(ret_list, object);
+		/* get the lft and rgt associations also */
 		if(!rc) {
-			xstrfmtcat(name_char, "id=%s", object);
+			xstrfmtcat(name_char, 
+				   "(id=%s || (lft>=%s && rgt<=%s))",
+				   object, row[1], row[2]);
 			rc = 1;
 		} else {
-			xstrfmtcat(name_char, " || id=%s", object);
+			xstrfmtcat(name_char,
+				   " || (id=%s || (lft>=%s && rgt<=%s))",
+				   object, row[1], row[2]);
 		}
 	}
 	mysql_free_result(result);
 
 	if(!list_count(ret_list)) {
-		debug3("didn't effect anything");
+		debug3("didn't effect anything\n%s", query);
 		list_destroy(ret_list);
+		xfree(query);
 		return NULL;
 	}
+	xfree(query);
 
 	if(_remove_common(mysql_conn, DBD_REMOVE_ASSOCS, now,
 			  user_name, assoc_table, name_char, NULL)
diff --git a/src/sacctmgr/account_functions.c b/src/sacctmgr/account_functions.c
index 177aa2cf541..f40ac87be0d 100644
--- a/src/sacctmgr/account_functions.c
+++ b/src/sacctmgr/account_functions.c
@@ -44,8 +44,8 @@ static int _set_cond(int *start, int argc, char *argv[],
 		     acct_association_cond_t *assoc_cond)
 {
 	int i;
-	int u_set = 0;
 	int a_set = 0;
+	int u_set = 0;
 	int end = 0;
 
 	for (i=(*start); i<argc; i++) {
@@ -80,19 +80,17 @@ static int _set_cond(int *start, int argc, char *argv[],
 			u_set = 1;
 		} else if (strncasecmp (argv[i], "ShowAssoc", 1) == 0) {
 			acct_cond->with_assocs = 1;
-			u_set = 1;
 		} else {
 			printf(" Unknown condition: %s", argv[i]);
 		}
 	}
 	(*start) = i;
 
-	if(u_set && a_set)
-		return 3;
-	else if(u_set)
-		return 1;
-	else if(a_set)
+	if(a_set) {
 		return 2;
+	} else if(u_set)
+		return 1;
+
 	return 0;
 }
 
@@ -696,7 +694,8 @@ extern int sacctmgr_delete_account(int argc, char *argv[])
 		xmalloc(sizeof(acct_association_cond_t));
 	int i=0;
 	List ret_list = NULL;
-
+	int set = 0;
+	
 	acct_cond->acct_list = list_create(slurm_destroy_char);
 	acct_cond->description_list = list_create(slurm_destroy_char);
 	acct_cond->organization_list = list_create(slurm_destroy_char);
@@ -706,18 +705,31 @@ extern int sacctmgr_delete_account(int argc, char *argv[])
 	assoc_cond->cluster_list = list_create(slurm_destroy_char);
 	assoc_cond->partition_list = list_create(slurm_destroy_char);
 
-	if(!_set_cond(&i, argc, argv, acct_cond, assoc_cond)) {
+	if(!(set = _set_cond(&i, argc, argv, acct_cond, assoc_cond))) {
 		printf(" No conditions given to remove, not executing.\n");
 		destroy_acct_account_cond(acct_cond);
 		destroy_acct_association_cond(assoc_cond);
 		return SLURM_ERROR;
 	}
 
-	if((ret_list = acct_storage_g_remove_accounts(db_conn, my_uid, 
-						      acct_cond))) {
+	if(set == 1) {
+		ret_list = acct_storage_g_remove_accounts(
+			db_conn, my_uid, acct_cond);		
+	} else if(set == 2) {
+		ret_list = acct_storage_g_remove_associations(
+			db_conn, my_uid, assoc_cond);
+	}
+	destroy_acct_account_cond(acct_cond);
+	destroy_acct_association_cond(assoc_cond);
+	
+	if(ret_list && list_count(ret_list)) {
 		char *object = NULL;
 		ListIterator itr = list_iterator_create(ret_list);
-		printf(" Deleting accounts...\n");
+		if(set == 1) {
+			printf(" Deleting accounts...\n");
+		} else if(set == 2) {
+			printf(" Deleting account associations...\n");
+		}
 		while((object = list_next(itr))) {
 			printf("  %s\n", object);
 		}
@@ -727,12 +739,12 @@ extern int sacctmgr_delete_account(int argc, char *argv[])
 			_remove_existing_accounts(ret_list);
 		} else
 			acct_storage_g_commit(db_conn, 0);;
-		list_destroy(ret_list);
 	} else {
-		rc = SLURM_ERROR;
+		printf(" Nothing deleted\n");
 	}
-	destroy_acct_account_cond(acct_cond);
-	destroy_acct_association_cond(assoc_cond);
+
+	if(ret_list)
+		list_destroy(ret_list);
 
 	return rc;
 }
diff --git a/src/sacctmgr/cluster_functions.c b/src/sacctmgr/cluster_functions.c
index 1d1afb85d20..c7d0f13a71b 100644
--- a/src/sacctmgr/cluster_functions.c
+++ b/src/sacctmgr/cluster_functions.c
@@ -430,6 +430,9 @@ extern int sacctmgr_delete_cluster(int argc, char *argv[])
 	}
 	ret_list = acct_storage_g_remove_clusters(
 		db_conn, my_uid, cluster_cond);
+
+	destroy_acct_cluster_cond(cluster_cond);
+
 	if(ret_list && list_count(ret_list)) {
 		char *object = NULL;
 		ListIterator itr = list_iterator_create(ret_list);
@@ -445,14 +448,11 @@ extern int sacctmgr_delete_cluster(int argc, char *argv[])
 			acct_storage_g_commit(db_conn, 0);;
 
 	} else {
-		printf(" error: problem deleting clusters\n");
-		rc = SLURM_ERROR;
+		printf(" Nothing deleted\n");
 	}
 
 	if(ret_list)
 		list_destroy(ret_list);
-	
-	destroy_acct_cluster_cond(cluster_cond);
 
 	return rc;
 }
diff --git a/src/sacctmgr/user_functions.c b/src/sacctmgr/user_functions.c
index af1ae01807d..11a3d083c28 100644
--- a/src/sacctmgr/user_functions.c
+++ b/src/sacctmgr/user_functions.c
@@ -89,12 +89,11 @@ static int _set_cond(int *start, int argc, char *argv[],
 	}	
 	(*start) = i;
 
-	if(u_set && a_set)
-		return 3;
-	else if(u_set)
-		return 1;
-	else if(a_set)
+	if(a_set) {
 		return 2;
+	} else if(u_set)
+		return 1;
+
 	return 0;
 }
 
@@ -772,6 +771,7 @@ extern int sacctmgr_delete_user(int argc, char *argv[])
 		xmalloc(sizeof(acct_association_cond_t));
 	int i=0;
 	List ret_list = NULL;
+	int set = 0;
 
 	user_cond->user_list = list_create(slurm_destroy_char);
 	user_cond->def_acct_list = list_create(slurm_destroy_char);
@@ -781,18 +781,32 @@ extern int sacctmgr_delete_user(int argc, char *argv[])
 	assoc_cond->cluster_list = list_create(slurm_destroy_char);
 	assoc_cond->partition_list = list_create(slurm_destroy_char);
 
-	if(!_set_cond(&i, argc, argv, user_cond, assoc_cond)) {
+	if(!(set = _set_cond(&i, argc, argv, user_cond, assoc_cond))) {
 		printf(" No conditions given to remove, not executing.\n");
 		destroy_acct_user_cond(user_cond);
 		destroy_acct_association_cond(assoc_cond);
 		return SLURM_ERROR;
 	}
 
-	if((ret_list = acct_storage_g_remove_users(db_conn, my_uid,
-						   user_cond))) {
+	if(set == 1) {
+		ret_list = acct_storage_g_remove_users(
+			db_conn, my_uid, user_cond);		
+	} else if(set == 2) {
+		ret_list = acct_storage_g_remove_associations(
+			db_conn, my_uid, assoc_cond);
+	}
+
+	destroy_acct_user_cond(user_cond);
+	destroy_acct_association_cond(assoc_cond);
+
+	if(ret_list && list_count(ret_list)) {
 		char *object = NULL;
 		ListIterator itr = list_iterator_create(ret_list);
-		printf(" Deleting users...\n");
+		if(set == 1) {
+			printf(" Deleting users...\n");
+		} else if(set == 2) {
+			printf(" Deleting user associations...\n");
+		}
 		while((object = list_next(itr))) {
 			printf("  %s\n", object);
 		}
@@ -801,14 +815,13 @@ extern int sacctmgr_delete_user(int argc, char *argv[])
 			acct_storage_g_commit(db_conn, 1);
 			_remove_existing_users(ret_list);
 		} else
-			acct_storage_g_commit(db_conn, 0);
-		list_destroy(ret_list);
+			acct_storage_g_commit(db_conn, 0);;
 	} else {
-		rc = SLURM_ERROR;
+		printf(" Nothing deleted\n");
 	}
 
-	destroy_acct_user_cond(user_cond);
-	destroy_acct_association_cond(assoc_cond);
+	if(ret_list)
+		list_destroy(ret_list);
 
 	return rc;
 }
-- 
GitLab