Skip to content

Commit 20a3cec

Browse files
committed
Anel review
1 parent 51b8208 commit 20a3cec

File tree

4 files changed

+113
-62
lines changed

4 files changed

+113
-62
lines changed

client/mysqldump.c

Lines changed: 86 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,18 @@ static my_bool verbose= 0, opt_no_create_info= 0, opt_no_data= 0, opt_no_data_m
118118
opt_alltspcs=0, opt_notspcs= 0, opt_logging,
119119
opt_drop_trigger= 0 ;
120120
#define OPT_SYSTEM_ALL 1
121-
#define OPT_SYSTEM_USER 2
122-
#define OPT_SYSTEM_PLUGIN 4
123-
#define OPT_SYSTEM_UDF 8
121+
#define OPT_SYSTEM_USERS 2
122+
#define OPT_SYSTEM_PLUGINS 4
123+
#define OPT_SYSTEM_UDFS 8
124124
#define OPT_SYSTEM_SERVERS 16
125125
#define OPT_SYSTEM_STATS 32
126126
#define OPT_SYSTEM_TIMEZONES 64
127127
static const char *opt_system_type_values[]=
128-
{"all", "users", "plugins", "udf", "servers", "stats", "timezones"};
128+
{"all", "users", "plugins", "udfs", "servers", "stats", "timezones"};
129129
static TYPELIB opt_system_types=
130130
{
131-
array_elements(opt_system_type_values), "system dump options", opt_system_type_values, NULL
131+
array_elements(opt_system_type_values), "system dump options",
132+
opt_system_type_values, NULL
132133
};
133134
static ulonglong opt_system= 0ULL;
134135
static my_bool insert_pat_inited= 0, debug_info_flag= 0, debug_check_flag= 0;
@@ -595,9 +596,9 @@ static int init_dumping_tables(char *);
595596
static int init_dumping(char *, int init_func(char*));
596597
static int dump_databases(char **);
597598
static int dump_all_databases();
598-
static int dump_all_users();
599+
static int dump_all_users_roles_and_grants();
599600
static int dump_all_plugins();
600-
static int dump_all_udf();
601+
static int dump_all_udfs();
601602
static int dump_all_servers();
602603
static int dump_all_stats();
603604
static int dump_all_timezones();
@@ -1054,32 +1055,32 @@ static int get_options(int *argc, char ***argv)
10541055
if (opt_system & OPT_SYSTEM_ALL)
10551056
opt_system|= ~0;
10561057

1057-
if (opt_system & OPT_SYSTEM_USER &&
1058+
if (opt_system & OPT_SYSTEM_USERS &&
10581059
(my_hash_insert(&ignore_table,
10591060
(uchar*) my_strdup("mysql.db", MYF(MY_WME))) ||
10601061
my_hash_insert(&ignore_table,
10611062
(uchar*) my_strdup("mysql.global_priv", MYF(MY_WME))) ||
10621063
my_hash_insert(&ignore_table,
1063-
(uchar*) my_strdup("mysql.table_priv", MYF(MY_WME))) ||
1064+
(uchar*) my_strdup("mysql.tables_priv", MYF(MY_WME))) ||
10641065
my_hash_insert(&ignore_table,
1065-
(uchar*) my_strdup("mysql.column_priv", MYF(MY_WME))) ||
1066+
(uchar*) my_strdup("mysql.columns_priv", MYF(MY_WME))) ||
10661067
my_hash_insert(&ignore_table,
10671068
(uchar*) my_strdup("mysql.procs_priv", MYF(MY_WME))) ||
10681069
my_hash_insert(&ignore_table,
10691070
(uchar*) my_strdup("mysql.roles_mapping", MYF(MY_WME))) ||
1070-
/* and MySQL-8.0 role tables as well */
1071+
/* and MySQL-8.0 role tables (role_edges and default_roles) as well */
10711072
my_hash_insert(&ignore_table,
10721073
(uchar*) my_strdup("mysql.role_edges", MYF(MY_WME))) ||
10731074
my_hash_insert(&ignore_table,
10741075
(uchar*) my_strdup("mysql.default_roles", MYF(MY_WME)))))
10751076
return(EX_EOM);
10761077

1077-
if (opt_system & OPT_SYSTEM_PLUGIN &&
1078+
if (opt_system & OPT_SYSTEM_PLUGINS &&
10781079
my_hash_insert(&ignore_table,
1079-
(uchar*) my_strdup("mysql.plugins", MYF(MY_WME))))
1080+
(uchar*) my_strdup("mysql.plugin", MYF(MY_WME))))
10801081
return(EX_EOM);
10811082

