Skip to content

Commit

Permalink
Merge pull request #710 from mydumper/enh_620
Browse files Browse the repository at this point in the history
Adding option to add IF NOT EXISTS to CREATE TABLE
  • Loading branch information
davidducos committed Jun 2, 2022
2 parents cd3411b + 0f1f58f commit 213da75
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/myloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ gchar *purge_mode_str=NULL;
gchar *set_names_str=NULL;
guint errors = 0;
guint max_threads_per_table=4;
gboolean append_if_not_exist=FALSE;
//unsigned long long int total_data_sql_files = 0;
//unsigned long long int progress = 0;
//GHashTable *db_hash=NULL;
Expand Down Expand Up @@ -116,6 +117,8 @@ static GOptionEntry entries[] = {
"Number of queries per transaction, default 1000", NULL},
{"overwrite-tables", 'o', 0, G_OPTION_ARG_NONE, &overwrite_tables,
"Drop tables if they already exist", NULL},
{"append-if-not-exist", 0, 0, G_OPTION_ARG_NONE,&append_if_not_exist,
"Appends IF NOT EXISTS to the create table statements. This will be removed when https://bugs.mysql.com/bug.php?id=103791 has been implemented", NULL},
{"database", 'B', 0, G_OPTION_ARG_STRING, &db,
"An alternative database to restore into", NULL},
{"source-db", 's', 0, G_OPTION_ARG_STRING, &source_db,
Expand Down
13 changes: 12 additions & 1 deletion src/myloader_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extern guint total_data_sql_files;
extern gboolean no_delete;
extern GHashTable *tbl_hash;
extern gboolean innodb_optimize_keys;
extern gboolean append_if_not_exist;

struct configuration *conf;
GMutex *table_hash_mutex=NULL;
Expand Down Expand Up @@ -148,7 +149,7 @@ void load_schema(struct db_table *dbt, gchar *filename){
while (eof == FALSE) {
if (read_data(infile, is_compressed, data, &eof,&line)) {
if (g_strrstr(&data->str[data->len >= 5 ? data->len - 5 : 0], ";\n")) {
if (g_strrstr(data->str,"CREATE ")){
if (g_strstr_len(data->str,13,"CREATE TABLE ")){
gchar** create_table= g_strsplit(data->str, "`", 3);
dbt->real_table=g_strdup(create_table[1]);
if ( g_str_has_prefix(dbt->table,"mydumper_")){
Expand All @@ -157,7 +158,17 @@ void load_schema(struct db_table *dbt, gchar *filename){
g_hash_table_insert(tbl_hash, dbt->real_table, dbt->real_table);
}
g_strfreev(create_table);
if (append_if_not_exist){
if ((g_strstr_len(data->str,13,"CREATE TABLE ")) && !(g_strstr_len(data->str,15,"CREATE TABLE IF"))){
GString *tmp_data=g_string_sized_new(data->len);
g_string_append(tmp_data, "CREATE TABLE IF NOT EXISTS ");
g_string_append(tmp_data, &(data->str[13]));
g_string_free(data,TRUE);
data=tmp_data;
}
}
}

if (innodb_optimize_keys){
GString *alter_table_statement=g_string_sized_new(512);
GString *alter_table_constraint_statement=g_string_sized_new(512);
Expand Down

0 comments on commit 213da75

Please sign in to comment.