Skip to content

Commit c207f04

Browse files
committed
MDEV-21785: sequences used as default by other table not dumped in right order by mysqldump
Dump sequences first. This atch made to keep it small and to keep number of queries to the server the same. Order of tables in a dump can not be changed (except sequences first) because mysql_list_tables uses SHOW TABLES and I used SHOW FULL TABLES.
1 parent 75538f9 commit c207f04

File tree

3 files changed

+115
-17
lines changed

3 files changed

+115
-17
lines changed

client/mysqldump.c

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
/* on merge conflict, bump to a higher version again */
4343
#define DUMP_VERSION "10.19"
4444

45+
/**
46+
First mysql version supporting sequences.
47+
*/
48+
#define FIRST_SEQUENCE_VERSION 100300
49+
4550
#include <my_global.h>
4651
#include <my_sys.h>
4752
#include <my_user.h>
@@ -92,6 +97,11 @@
9297
/* Max length GTID position that we will output. */
9398
#define MAX_GTID_LENGTH 1024
9499

100+
/* Dump sequence/tables control */
101+
#define DUMP_TABLE_ALL -1
102+
#define DUMP_TABLE_TABLE 0
103+
#define DUMP_TABLE_SEQUENCE 1
104+
95105
static my_bool ignore_table_data(const uchar *hash_key, size_t len);
96106
static void add_load_option(DYNAMIC_STRING *str, const char *option,
97107
const char *option_value);
@@ -3876,14 +3886,6 @@ static void dump_table(const char *table, const char *db, const uchar *hash_key,
38763886
MYSQL_ROW row;
38773887
DBUG_ENTER("dump_table");
38783888

3879-
/*
3880-
Check does table has a sequence structure and if has apply different sql queries
3881-
*/
3882-
if (check_if_ignore_table(table, table_type) & IGNORE_SEQUENCE_TABLE)
3883-
{
3884-
get_sequence_structure(table, db);
3885-
DBUG_VOID_RETURN;
3886-
}
38873889
/*
38883890
Make sure you get the create table info before the following check for
38893891
--no-data flag below. Otherwise, the create table info won't be printed.
@@ -4368,18 +4370,36 @@ static void dump_table(const char *table, const char *db, const uchar *hash_key,
43684370
} /* dump_table */
43694371

43704372

4371-
static char *getTableName(int reset)
4373+
static char *getTableName(int reset, int want_sequences)
43724374
{
43734375
MYSQL_ROW row;
43744376

43754377
if (!get_table_name_result)
43764378
{
4377-
if (!(get_table_name_result= mysql_list_tables(mysql,NullS)))
4378-
return(NULL);
4379+
if (mysql_get_server_version(mysql) >= FIRST_SEQUENCE_VERSION)
4380+
{
4381+
const char *query= "SHOW FULL TABLES";
4382+
if (mysql_query_with_error_report(mysql, 0, query))
4383+
return (NULL);
4384+
4385+
if (!(get_table_name_result= mysql_store_result(mysql)))
4386+
return (NULL);
4387+
}
4388+
else
4389+
{
4390+
if (!(get_table_name_result= mysql_list_tables(mysql,NullS)))
4391+
return(NULL);
4392+
}
43794393
}
43804394
if ((row= mysql_fetch_row(get_table_name_result)))
4381-
return((char*) row[0]);
4395+
{
4396+
if (want_sequences != DUMP_TABLE_ALL)
4397+
while (row && MY_TEST(strcmp(row[1], "SEQUENCE")) == want_sequences)
4398+
row= mysql_fetch_row(get_table_name_result);
43824399

4400+
if (row)
4401+
return((char*) row[0]);
4402+
}
43834403
if (reset)
43844404
mysql_data_seek(get_table_name_result,0); /* We want to read again */
43854405
else
@@ -5312,7 +5332,7 @@ static int dump_all_tables_in_db(char *database)
53125332
{
53135333
DYNAMIC_STRING query;
53145334
init_dynamic_string_checked(&query, "LOCK TABLES ", 256, 1024);
5315-
for (numrows= 0 ; (table= getTableName(1)) ; )
5335+
for (numrows= 0 ; (table= getTableName(1, DUMP_TABLE_ALL)) ; )
53165336
{
53175337
char *end= strmov(afterdot, table);
53185338
if (include_table((uchar*) hash_key,end - hash_key))
@@ -5346,7 +5366,19 @@ static int dump_all_tables_in_db(char *database)
53465366
DBUG_RETURN(1);
53475367
}
53485368
}
5349-
while ((table= getTableName(0)))
5369+
5370+
if (mysql_get_server_version(mysql) >= FIRST_SEQUENCE_VERSION &&
5371+
!opt_no_create_info)
5372+
{
5373+
// First process sequences
5374+
while ((table= getTableName(1, DUMP_TABLE_SEQUENCE)))
5375+
{
5376+
char *end= strmov(afterdot, table);
5377+
if (include_table((uchar*) hash_key, end - hash_key))
5378+
get_sequence_structure(table, database);
5379+
}
5380+
}
5381+
while ((table= getTableName(0, DUMP_TABLE_TABLE)))
53505382
{
53515383
char *end= strmov(afterdot, table);
53525384
if (include_table((uchar*) hash_key, end - hash_key))
@@ -5495,7 +5527,7 @@ static my_bool dump_all_views_in_db(char *database)
54955527
{
54965528
DYNAMIC_STRING query;
54975529
init_dynamic_string_checked(&query, "LOCK TABLES ", 256, 1024);
5498-
for (numrows= 0 ; (table= getTableName(1)); )
5530+
for (numrows= 0 ; (table= getTableName(1, DUMP_TABLE_TABLE)); )
54995531
{
55005532
char *end= strmov(afterdot, table);
55015533
if (include_table((uchar*) hash_key,end - hash_key))
@@ -5518,7 +5550,7 @@ static my_bool dump_all_views_in_db(char *database)
55185550
else
55195551
verbose_msg("-- dump_all_views_in_db : logs flushed successfully!\n");
55205552
}
5521-
while ((table= getTableName(0)))
5553+
while ((table= getTableName(0, DUMP_TABLE_TABLE)))
55225554
{
55235555
char *end= strmov(afterdot, table);
55245556
if (include_table((uchar*) hash_key, end - hash_key))
@@ -5648,7 +5680,7 @@ static int get_sys_var_lower_case_table_names()
56485680

56495681
static int dump_selected_tables(char *db, char **table_names, int tables)
56505682
{
5651-
char table_buff[NAME_LEN*2+3];
5683+
char table_buff[NAME_LEN*2+3], table_type[NAME_LEN];
56525684
DYNAMIC_STRING lock_tables_query;
56535685
char **dump_tables, **pos, **end;
56545686
int lower_case_table_names;
@@ -5745,9 +5777,22 @@ static int dump_selected_tables(char *db, char **table_names, int tables)
57455777
DBUG_RETURN(1);
57465778
}
57475779
}
5780+
5781+
if (mysql_get_server_version(mysql) >= FIRST_SEQUENCE_VERSION)
5782+
{
5783+
/* Dump Sequence first */
5784+
for (pos= dump_tables; pos < end; pos++)
5785+
{
5786+
DBUG_PRINT("info",("Dumping sequence(?) %s", *pos));
5787+
if (check_if_ignore_table(*pos, table_type) & IGNORE_SEQUENCE_TABLE)
5788+
get_sequence_structure(*pos, db);
5789+
}
5790+
}
57485791
/* Dump each selected table */
57495792
for (pos= dump_tables; pos < end; pos++)
57505793
{
5794+
if (check_if_ignore_table(*pos, table_type) & IGNORE_SEQUENCE_TABLE)
5795+
continue;
57515796
DBUG_PRINT("info",("Dumping table %s", *pos));
57525797
dump_table(*pos, db, NULL, 0);
57535798
if (opt_dump_triggers &&

mysql-test/suite/sql_sequence/mysqldump.result

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,46 @@ CREATE SEQUENCE a1 engine=aria;
22
CREATE TABLE t1(a INT, KEY (a)) KEY_BLOCK_SIZE=1024;
33
insert into t1 values (1),(2);
44
CREATE SEQUENCE x1 engine=innodb;
5+
# dump whole database
56
CREATE SEQUENCE `a1` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=Aria;
67
SELECT SETVAL(`a1`, 1, 0);
8+
CREATE SEQUENCE `x1` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB;
9+
SELECT SETVAL(`x1`, 1, 0);
10+
/*!40101 SET @saved_cs_client = @@character_set_client */;
11+
/*!40101 SET character_set_client = utf8 */;
12+
CREATE TABLE `t1` (
13+
`a` int(11) DEFAULT NULL,
14+
KEY `a` (`a`)
15+
) ENGINE=MyISAM DEFAULT CHARSET=latin1 KEY_BLOCK_SIZE=1024;
16+
/*!40101 SET character_set_client = @saved_cs_client */;
17+
INSERT INTO `t1` VALUES (1),(2);
18+
# dump by tables order 1
19+
CREATE SEQUENCE `a1` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=Aria;
20+
SELECT SETVAL(`a1`, 1, 0);
21+
CREATE SEQUENCE `x1` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB;
22+
SELECT SETVAL(`x1`, 1, 0);
23+
/*!40101 SET @saved_cs_client = @@character_set_client */;
24+
/*!40101 SET character_set_client = utf8 */;
25+
CREATE TABLE `t1` (
26+
`a` int(11) DEFAULT NULL,
27+
KEY `a` (`a`)
28+
) ENGINE=MyISAM DEFAULT CHARSET=latin1 KEY_BLOCK_SIZE=1024;
29+
/*!40101 SET character_set_client = @saved_cs_client */;
30+
INSERT INTO `t1` VALUES (1),(2);
31+
# dump by tables order 2
32+
CREATE SEQUENCE `a1` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=Aria;
33+
SELECT SETVAL(`a1`, 1, 0);
34+
CREATE SEQUENCE `x1` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB;
35+
SELECT SETVAL(`x1`, 1, 0);
36+
/*!40101 SET @saved_cs_client = @@character_set_client */;
37+
/*!40101 SET character_set_client = utf8 */;
38+
CREATE TABLE `t1` (
39+
`a` int(11) DEFAULT NULL,
40+
KEY `a` (`a`)
41+
) ENGINE=MyISAM DEFAULT CHARSET=latin1 KEY_BLOCK_SIZE=1024;
42+
/*!40101 SET character_set_client = @saved_cs_client */;
43+
INSERT INTO `t1` VALUES (1),(2);
44+
# dump by tables only tables
745
/*!40101 SET @saved_cs_client = @@character_set_client */;
846
/*!40101 SET character_set_client = utf8 */;
947
CREATE TABLE `t1` (
@@ -12,8 +50,12 @@ CREATE TABLE `t1` (
1250
) ENGINE=MyISAM DEFAULT CHARSET=latin1 KEY_BLOCK_SIZE=1024;
1351
/*!40101 SET character_set_client = @saved_cs_client */;
1452
INSERT INTO `t1` VALUES (1),(2);
53+
# dump by tables only sequences
54+
CREATE SEQUENCE `a1` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=Aria;
55+
SELECT SETVAL(`a1`, 1, 0);
1556
CREATE SEQUENCE `x1` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB;
1657
SELECT SETVAL(`x1`, 1, 0);
58+
# end of dumps
1759
DROP TABLE a1,t1,x1;
1860
set default_storage_engine=InnoDB;
1961
create sequence t1;

mysql-test/suite/sql_sequence/mysqldump.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@ CREATE SEQUENCE a1 engine=aria;
1111
CREATE TABLE t1(a INT, KEY (a)) KEY_BLOCK_SIZE=1024;
1212
insert into t1 values (1),(2);
1313
CREATE SEQUENCE x1 engine=innodb;
14+
--echo # dump whole database
1415
--exec $MYSQL_DUMP --compact test
16+
--echo # dump by tables order 1
17+
--exec $MYSQL_DUMP --compact --tables test t1 a1 x1
18+
--echo # dump by tables order 2
19+
--exec $MYSQL_DUMP --compact --tables test a1 t1 x1
20+
--echo # dump by tables only tables
21+
--exec $MYSQL_DUMP --compact --tables test t1
22+
--echo # dump by tables only sequences
23+
--exec $MYSQL_DUMP --compact --tables test a1 x1
24+
--echo # end of dumps
25+
1526
DROP TABLE a1,t1,x1;
1627

1728
#

0 commit comments

Comments
 (0)