1082-
if (opt_system & OPT_SYSTEM_UDF &&
1083+
if (opt_system & OPT_SYSTEM_UDFS &&
10831084
my_hash_insert(&ignore_table,
10841085
(uchar*) my_strdup("mysql.func", MYF(MY_WME))))
10851086
return(EX_EOM);
@@ -4254,16 +4255,18 @@ static char *getTableName(int reset)
42544255

42554256
/*
42564257
dump user/role grants
4258+
ARGS
4259+
user_role: is either a user, or a role
42574260
*/
42584261

4259-
static int dump_grants(const char *ur)
4262+
static int dump_grants(const char *user_role)
42604263
{
42614264
DYNAMIC_STRING sqlbuf;
42624265
MYSQL_ROW row;
42634266
MYSQL_RES *tableres;
42644267

42654268
init_dynamic_string_checked(&sqlbuf, "SHOW GRANTS FOR ", 256, 1024);
4266-
dynstr_append_checked(&sqlbuf, ur);
4269+
dynstr_append_checked(&sqlbuf, user_role);
42674270

42684271
if (mysql_query_with_error_report(mysql, &tableres, sqlbuf.str))
42694272
{
@@ -4281,7 +4284,7 @@ static int dump_grants(const char *ur)
42814284

42824285

42834286
/*
4284-
dump creater user
4287+
dump create user
42854288
*/
42864289

42874290
static int dump_create_user(const char *user)
@@ -4310,12 +4313,11 @@ static int dump_create_user(const char *user)
43104313
}
43114314

43124315

4313-
43144316
/*
43154317
dump all users and roles
43164318
*/
43174319

4318-
static int dump_all_users()
4320+
static int dump_all_users_roles_and_grants()
43194321
{
43204322
MYSQL_ROW row;
43214323
MYSQL_RES *tableres;
@@ -4327,21 +4329,24 @@ static int dump_all_users()
43274329
if (mysql_query_with_error_report(mysql, &tableres,
43284330
"SELECT CONCAT(QUOTE(u.user), '@', QUOTE(u.Host)) AS u "
43294331
"FROM mysql.user u "
4330-
" /*!80001 LEFT JOIN mysql.role_edges e ON u.user=e.from_user AND u.host=e.from_host "
4331-
" WHERE e.from_user IS NULL */"
4332-
" /*M!100005 WHERE is_role='N' */"))
4332+
" /*!80001 LEFT JOIN mysql.role_edges e "
4333+
" ON u.user=e.from_user "
4334+
" AND u.host=e.from_host "
4335+
" WHERE e.from_user IS NULL */"
4336+
" /*M!100005 WHERE is_role='N' */"))
43334337
return 1;
43344338
while ((row= mysql_fetch_row(tableres)))
43354339
{
43364340
if (opt_replace_into)
43374341
/* Protection against removing the current import user */
43384342
/* MySQL-8.0 export capability */
43394343
fprintf(md_result_file,
4340-
"DELIMITER |\n"
4344+
"DELIMITER |\n"
43414345
"/*M!100101 IF current_user()=\"%s\" THEN\n"
4342-
" SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=30001, MESSAGE_TEXT=\"Don't remove current user %s'\";\n"
4346+
" SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=30001,"
4347+
" MESSAGE_TEXT=\"Don't remove current user %s'\";\n"
43434348
"END IF */|\n"
4344-
"DELIMITER ;\n"
4349+
"DELIMITER ;\n"
43454350
"/*!50701 DROP USER IF EXISTS %s */;\n", row[0], row[0], row[0]);
43464351
if (dump_create_user(row[0]))
43474352
result= 1;
@@ -4354,15 +4359,37 @@ static int dump_all_users()
43544359
if (!(maria_roles_exist || mysql_roles_exist))
43554360
goto exit;
43564361

4357-
/* preserve current role. Create a new role for importing that becomes the
4358-
* default admin for new roles, so it can be dropped after roles are assigned */
4362+
/*
4363+
Preserve current role active role, in case this dump is imported
4364+
in the same connection that assumes the active role at the beginning
4365+
is the same as at the end of the connection. This is so:
4366+
4367+
#!/bin/sh
4368+
(
4369+
echo "set role special_role; ";
4370+
cat mysqldump.sql;
4371+
echo "$dosomethingspecial"
4372+
) | mysql -h $host
4373+
4374+
doesn't end up with a suprise that the $dosomethingspecial cannot
4375+
be done because `special_role` isn't active.
4376+
4377+
We create a new role for importing that becomes the default admin for new
4378+
roles. This is because without being a admin on new roles we don't
4379+
have the necessary privileges to grant users to a created role or to
4380+
create new admins for the created role.
4381+
At the end of the import the mariadb_dump_import_role is be dropped,
4382+
which implictly drops all its admin aspects of all the role.
4383+
This is significiantly easlier than revoking the ADMIN of each role
4384+
from the current user.
4385+
*/
43594386
fputs("SELECT COALESCE(CURRENT_ROLE(),'NONE') into @current_role;\n"
43604387
"CREATE ROLE IF NOT EXISTS mariadb_dump_import_role;\n"
43614388
"GRANT mariadb_dump_import_role TO CURRENT_USER();\n"
43624389
"SET ROLE mariadb_dump_import_role;\n"
43634390
, md_result_file);
43644391
/* No show create role yet, MDEV-22311 */
4365-
/* Roles, with user admins first, then roles they adminster, and recurse on that */
4392+
/* Roles, with user admins first, then roles they administer, and recurse on that */
43664393
if (maria_roles_exist && mysql_query_with_error_report(mysql, &tableres,
43674394
"WITH RECURSIVE create_role_order AS"
43684395
" (SELECT 1 as n, roles_mapping.*"
@@ -4401,7 +4428,8 @@ static int dump_all_users()
44014428
" JOIN mysql.role_edges re ON c.FROM_USER=re.TO_USER"
44024429
" AND c.FROM_HOST=re.TO_HOST) "
44034430
"SELECT CONCAT(QUOTE(FROM_USER), '/*!80001 @', QUOTE(FROM_HOST), '*/') AS r,"
4404-
" CONCAT(QUOTE(TO_USER), IF(n=1, CONCAT('@', QUOTE(TO_HOST)), CONCAT('/*!80001 @', QUOTE(TO_HOST), ' */'))) AS u,"
4431+
" CONCAT(QUOTE(TO_USER), IF(n=1, CONCAT('@', QUOTE(TO_HOST)),"
4432+
" CONCAT('/*!80001 @', QUOTE(TO_HOST), ' */'))) AS u,"
44054433
" WITH_ADMIN_OPTION "
44064434
"FROM create_role_order "
44074435
"ORDER BY n,"
@@ -4436,7 +4464,8 @@ static int dump_all_users()
44364464
"/*M!100005 WHERE is_role='N' */"))
44374465
return 1;
44384466
if (mysql_roles_exist && mysql_roles_exist && mysql_query_with_error_report(mysql, &tableres,
4439-
"SELECT IF(DEFAULT_ROLE_HOST IS NULL, 'NONE', CONCAT(QUOTE(DEFAULT_ROLE_USER), '@', QUOTE(DEFAULT_ROLE_HOST))) as r,"
4467+
"SELECT IF(DEFAULT_ROLE_HOST IS NULL, 'NONE', CONCAT(QUOTE(DEFAULT_ROLE_USER),"
4468+
" '@', QUOTE(DEFAULT_ROLE_HOST))) as r,"
44404469
" CONCAT(QUOTE(mu.USER),'@',QUOTE(mu.HOST)) as u "
44414470
"FROM mysql.user mu LEFT JOIN mysql.default_roles using (USER, HOST)"))
44424471
return 1;
@@ -4489,16 +4518,22 @@ static int dump_all_plugins()
44894518

44904519
if (mysql_query_with_error_report(mysql, &tableres, "SHOW PLUGINS"))
44914520
return 1;
4492-
/* Name, Status, Type, Library,License */
4521+
/* Name, Status, Type, Library, License */
44934522
while ((row= mysql_fetch_row(tableres)))
44944523
{
44954524
if (strcmp("ACTIVE", row[1]) != 0)
44964525
continue;
44974526
/* Should we be skipping builtins? */
44984527
if (row[3] == NULL)
44994528
continue;
4529+
if (opt_replace_into)
4530+
{
4531+
fprintf(md_result_file, "/*M!100401 UNINSTALL PLUGIN IF EXIST %s */;\n",
4532+
row[0]);
4533+
}
45004534
fprintf(md_result_file,
4501-
"INSTALL PLUGIN %s %s SONAME '%s';\n", row[0], opt_ignore ? "/*!100401 IF NOT EXISTS */" : "", row[3]);
4535+
"INSTALL PLUGIN %s %s SONAME '%s';\n", row[0],
4536+
opt_ignore ? "/*M!100401 IF NOT EXISTS */" : "", row[3]);
45024537
}
45034538
mysql_free_result(tableres);
45044539

@@ -4507,16 +4542,16 @@ static int dump_all_plugins()
45074542

45084543

45094544
/*
4510-
dump all udf
4545+
dump all udfs
45114546
*/
45124547

4513-
static int dump_all_udf()
4548+
static int dump_all_udfs()
45144549
{
45154550
/* we don't support all these types yet, but get prepared if we do */
45164551
static const char *udf_types[] = {"STRING", "REAL", "INT", "ROW", "DECIMAL", "TIME" };
45174552
MYSQL_ROW row;
45184553
MYSQL_RES *tableres;
4519-
int retresult;
4554+
int retresult, result= 0;
45204555

45214556
if (mysql_query_with_error_report(mysql, &tableres, "SELECT * FROM mysql.func"))
45224557
return 1;
@@ -4526,9 +4561,15 @@ static int dump_all_udf()
45264561
retresult= atoi(row[1]);
45274562
if (retresult < 0 || array_elements(udf_types) <= (size_t) retresult)
45284563
{
4529-
fprintf(stderr, "%s: Error: invalid return type on udf function '%s'\n",
4564+
fprintf(stderr, "%s: Error: invalid return type on udf function '%s'\n",
45304565
my_progname_short, row[0]);
4531-
return 1;
4566+
result= 1;
4567+
continue;
4568+
}
4569+
if (opt_replace_into)
4570+
{
4571+
fprintf(md_result_file, "/*!50701 DROP FUNCTION IF EXISTS %s */;\n",
4572+
row[0]);
45324573
}
45334574
fprintf(md_result_file,
45344575
"CREATE %s%sFUNCTION %s%s RETURNS %s SONAME '%s';\n",
@@ -4538,12 +4579,12 @@ static int dump_all_udf()
45384579
}
45394580
mysql_free_result(tableres);
45404581

4541-
return 0;
4582+
return result;
45424583
}
45434584

45444585

45454586
/*
4546-
dump all plugins
4587+
dump all servers
45474588
*/
45484589

45494590
static int dump_all_servers()
@@ -4583,7 +4624,7 @@ static int dump_all_servers()
45834624

45844625

45854626
/*
4586-
dump all system statitical tables
4627+
dump all system statistical tables
45874628
*/
45884629

45894630
static int dump_all_stats()
@@ -6662,14 +6703,14 @@ int main(int argc, char **argv)
66626703
}
66636704
}
66646705

6665-
if (opt_system & OPT_SYSTEM_PLUGIN)
6706+
if (opt_system & OPT_SYSTEM_PLUGINS)
66666707
dump_all_plugins();
66676708

6668-
if (opt_system & OPT_SYSTEM_USER)
6669-
dump_all_users();
6709+
if (opt_system & OPT_SYSTEM_USERS)
6710+
dump_all_users_roles_and_grants();
66706711

6671-
if (opt_system & OPT_SYSTEM_UDF)
6672-
dump_all_udf();
6712+
if (opt_system & OPT_SYSTEM_UDFS)
6713+
dump_all_udfs();
66736714

66746715
if (opt_system & OPT_SYSTEM_SERVERS)
66756716
dump_all_servers();

man/mysqldump.1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ tables, consider using the
3636
instead because it can accomplish faster backups and faster restores\&. See
3737
\fBmysqlhotcopy\fR(1)\&.
3838
.PP
39-
There are three general ways to invoke
39+
There are four general ways to invoke
4040
\fBmysqldump\fR:
4141
.sp
4242
.if n \{\
@@ -46,6 +46,7 @@ There are three general ways to invoke
4646
shell> \fBmysqldump [\fR\fB\fIoptions\fR\fR\fB] \fR\fB\fIdb_name\fR\fR\fB [\fR\fB\fItbl_name\fR\fR\fB \&.\&.\&.]\fR
4747
shell> \fBmysqldump [\fR\fB\fIoptions\fR\fR\fB] \-\-databases \fR\fB\fIdb_name\fR\fR\fB \&.\&.\&.\fR
4848
shell> \fBmysqldump [\fR\fB\fIoptions\fR\fR\fB] \-\-all\-databases\fR
49+
shell> \fBmysqldump [\fR\fB\fIoptions\fR\fR\fB] \-\-system={options}\fR
4950
.fi
5051
.if n \{\
5152
.RE

0 commit comments

Comments
 (0)