Debian Codegraph @

storage/innobase/dict/dict0upgrade.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1996, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27 #include "dict0upgrade.h"
28 #include <sql_backup_lock.h>
29 #include <sql_class.h>
30 #include <sql_show.h>
31 #include <sql_table.h>
32 #include <sql_tablespace.h>
33
34 #include <algorithm>
35 #include <regex>
36
37 #include "dict0boot.h"
38 #include "dict0crea.h"
39 #include "dict0dd.h"
40 #include "dict0dict.h"
41 #include "dict0load.h"
42 #include "ha_innodb.h"
43 #include "ha_innopart.h"
44 #include "log0buf.h"
45 #include "log0chkp.h"
46 #include "row0sel.h"
47 #include "srv0start.h"
48
49 /* This is used only during upgrade. We don't use ids
50 from DICT_HDR during upgrade because unlike bootstrap case,
51 the ids are moved after user table creation. Since we
52 want to create dictionary tables with fixed ids, we use
53 in-memory counter for upgrade */
54 uint32_t dd_upgrade_indexes_num = 1;
55
56 /** Vector of tables that have FTS indexes. Used for
57 reverting from 8.0 format FTS AUX table names to
58 5.7 FTS AUX table names */
59 static std::vector<std::string> tables_with_fts;
60
61 /** Fill foreign key information from InnoDB table to
62 server table
63 @param[in] ib_table InnoDB table object
64 @param[in,out] dd_table DD table object
65 @return false on success, otherwise true */
66 static bool dd_upgrade_table_fk(dict_table_t *ib_table, dd::Table *dd_table) {
67 for (dict_foreign_set::iterator it = ib_table->foreign_set.begin();
68 it != ib_table->foreign_set.end(); ++it) {
69 dict_foreign_t *foreign = *it;
70
71 /* Set the foreign_key name. */
72 dd::Foreign_key *fk_obj = dd_table->add_foreign_key();
73
74 /* Check if the foreign key name is valid */
75 if (innobase_check_identifier_length(strchr(foreign->id, '/') + 1)) {
76 ib::error(ER_IB_MSG_229)
77 << "Foreign key name:" << foreign->id
78 << " is too long, for the table:" << dd_table->name()
79 << ". Please ALTER the foreign key name to use less"
80 " than 64 characters and try upgrade again.\n";
81 return true;
82 }
83
84 /* Ignore the schema name prefixed with the foreign_key name */
85 if (strchr(foreign->id, '/'))
86 fk_obj->set_name(strchr(foreign->id, '/') + 1);
87 else
88 fk_obj->set_name(foreign->id);
89
90 /* Don't set unique constraint name, it will be set by SQL-layer later. */
91
92 /* Set match option. Unused for InnoDB */
93 fk_obj->set_match_option(dd::Foreign_key::OPTION_NONE);
94
95 /* Set Update rule */
96 if (foreign->type & DICT_FOREIGN_ON_UPDATE_CASCADE) {
97 fk_obj->set_update_rule(dd::Foreign_key::RULE_CASCADE);
98 } else if (foreign->type & DICT_FOREIGN_ON_UPDATE_SET_NULL) {
99 fk_obj->set_update_rule(dd::Foreign_key::RULE_SET_NULL);
100 } else if (foreign->type & DICT_FOREIGN_ON_UPDATE_NO_ACTION) {
101 fk_obj->set_update_rule(dd::Foreign_key::RULE_NO_ACTION);
102 } else {
103 fk_obj->set_update_rule(dd::Foreign_key::RULE_RESTRICT);
104 }
105
106 /* Set delete rule */
107 if (foreign->type & DICT_FOREIGN_ON_DELETE_CASCADE) {
108 fk_obj->set_delete_rule(dd::Foreign_key::RULE_CASCADE);
109 } else if (foreign->type & DICT_FOREIGN_ON_DELETE_SET_NULL) {
110 fk_obj->set_delete_rule(dd::Foreign_key::RULE_SET_NULL);
111 } else if (foreign->type & DICT_FOREIGN_ON_DELETE_NO_ACTION) {
112 fk_obj->set_delete_rule(dd::Foreign_key::RULE_NO_ACTION);
113 } else {
114 fk_obj->set_delete_rule(dd::Foreign_key::RULE_RESTRICT);
115 }
116
117 /* Set catalog name */
118 fk_obj->set_referenced_table_catalog_name("def");
119
120 /* Set referenced table schema name */
121 std::string db_str;
122 std::string tbl_str;
123 dict_name::get_table(foreign->referenced_table_name, db_str, tbl_str);
124
125 fk_obj->set_referenced_table_schema_name(db_str.c_str());
126 fk_obj->set_referenced_table_name(tbl_str.c_str());
127
128 /* Set referencing columns */
129 for (uint32_t i = 0; i < foreign->n_fields; i++) {
130 dd::Foreign_key_element *fk_col_obj = fk_obj->add_element();
131
132 const char *foreign_col = foreign->foreign_col_names[i];
133 ut_ad(foreign_col != nullptr);
134 const dd::Column *column = dd_table->get_column(
135 dd::String_type(foreign_col, strlen(foreign_col)));
136 ut_ad(column != nullptr);
137 fk_col_obj->set_column(column);
138
139 const char *referenced_col = foreign->referenced_col_names[i];
140 ut_ad(referenced_col != nullptr);
141
142 DBUG_EXECUTE_IF("dd_upgrade",
143 ib::info(ER_IB_MSG_230)
144 << "FK on table: " << ib_table->name
145 << " col: " << foreign_col << " references col: "
146 << " of table: " << foreign->referenced_table_name;);
147
148 fk_col_obj->referenced_column_name(
149 dd::String_type(referenced_col, strlen(referenced_col)));
150 }
151
152 DBUG_EXECUTE_IF(
153 "dd_upgrade", ib::info(ER_IB_MSG_231)
154 << "foreign name: " << foreign->id;
155 ib::info(ER_IB_MSG_232) << " foreign fields: " << foreign->n_fields;
156 ib::info(ER_IB_MSG_233) << " foreign type: " << foreign->type;
157 ib::info(ER_IB_MSG_234)
158 << " foreign table name: " << foreign->foreign_table_name;
159 ib::info(ER_IB_MSG_235)
160 << " referenced table name: " << foreign->referenced_table_name;
161 ib::info(ER_IB_MSG_236)
162 << " foreign index: " << foreign->foreign_index->name;
163 ib::info(ER_IB_MSG_237)
164 << " foreign table: " << foreign->foreign_index->table->name;);
165 }
166
167 return false;
168 }
169
170 /** Get Server Tablespace object for a InnoDB table. The tablespace is
171 acquired with MDL and for modification, so the caller can update the
172 dd::Tablespace object returned.
173 @param[in,out] dd_client dictionary client to retrieve tablespace
174 object
175 @param[in] ib_table InnoDB table
176 @return dd::Tablespace object or nullptr */
177 static dd::Tablespace *dd_upgrade_get_tablespace(
178 dd::cache::Dictionary_client *dd_client, dict_table_t *ib_table) {
179 std::string tablespace_name;
180
181 dd::Tablespace *ts_obj = nullptr;
182 ut_ad(ib_table->space != SPACE_UNKNOWN);
183 ut_ad(ib_table->space != SYSTEM_TABLE_SPACE);
184
185 if (dict_table_is_file_per_table(ib_table)) {
186 tablespace_name.assign(ib_table->name.m_name);
187 dict_name::convert_to_space(tablespace_name);
188
189 } else {
190 ut_ad(DICT_TF_HAS_SHARED_SPACE(ib_table->flags));
191 if (ib_table->tablespace == nullptr) return (ts_obj);
192 tablespace_name.assign(ib_table->tablespace());
193 }
194 ut_ad(tablespace_name.length() < MAX_FULL_NAME_LEN);
195
196 DBUG_EXECUTE_IF("dd_upgrade", ib::info(ER_IB_MSG_238)
197 << "The derived tablespace name is: "
198 << tablespace_name;);
199
200 /* MDL on tablespace name */
201 if (dd_tablespace_get_mdl(tablespace_name.c_str())) {
202 ut_error;
203 }
204
205 /* For file per table tablespaces and general tablespaces, we will get
206 the tablespace object and then get space_id. */
207 if (dd_client->acquire_for_modification(tablespace_name.c_str(), &ts_obj)) {
208 ut_error;
209 }
210
211 return (ts_obj);
212 }
213
214 /** Get field from Server table object
215 @param[in] srv_table server table object
216 @param[in] name Field name
217 @return field object if found or null on failure. */
218 static Field *dd_upgrade_get_field(const TABLE *srv_table, const char *name) {
219 for (uint i = 0; i < srv_table->s->fields; i++) {
220 Field *field = srv_table->field[i];
221 if (strcmp(field->field_name, name) == 0) {
222 return (field);
223 }
224 }
225 return (nullptr);
226 }
227
228 /** Return true if table has primary key given by user else false
229 @param[in] dd_table Server table object
230 @retval true Table has PK given by user
231 @retval false Primary Key is hidden and generated */
232 static bool dd_has_explicit_pk(const dd::Table *dd_table) {
233 return (!dd_table->indexes().front()->is_hidden());
234 }
235
236 /** Match InnoDB column object and Server column object
237 @param[in] field Server field object
238 @param[in] col InnoDB column object
239 @retval false column definition matches
240 @retval true column definition mismatch */
241 static bool dd_upgrade_match_single_col(const Field *field,
242 const dict_col_t *col) {
243 ulint unsigned_type;
244 ulint col_type = get_innobase_type_from_mysql_type(&unsigned_type, field);
245
246 bool failure = false;
247
248 DBUG_EXECUTE_IF("dd_upgrade_strict_mode", ut_ad(col->mtype == col_type););
249
250 /* The columns of datatype MYSQL_TYPE_GEOMETRY were represented in InnoDB
251 as DATA_BLOB until 5.7 where they were changed to DATA_GEOMETRY type.
252 However, the following check fails while upgrading a 5.7 database with
253 GEOMETRY columns originally created with 5.6.
254 It is safe to ignore this datatype mismatch here so that upgrade can proceed
255 without affecting anything else. The correct datatype will be reflected in the
256 metadata once it is upgraded. */
257 if (col_type == DATA_GEOMETRY && col->mtype == DATA_BLOB) {
258 ib::warn(ER_IB_WRN_OLD_GEOMETRY_TYPE, field->field_name);
259 } else if (col->mtype != col_type) {
260 ib::error(ER_IB_MSG_239)
261 << "Column datatype mismatch for col: " << field->field_name;
262 failure = true;
263 }
264
265 ulint nulls_allowed = field->is_nullable() ? 0 : DATA_NOT_NULL;
266 ulint binary_type = field->binary() ? DATA_BINARY_TYPE : 0;
267 ulint charset_no = 0;
268
269 if (dtype_is_string_type(col_type)) {
270 charset_no = (ulint)field->charset()->number;
271
272 if (charset_no > MAX_CHAR_COLL_NUM) {
273 ib::error(ER_IB_MSG_240)
274 << "In InnoDB, charset-collation codes"
275 << " must be below 256. Unsupported code " << charset_no;
276 DBUG_EXECUTE_IF("dd_upgrade_strict_mode", bool invalid_collation = true;
277 ut_ad(!invalid_collation););
278
279 failure = true;
280 }
281 }
282 ulint col_len = field->pack_length();
283
284 /* The MySQL pack length contains 1 or 2 bytes length field
285 for a true VARCHAR. Let us subtract that, so that the InnoDB
286 column length in the InnoDB data dictionary is the real
287 maximum byte length of the actual data. */
288
289 ulint long_true_varchar = 0;
290
291 if (field->type() == MYSQL_TYPE_VARCHAR) {
292 col_len -= field->get_length_bytes();
293
294 if (field->get_length_bytes() == 2) {
295 long_true_varchar = DATA_LONG_TRUE_VARCHAR;
296 }
297 }
298
299 if (col_type == DATA_POINT) {
300 col_len = DATA_POINT_LEN;
301 }
302
303 ulint is_virtual = (innobase_is_v_fld(field)) ? DATA_VIRTUAL : 0;
304
305 ulint server_prtype =
306 (static_cast<ulint>(field->type()) | nulls_allowed | unsigned_type |
307 binary_type | long_true_varchar | is_virtual);
308
309 /* First two bytes store charset, last two bytes store precision
310 value. Get the last two bytes. i.e precision value */
311 ulint innodb_prtype = (col->prtype & 0x0000FFFF);
312
313 if (server_prtype != innodb_prtype) {
314 ib::error(ER_IB_MSG_241)
315 << "Column precision type mismatch(i.e NULLs, SIGNED/UNSIGNED "
316 "etc) for col: "
317 << field->field_name;
318 failure = true;
319 }
320
321 /* Numeric columns from 5.1 might have charset as my_charset_bin
322 while 5.5+ will have charset as my_charset_latin1. Compare charsets
323 only if field supports character set. */
324 if (field->has_charset()) {
325 ulint col_charset = col->prtype >> 16;
326 if (charset_no != col_charset) {
327 ib::error(ER_IB_MSG_242)
328 << "Column character set mismatch for col: " << field->field_name;
329 failure = true;
330 }
331 }
332
333 DBUG_EXECUTE_IF("dd_upgrade_strict_mode", ut_ad(col->len == col_len););
334
335 if (col_len != col->len) {
336 ib::error(ER_IB_MSG_243)
337 << "Column length mismatch for col: " << field->field_name;
338 failure = true;
339 }
340
341 return (failure);
342 }
343
344 /* Match definition of all columns in InnoDB table and DD table
345 @param[in] srv_table Server table object
346 @param[in] dd_table New DD table object
347 @param[in] ib_table InnoDB table object
348 @param[in] skip_fts_col Skip FTS_DOC_ID column match
349 @retval true failure
350 @retval false success, all columns matched */
351 static bool dd_upgrade_match_cols(const TABLE *srv_table,
352 const dd::Table *dd_table,
353 const dict_table_t *ib_table,
354 bool skip_fts_col) {
355 uint32_t innodb_num_cols = ib_table->n_t_cols;
356 bool has_explicit_pk = dd_has_explicit_pk(dd_table);
357 if (has_explicit_pk) {
358 /* Even when there is explicit PK, InnoDB registers DB_ROW_ID
359 in list of columns. It is unused though. */
360 innodb_num_cols = innodb_num_cols - 1 /* DB_ROW_ID */;
361 }
362
363 if (innodb_num_cols != dd_table->columns().size()) {
364 ib::error(ER_IB_MSG_244)
365 << "table: " << dd_table->name() << " has "
366 << dd_table->columns().size() << " columns but InnoDB dictionary"
367 << " has " << innodb_num_cols << " columns";
368 DBUG_EXECUTE_IF("dd_upgrade_strict_mode", bool columns_num_mismatch = true;
369 ut_ad(!columns_num_mismatch););
370 return (true);
371 }
372
373 /* Match columns */
374 uint32_t idx = 0;
375 uint32_t v_idx = 0;
376 for (const dd::Column *col_obj : dd_table->columns()) {
377 dict_col_t *ib_col = nullptr;
378 const char *ib_col_name = nullptr;
379 if (col_obj->is_virtual()) {
380 dict_v_col_t *v_col = dict_table_get_nth_v_col(ib_table, v_idx);
381
382 ib_col = &v_col->m_col;
383 ib_col_name = dict_table_get_v_col_name(ib_table, v_idx);
384 ++v_idx;
385 } else {
386 if (strcmp(col_obj->name().c_str(), FTS_DOC_ID_COL_NAME) == 0 &&
387 skip_fts_col) {
388 continue;
389 }
390 ib_col_name = ib_table->get_col_name(idx);
391 if (strcmp(ib_col_name, FTS_DOC_ID_COL_NAME) == 0 && skip_fts_col) {
392 ++idx;
393 }
394 ib_col_name = ib_table->get_col_name(idx);
395 if (has_explicit_pk && strcmp(ib_col_name, "DB_ROW_ID") == 0) {
396 ++idx;
397 }
398
399 ib_col = ib_table->get_col(idx);
400 ib_col_name = ib_table->get_col_name(idx);
401 ++idx;
402 }
403
404 if (strcmp(ib_col_name, col_obj->name().c_str()) == 0) {
405 /* Skip matching hidden fields like DB_ROW_ID, DB_TRX_ID because
406 these don't exist in TABLE* object of server. */
407 if (!col_obj->is_se_hidden()) {
408 /* Match col object and field */
409 Field *field = dd_upgrade_get_field(srv_table, ib_col_name);
410 ut_ad(field != nullptr);
411 ut_ad(ib_col != nullptr);
412 bool failure = dd_upgrade_match_single_col(field, ib_col);
413 if (failure) {
414 ib::error(ER_IB_MSG_245) << "Column " << col_obj->name()
415 << " for table: " << ib_table->name
416 << " mismatches with InnoDB Dictionary";
417 DBUG_EXECUTE_IF("dd_upgrade_strict_mode", bool column_mismatch = true;
418 ut_ad(!column_mismatch););
419 return (true);
420 }
421 }
422 } else {
423 ib::error(ER_IB_MSG_246)
424 << "Column name mismatch: From InnoDB: " << ib_col_name
425 << " From Server: " << col_obj->name();
426 DBUG_EXECUTE_IF("dd_upgrade_strict_mode",
427 bool column_name_mismatch = true;
428 ut_ad(!column_name_mismatch););
429 return (true);
430 }
431 }
432
433 #ifdef UNIV_DEBUG
434 uint32_t processed_columns_num = idx + v_idx;
435 if (has_explicit_pk) {
436 processed_columns_num -= 1;
437 }
438 ut_ad(processed_columns_num == dd_table->columns().size());
439 #endif /* UNIV_DEBUG */
440
441 return (false);
442 }
443
444 /** Find key number from a server table object
445 @param[in] srv_table server table object
446 @param[in] name index name
447 @retval UINT32_MAX if index not found, else key number */
448 static uint32_t dd_upgrade_find_index(TABLE *srv_table, const char *name) {
449 for (uint32_t i = 0; i < srv_table->s->keys; i++) {
450 KEY *key = srv_table->key_info + i;
451 if (strcmp(key->name, name) == 0) {
452 return (i);
453 }
454 }
455 return (UINT32_MAX);
456 }
457
458 /** Match InnoDB index definition from Server object
459 @param[in] srv_table Server table object
460 @param[in] index InnoDB index
461 @retval false Index definition matches
462 @retval true Index definition mismatch */
463 static bool dd_upgrade_match_index(TABLE *srv_table, dict_index_t *index) {
464 uint32_t key_no = dd_upgrade_find_index(srv_table, index->name);
465
466 if (key_no == UINT32_MAX) {
467 ib::info(ER_IB_MSG_247) << "Index: " << index->name << " exists"
468 << " in InnoDB but not in Server";
469 DBUG_EXECUTE_IF("dd_upgrade_strict_mode", bool index_not_found = true;
470 ut_ad(!index_not_found););
471 return (true);
472 }
473
474 KEY *key = srv_table->key_info + key_no;
475
476 ut_ad(key != nullptr);
477
478 DBUG_EXECUTE_IF("dd_upgrade_strict_mode", ut_ad(key->user_defined_key_parts ==
479 index->n_user_defined_cols););
480
481 if (key->user_defined_key_parts != index->n_user_defined_cols) {
482 ib::error(ER_IB_MSG_248)
483 << "The number of fields in index " << index->name
484 << " according to Server: " << key->user_defined_key_parts
485 << " according to InnoDB: " << index->n_user_defined_cols;
486 return (true);
487 }
488
489 ulint ind_type = 0;
490 if (key_no == srv_table->s->primary_key) {
491 ind_type |= DICT_CLUSTERED;
492 }
493
494 if (key->flags & HA_NOSAME) {
495 ind_type |= DICT_UNIQUE;
496 }
497
498 if (key->flags & HA_SPATIAL) {
499 ind_type |= DICT_SPATIAL;
500 }
501
502 if (key->flags & HA_FULLTEXT) {
503 ind_type |= DICT_FTS;
504 }
505
506 ulint nulls_equal = (key->flags & HA_NULL_ARE_EQUAL) ? true : false;
507
508 DBUG_EXECUTE_IF("dd_upgrade_strict_mode",
509 ut_ad(nulls_equal == index->nulls_equal););
510
511 if (nulls_equal != index->nulls_equal) {
512 ib::error(ER_IB_MSG_249) << "In index: " << index->name
513 << " NULL equal from Server: " << nulls_equal
514 << " From InnoDB: " << index->nulls_equal;
515 return (true);
516 }
517
518 for (ulint i = 0; i < key->user_defined_key_parts; i++) {
519 KEY_PART_INFO *key_part = key->key_part + i;
520
521 Field *field = srv_table->field[key_part->field->field_index()];
522 if (field == nullptr) ut_error;
523
524 const char *field_name = key_part->field->field_name;
525 dict_field_t *idx_field = index->get_field(i);
526
527 DBUG_EXECUTE_IF("dd_upgrade_strict_mode",
528 ut_ad(strcmp(field_name, idx_field->name()) == 0););
529
530 if (strcmp(field_name, idx_field->name()) != 0) {
531 ib::error(ER_IB_MSG_250)
532 << "In index: " << index->name
533 << " field name mismatches: from server: " << field_name
534 << " from InnoDB: " << idx_field->name();
535 return (true);
536 }
537
538 ulint is_unsigned;
539 ulint col_type =
540 get_innobase_type_from_mysql_type(&is_unsigned, key_part->field);
541 ulint prefix_len;
542
543 if (DATA_LARGE_MTYPE(col_type) ||
544 (key_part->length < field->pack_length() &&
545 field->type() != MYSQL_TYPE_VARCHAR) ||
546 (field->type() == MYSQL_TYPE_VARCHAR &&
547 key_part->length < field->pack_length() - field->get_length_bytes())) {
548 switch (col_type) {
549 default:
550 prefix_len = key_part->length;
551 break;
552 case DATA_INT:
553 case DATA_FLOAT:
554 case DATA_DOUBLE:
555 case DATA_DECIMAL:
556 prefix_len = 0;
557 }
558 } else {
559 prefix_len = 0;
560 }
561
562 if (!(index->type & (DICT_FTS | DICT_SPATIAL))) {
563 if (prefix_len != index->get_field(i)->prefix_len) {
564 ib::error(ER_IB_MSG_251)
565 << "In Index: " << index->name
566 << " prefix_len mismatches: from server: " << prefix_len
567 << " from InnoDB: " << index->get_field(i)->prefix_len;
568 DBUG_EXECUTE_IF("dd_upgrade_strict_mode",
569 ut_ad(prefix_len == index->get_field(i)->prefix_len););
570 return (true);
571 }
572 }
573
574 if (innobase_is_v_fld(key_part->field)) {
575 ind_type |= DICT_VIRTUAL;
576 }
577 }
578
579 DBUG_EXECUTE_IF("dd_upgrade_strict_mode", ut_ad(index->type == ind_type););
580
581 if (index->type != ind_type) {
582 ib::error(ER_IB_MSG_252) << "Index name: " << index->name
583 << " type mismatches: from server: " << ind_type
584 << " from InnoDB: " << index->type;
585 return (true);
586 }
587
588 return (false);
589 }
590
591 /* Check if the table has auto inc field
592 @param[in] srv_tabl server table object
593 @param[in,out] auto_inc_index_name Index name on which auto inc exists
594 @param[in,out] auto_inc_col_name Column name of the auto inc field
595 @retval true if auto inc field exists
596 @retval false if auto inc field doesn't exist */
597 static bool dd_upgrade_check_for_autoinc(TABLE *srv_table,
598 const char *&auto_inc_index_name,
599 const char *&auto_inc_col_name) {
600 if (srv_table->s->found_next_number_field) {
601 const Field *field = *srv_table->s->found_next_number_field;
602 KEY *key;
603 key = srv_table->s->key_info + srv_table->s->next_number_index;
604
605 auto_inc_index_name = key->name;
606 auto_inc_col_name = field->field_name;
607
608 DBUG_EXECUTE_IF("dd_upgrade", ib::info(ER_IB_MSG_253)
609 << "Index with auto_increment "
610 << key->name;);
611 if (auto_inc_index_name == nullptr || auto_inc_col_name == nullptr) {
612 return (false);
613 } else {
614 return (true);
615 }
616 } else {
617 auto_inc_index_name = nullptr;
618 auto_inc_col_name = nullptr;
619 return (false);
620 }
621 }
622
623 /** Set auto-inc field value in dd::Table object
624 @param[in] srv_table server table object
625 @param[in,out] dd_table dd table object to be filled
626 @param[in,out] auto_inc_value auto_inc value */
627 static void dd_upgrade_set_auto_inc(const TABLE *srv_table, dd::Table *dd_table,
628 uint64_t auto_inc_value) {
629 ulonglong col_max_value;
630 const Field *field = *srv_table->s->found_next_number_field;
631
632 col_max_value = field->get_max_int_value();
633
634 /* At the this stage we do not know the increment
635 nor the offset, so use a default increment of 1. */
636
637 auto_inc_value =
638 innobase_next_autoinc(auto_inc_value, 1, 1, 0, col_max_value);
639
640 dd::Properties &table_properties = dd_table->se_private_data();
641 dd_set_autoinc(table_properties, auto_inc_value);
642 }
643
644 /* Set DD Index se_private_data and also read auto_inc if it the index
645 matches with auto_inc index name
646 @param[in,out] dd_index dd::Index object
647 @param[in] index InnoDB index object
648 @param[in] dd_space_id Server space id for index
649 (not InnoDB space id)
650 @param[in] has_auto_inc true if table has auto inc field
651 @param[in] auto_inc_index_name Index name on which auto_inc exists
652 @param[in] auto_inc_col_name col name on which auto_inc exists
653 @param[in,out] read_auto_inc auto inc value read */
654 template <typename Index>
655 static void dd_upgrade_process_index(Index dd_index, dict_index_t *index,
656 dd::Object_id dd_space_id,
657 bool has_auto_inc,
658 const char *auto_inc_index_name,
659 const char *auto_inc_col_name,
660 uint64_t *read_auto_inc) {
661 dd_index->set_tablespace_id(dd_space_id);
662 dd::Properties &p = dd_index->se_private_data();
663
664 p.set(dd_index_key_strings[DD_INDEX_ROOT], index->page);
665 p.set(dd_index_key_strings[DD_INDEX_SPACE_ID], index->space);
666 p.set(dd_index_key_strings[DD_INDEX_ID], index->id);
667 p.set(dd_index_key_strings[DD_TABLE_ID], index->table->id);
668 p.set(dd_index_key_strings[DD_INDEX_TRX_ID], 0);
669
670 if (has_auto_inc) {
671 ut_ad(auto_inc_index_name != nullptr);
672 ut_ad(auto_inc_col_name != nullptr);
673 if (strcmp(index->name(), auto_inc_index_name) == 0) {
674 dberr_t err =
675 row_search_max_autoinc(index, auto_inc_col_name, read_auto_inc);
676 if (err != DB_SUCCESS) {
677 ut_d(ut_error);
678 }
679 }
680 }
681 }
682
683 /** Ensures that the ib_table->dd_space_id is properly initialized.
684 @param[in] thd The THD to identify as during lookup
685 @param[in] ib_table The instance to be initialized
686 @return true iff it succeeded */
687 static bool dd_upgrade_ensure_has_dd_space_id(THD *thd,
688 dict_table_t *ib_table) {
689 if (ib_table->dd_space_id != dd::INVALID_OBJECT_ID) {
690 /* Already initialized, nothing to do. */
691 return true;
692 }
693 if (ib_table->space == SYSTEM_TABLE_SPACE) {
694 ib_table->dd_space_id = dict_sys_t::s_dd_sys_space_id;
695 /* Tables in system tablespace cannot be discarded. */
696 ut_ad(!dict_table_is_discarded(ib_table));
697 return true;
698 }
699 dd::cache::Dictionary_client *dd_client = dd::get_dd_client(thd);
700 dd::cache::Dictionary_client::Auto_releaser releaser(dd_client);
701 dd::Tablespace *dd_space = dd_upgrade_get_tablespace(dd_client, ib_table);
702 if (dd_space == nullptr) {
703 return false;
704 }
705 ib_table->dd_space_id = dd_space->id();
706 return true;
707 }
708
709 /** Migrate partitions to new dictionary
710 @param[in] thd Server thread object
711 @param[in] norm_name partition table name
712 @param[in,out] dd_table Server new DD table object to be filled
713 @param[in] srv_table Server table object
714 @return false on success, true on error */
715 static bool dd_upgrade_partitions(THD *thd, const char *norm_name,
716 dd::Table *dd_table, TABLE *srv_table) {
717 /* Check for auto inc */
718 const char *auto_inc_index_name = nullptr;
719 const char *auto_inc_col_name = nullptr;
720
721 bool has_auto_inc = dd_upgrade_check_for_autoinc(
722 srv_table, auto_inc_index_name, auto_inc_col_name);
723
724 uint64_t max_auto_inc = 0;
725
726 for (dd::Partition *part_obj : *dd_table->leaf_partitions()) {
727 /* Build the partition name. */
728 std::string part_str;
729 dict_name::build_57_partition(part_obj, part_str);
730
731 /* Build the partitioned table name. */
732 std::string table_name;
733 dict_name::build_table("", norm_name, part_str, false, false, table_name);
734
735 dict_table_t *part_table = dict_table_open_on_name(
736 table_name.c_str(), false, true, DICT_ERR_IGNORE_NONE);
737
738 if (part_table == nullptr) {
739 ib::error(ER_IB_MSG_DICT_PARTITION_NOT_FOUND, table_name.c_str());
740 return (true);
741 }
742
743 dict_table_close(part_table, false, false);
744
745 DBUG_EXECUTE_IF("dd_upgrade",
746 ib::info(ER_IB_MSG_254)
747 << "Part table name from server: " << table_name.c_str()
748 << " from InnoDB: " << part_table->name.m_name;);
749
750 if (DICT_TF_HAS_SHARED_SPACE(part_table->flags)) {
751 ib::error(ER_IB_MSG_1282)
752 << "Partitioned table '" << part_table->name.m_name
753 << "' is not allowed to use shared tablespace '"
754 << part_table->tablespace << "'. Please move all "
755 << "partitions to file-per-table tablespaces before upgrade.";
756 return (true);
757 }
758
759 /* Set table id to mysql.columns at runtime */
760 if (dd_part_is_first(part_obj)) {
761 for (auto dd_column : *dd_table->table().columns()) {
762 dd_column->se_private_data().set(dd_index_key_strings[DD_TABLE_ID],
763 part_table->id);
764 }
765 }
766
767 /* Set table id */
768 part_obj->set_se_private_id(part_table->id);
769
770 /* Set DATA_DIRECTORY attribute in se_private_data */
771 if (DICT_TF_HAS_DATA_DIR(part_table->flags)) {
772 ut_ad(dict_table_is_file_per_table(part_table));
773 part_obj->se_private_data().set(
774 dd_table_key_strings[DD_TABLE_DATA_DIRECTORY], true);
775 }
776
777 /* We don't support upgrade from 5.7 with discarded Tablespaces.
778 Upgrade should stop in a dd_upgrade_tablespace function. */
779 ut_ad(!dict_table_is_discarded(part_table));
780
781 if (!dd_upgrade_ensure_has_dd_space_id(thd, part_table)) {
782 ut_d(ut_error);
783 ut_o(return true);
784 }
785
786 dd_set_table_options(part_obj, part_table);
787
788 uint32_t processed_indexes_num = 0;
789 for (dd::Partition_index *part_index : *part_obj->indexes()) {
790 DBUG_EXECUTE_IF("dd_upgrade",
791 ib::info(ER_IB_MSG_255)
792 << "Partition Index " << part_index->name()
793 << " from server for table: " << part_table->name;);
794
795 for (auto index : part_table->indexes) {
796 if (strcmp(part_index->name().c_str(), index->name()) == 0) {
797 uint64_t read_auto_inc = 0;
798 dd_upgrade_process_index(part_index, index, part_table->dd_space_id,
799 has_auto_inc, auto_inc_index_name,
800 auto_inc_col_name, &read_auto_inc);
801 ++processed_indexes_num;
802 if (has_auto_inc) {
803 max_auto_inc = std::max(max_auto_inc, read_auto_inc);
804 }
805 break;
806 }
807 }
808 }
809
810 if (processed_indexes_num != part_obj->indexes()->size()) {
811 ib::error(ER_IB_MSG_256)
812 << "Num of Indexes in InnoDB Partition doesn't match"
813 << " with Indexes from server";
814 ib::error(ER_IB_MSG_257)
815 << "Indexes from InnoDB: " << processed_indexes_num
816 << " Indexes from Server: " << dd_table->indexes()->size();
817 return (true);
818 }
819 }
820
821 /* Set auto increment properties */
822 if (has_auto_inc) {
823 dd_upgrade_set_auto_inc(srv_table, dd_table, max_auto_inc);
824 }
825
826 return (false);
827 }
828
829 /* Set the ROW_FORMAT in dd_table based on InnoDB dictionary table
830 @param[in] ib_table InnoDB table
831 @param[in,out] dd_table Server table object */
832 static void dd_upgrade_set_row_type(dict_table_t *ib_table,
833 dd::Table *dd_table) {
834 if (ib_table) {
835 const uint32_t flags = ib_table->flags;
836
837 switch (dict_tf_get_rec_format(flags)) {
838 case REC_FORMAT_REDUNDANT:
839 dd_table->set_row_format(dd::Table::RF_REDUNDANT);
840 break;
841 case REC_FORMAT_COMPACT:
842 dd_table->set_row_format(dd::Table::RF_COMPACT);
843 break;
844 case REC_FORMAT_COMPRESSED:
845 dd_table->set_row_format(dd::Table::RF_COMPRESSED);
846 break;
847 case REC_FORMAT_DYNAMIC:
848 dd_table->set_row_format(dd::Table::RF_DYNAMIC);
849 break;
850 default:
851 ut_d(ut_error);
852 }
853 }
854 }
855
856 /* Check Innodb table definition and add FTS_DOC_ID column and index to DD table
857 if needed. This is required when all FTS index are dropped but Innodb still
858 retains the FTS_DOC_ID column and FTS_DOC_ID_INDEX.
859 @param[in,out] dd_table Server table object
860 @param[in] ib_table Innodb table
861 @return true if fix FTS_DOC_ID column, false otherwise. */
862 bool dd_upgrade_fix_fts_column(dd::Table *dd_table, dict_table_t *ib_table) {
863 if (DICT_TF2_FLAG_IS_SET(ib_table, DICT_TF2_FTS_HAS_DOC_ID) &&
864 !dict_table_has_fts_index(ib_table)) {
865 /* Add hidden FTS_DOC_ID column in the dd cache as it does not
866 exist there. */
867 dd::Column *col =
868 dd_add_hidden_column(&dd_table->table(), FTS_DOC_ID_COL_NAME,
869 FTS_DOC_ID_LEN, dd::enum_column_types::LONGLONG);
870 dd_set_hidden_unique_index(dd_table->table().add_index(),
871 FTS_DOC_ID_INDEX_NAME, col);
872 return true;
873 }
874 return false;
875 }
876
877 /** Migrate table from InnoDB Dictionary (INNODB SYS_*) tables to new Data
878 Dictionary. Since FTS tables contain table_id in their physical file name
879 and during upgrade we reserve DICT_MAX_DD_TABLES for dictionary tables.
880 So we rename FTS tablespace files
881 @param[in] thd Server thread object
882 @param[in] db_name database name
883 @param[in] table_name table name
884 @param[in,out] dd_table new dictionary table object to be filled
885 @param[in] srv_table server table object
886 @return false on success, true on failure. */
887 bool dd_upgrade_table(THD *thd, const char *db_name, const char *table_name,
888 dd::Table *dd_table, TABLE *srv_table) {
889 char norm_name[FN_REFLEN];
890 dict_table_t *ib_table = nullptr;
891
892 /* 2 * NAME_CHAR_LEN is for dbname and tablename, 5 assumes max bytes
893 for charset, + 2 is for path separator and +1 is for NULL. */
894 char buf[2 * NAME_CHAR_LEN * 5 + 2 + 1];
895 bool truncated;
896
897 build_table_filename(buf, sizeof(buf), db_name, table_name, nullptr, 0,
898 &truncated);
899
900 if (truncated || !normalize_table_name(norm_name, buf)) {
901 /* purecov: begin inspected */
902 ut_d(ut_error);
903 ut_o(return (true));
904 /* purecov: end */
905 }
906
907 bool is_part = dd_table->leaf_partitions()->size() != 0;
908
909 if (is_part) {
910 return (dd_upgrade_partitions(thd, norm_name, dd_table, srv_table));
911 }
912
913 ib_table =
914 dict_table_open_on_name(norm_name, false, true, DICT_ERR_IGNORE_NONE);
915
916 if (ib_table == nullptr) {
917 ib::error(ER_IB_MSG_258)
918 << "Table " << norm_name << " is not found in InnoDB dictionary";
919 return (true);
920 }
921
922 /* We don't support upgrade from 5.7 with discarded Tablespaces.
923 Upgrade should stop in a dd_upgrade_tablespace function. */
924 ut_ad(!dict_table_is_discarded(ib_table));
925
926 /* If all FTS index are dropped but Innodb still retains the
927 FTS_DOC_ID column then add FTS_DOC_ID column and index to DD table */
928 bool added_fts_col = dd_upgrade_fix_fts_column(dd_table, ib_table);
929
930 bool failure =
931 dd_upgrade_match_cols(srv_table, dd_table, ib_table, added_fts_col);
932
933 if (failure) {
934 dict_table_close(ib_table, false, false);
935 return (failure);
936 }
937
938 /* Set table id to mysql.columns as runtime */
939 for (auto dd_column : *dd_table->table().columns()) {
940 dd_column->se_private_data().set(dd_index_key_strings[DD_TABLE_ID],
941 ib_table->id);
942 }
943
944 if (!dd_upgrade_ensure_has_dd_space_id(thd, ib_table)) {
945 dict_table_close(ib_table, false, false);
946 return true;
947 }
948
949 dd_table->set_se_private_id(ib_table->id);
950
951 /* Set DATA_DIRECTORY attribute in se_private_data */
952 if (DICT_TF_HAS_DATA_DIR(ib_table->flags)) {
953 ut_ad(dict_table_is_file_per_table(ib_table));
954 dd_table->se_private_data().set(
955 dd_table_key_strings[DD_TABLE_DATA_DIRECTORY], true);
956 }
957
958 /* Set row_type */
959 dd_upgrade_set_row_type(ib_table, dd_table);
960
961 /* Check for auto inc */
962 const char *auto_inc_index_name = nullptr;
963 const char *auto_inc_col_name = nullptr;
964
965 bool has_auto_inc = dd_upgrade_check_for_autoinc(
966 srv_table, auto_inc_index_name, auto_inc_col_name);
967
968 uint64_t auto_inc = UINT64_MAX;
969
970 dd_set_table_options(dd_table, ib_table);
971
972 /* The number of indexes has to match. */
973 DBUG_EXECUTE_IF("dd_upgrade_strict_mode",
974 ut_ad(dd_table->indexes()->size() ==
975 UT_LIST_GET_LEN(ib_table->indexes)););
976
977 if (UT_LIST_GET_LEN(ib_table->indexes) != dd_table->indexes()->size()) {
978 ib::error(ER_IB_MSG_259) << "Num of Indexes in InnoDB doesn't match"
979 << " with Indexes from server";
980 ib::error(ER_IB_MSG_260)
981 << "Indexes from InnoDB: " << UT_LIST_GET_LEN(ib_table->indexes)
982 << " Indexes from Server: " << dd_table->indexes()->size();
983 dict_table_close(ib_table, false, false);
984 return (true);
985 }
986
987 uint32_t processed_indexes_num = 0;
988 for (dd::Index *dd_index : *dd_table->indexes()) {
989 DBUG_EXECUTE_IF("dd_upgrade",
990 ib::info(ER_IB_MSG_261)
991 << "Index " << dd_index->name()
992 << " from server for table: " << ib_table->name;);
993
994 for (auto index : ib_table->indexes) {
995 if (strcmp(dd_index->name().c_str(), index->name()) == 0) {
996 if (!dd_index->is_hidden()) {
997 failure = dd_upgrade_match_index(srv_table, index);
998 }
999
1000 dd_upgrade_process_index(dd_index, index, ib_table->dd_space_id,
1001 has_auto_inc, auto_inc_index_name,
1002 auto_inc_col_name, &auto_inc);
1003 ++processed_indexes_num;
1004 break;
1005 }
1006 }
1007 }
1008
1009 if (processed_indexes_num != dd_table->indexes()->size()) {
1010 ib::error(ER_IB_MSG_262) << "Num of Indexes in InnoDB doesn't match"
1011 << " with Indexes from server";
1012 ib::error(ER_IB_MSG_263)
1013 << "Indexes from InnoDB: " << processed_indexes_num
1014 << " Indexes from Server: " << dd_table->indexes()->size();
1015 dict_table_close(ib_table, false, false);
1016 return (true);
1017 }
1018
1019 /* Set auto increment properties */
1020 if (has_auto_inc) {
1021 ut_ad(auto_inc != UINT64_MAX);
1022 dd_upgrade_set_auto_inc(srv_table, dd_table, auto_inc);
1023 ib_table->autoinc = auto_inc == 0 ? 0 : auto_inc + 1;
1024 }
1025
1026 if (dict_table_has_fts_index(ib_table) || added_fts_col) {
1027 dberr_t err = fts_upgrade_aux_tables(ib_table);
1028
1029 if (err != DB_SUCCESS) {
1030 dict_table_close(ib_table, false, false);
1031 return (true);
1032 } else {
1033 dict_sys_mutex_enter();
1034 dict_table_prevent_eviction(ib_table);
1035 dict_sys_mutex_exit();
1036
1037 tables_with_fts.push_back(ib_table->name.m_name);
1038 }
1039 }
1040
1041 failure = failure || dd_upgrade_table_fk(ib_table, dd_table);
1042
1043 dict_table_close(ib_table, false, false);
1044 return (failure);
1045 }
1046
1047 /** Tablespace information required to create a
1048 dd::Tablespace object */
1049 typedef struct {
1050 /** InnoDB space id */
1051 space_id_t id;
1052 /** Tablespace name */
1053 const char *name;
1054 /** Tablespace flags */
1055 uint32_t flags;
1056 /** Path of the tablespace file */
1057 const char *path;
1058 } upgrade_space_t;
1059
1060 /** Register InnoDB tablespace to mysql
1061 dictionary table mysql.tablespaces
1062 @param[in] dd_client dictionary client
1063 @param[in] dd_space server tablespace object
1064 @param[in] upgrade_space upgrade tablespace object
1065 @return 0 on success, non-zero on error */
1066 static uint32_t dd_upgrade_register_tablespace(
1067 dd::cache::Dictionary_client *dd_client, dd::Tablespace *dd_space,
1068 upgrade_space_t *upgrade_space) {
1069 dd_space->set_engine(innobase_hton_name);
1070 dd_space->set_name(upgrade_space->name);
1071
1072 dd::Properties &p = dd_space->se_private_data();
1073
1074 p.set(dd_space_key_strings[DD_SPACE_ID],
1075 static_cast<uint32>(upgrade_space->id));
1076 p.set(dd_space_key_strings[DD_SPACE_FLAGS],
1077 static_cast<uint32>(upgrade_space->flags));
1078 p.set(dd_space_key_strings[DD_SPACE_SERVER_VERSION],
1079 DD_SPACE_CURRENT_SRV_VERSION);
1080 p.set(dd_space_key_strings[DD_SPACE_VERSION], DD_SPACE_CURRENT_SPACE_VERSION);
1081
1082 dd_space_states state =
1083 (fsp_is_undo_tablespace(upgrade_space->id) ? DD_SPACE_STATE_ACTIVE
1084 : DD_SPACE_STATE_NORMAL);
1085 p.set(dd_space_key_strings[DD_SPACE_STATE], dd_space_state_values[state]);
1086
1087 dd::Tablespace_file *dd_file = dd_space->add_file();
1088
1089 dd_file->set_filename(upgrade_space->path);
1090
1091 if (!FSP_FLAGS_GET_ENCRYPTION(upgrade_space->flags)) {
1092 /* Update DD Option value, for Unencryption */
1093 dd_space->options().set("encryption", "N");
1094
1095 } else {
1096 /* Update DD Option value, for Encryption */
1097 dd_space->options().set("encryption", "Y");
1098 }
1099
1100 if (dd_client->store(dd_space)) {
1101 /* It would be better to return thd->get_stmt_da()->mysql_errno(),
1102 however, server doesn't fill in the errno during bootstrap. */
1103 return (HA_ERR_GENERIC);
1104 }
1105
1106 return (0);
1107 }
1108
1109 /** Migrate tablespace entries from InnoDB SYS_TABLESPACES to new data
1110 dictionary. FTS Tablespaces are not registered as they are handled differently.
1111 FTS tablespaces have table_id in their name and we increment table_id of each
1112 table by DICT_MAX_DD_TABLES
1113 @param[in,out] thd THD
1114 @return MySQL error code */
1115 int dd_upgrade_tablespace(THD *thd) {
1116 DBUG_TRACE;
1117 btr_pcur_t pcur;
1118 const rec_t *rec;
1119 mem_heap_t *heap;
1120 mtr_t mtr;
1121
1122 if (has_discarded_tablespaces) {
1123 ib::error(ER_IB_CANNOT_UPGRADE_WITH_DISCARDED_TABLESPACES);
1124 return HA_ERR_TABLESPACE_MISSING;
1125 }
1126
1127 heap = mem_heap_create(100, UT_LOCATION_HERE);
1128 dd::cache::Dictionary_client *dd_client = dd::get_dd_client(thd);
1129 dd::cache::Dictionary_client::Auto_releaser releaser(dd_client);
1130 dict_sys_mutex_enter();
1131 mtr_start(&mtr);
1132
1133 /* Pattern for matching the FTS auxiliary tablespace name which starts with
1134 "FTS", followed by the table id. */
1135 std::regex fts_regex("\\S+FTS_[a-f0-9]{16,16}_\\S+");
1136
1137 for (rec = dict_startscan_system(&pcur, &mtr, SYS_TABLESPACES);
1138 rec != nullptr; rec = dict_getnext_system(&pcur, &mtr)) {
1139 const char *err_msg;
1140 space_id_t space;
1141 const char *name;
1142 uint32_t flags;
1143 std::string new_tablespace_name;
1144
1145 /* Extract necessary information from a SYS_TABLESPACES row */
1146 err_msg = dict_process_sys_tablespaces(heap, rec, &space, &name, &flags);
1147
1148 mtr_commit(&mtr);
1149 dict_sys_mutex_exit();
1150 std::string tablespace_name(name);
1151
1152 if (!err_msg && !regex_search(tablespace_name, fts_regex)) {
1153 // Fill the dictionary object here
1154 DBUG_EXECUTE_IF("dd_upgrade",
1155 ib::info(ER_IB_MSG_264)
1156 << "Creating dictionary entry for tablespace: "
1157 << name;);
1158
1159 std::unique_ptr<dd::Tablespace> dd_space(
1160 dd::create_object<dd::Tablespace>());
1161
1162 upgrade_space_t upgrade_space;
1163 upgrade_space.id = space;
1164 upgrade_space.flags = flags;
1165
1166 bool is_file_per_table = !fsp_is_system_or_temp_tablespace(space) &&
1167 !fsp_is_shared_tablespace(flags);
1168
1169 std::string file_per_name;
1170 if (is_file_per_table) {
1171 new_tablespace_name.assign(tablespace_name);
1172 if ((tablespace_name.compare("mysql/innodb_table_stats") == 0) ||
1173 (tablespace_name.find("mysql/innodb_index_stats") == 0)) {
1174 new_tablespace_name.append("_backup57");
1175 }
1176
1177 dict_name::convert_to_space(new_tablespace_name);
1178 upgrade_space.name = new_tablespace_name.c_str();
1179 } else {
1180 upgrade_space.name = name;
1181 }
1182
1183 dict_sys_mutex_enter();
1184 char *filename = dict_get_first_path(space);
1185 dict_sys_mutex_exit();
1186
1187 std::string orig_name(filename);
1188 ut::free(filename);
1189 filename = nullptr;
1190
1191 /* To migrate statistics from 57 satistics tables, we rename the
1192 5.7 statistics tables/tablespaces so that it doesn't conflict
1193 with statistics table names in 8.0 */
1194 if ((tablespace_name.compare("mysql/innodb_table_stats") == 0) ||
1195 (tablespace_name.find("mysql/innodb_index_stats") == 0)) {
1196 orig_name.erase(orig_name.end() - 4, orig_name.end());
1197 orig_name.append("_backup57.ibd");
1198 } else if (is_file_per_table) {
1199 /* Convert 5.7 name to 8.0 for partitioned table path. */
1200 fil_update_partition_name(space, flags, true, new_tablespace_name,
1201 orig_name);
1202
1203 /* Validate whether the tablespace file exists before making
1204 the entry in dd::tablespaces*/
1205 dict_sys_mutex_enter();
1206 fil_space_t *fil_space = fil_space_get(space);
1207 dict_sys_mutex_exit();
1208
1209 /* If the file is not already opened, check for its existence
1210 by opening it in read-only mode. */
1211 if (fil_space == nullptr) {
1212 Datafile df;
1213 df.set_filepath(orig_name.c_str());
1214 if (df.open_read_only(false) != DB_SUCCESS) {
1215 mem_heap_free(heap);
1216 pcur.close();
1217 return HA_ERR_TABLESPACE_MISSING;
1218 }
1219 df.close();
1220 }
1221 }
1222
1223 upgrade_space.path = orig_name.c_str();
1224
1225 if (dd_upgrade_register_tablespace(dd_client, dd_space.get(),
1226 &upgrade_space)) {
1227 mem_heap_free(heap);
1228 return HA_ERR_GENERIC;
1229 }
1230
1231 } else {
1232 if (err_msg != nullptr) {
1233 push_warning_printf(thd, Sql_condition::SL_WARNING,
1234 ER_CANT_FIND_SYSTEM_REC, "%s", err_msg);
1235 }
1236 }
1237
1238 mem_heap_empty(heap);
1239
1240 /* Get the next record */
1241 dict_sys_mutex_enter();
1242 mtr_start(&mtr);
1243 }
1244
1245 mtr_commit(&mtr);
1246 dict_sys_mutex_exit();
1247
1248 /* These are file_per_table tablespaces(created using 5.5 or
1249 earlier). These are not found in SYS_TABLESPACES but discovered
1250 from SYS_TABLES */
1251 for (auto space : missing_spaces) {
1252 std::string tablespace_name(space->name);
1253 /* FTS tablespaces will be registered later */
1254 if (regex_search(tablespace_name, fts_regex)) {
1255 continue;
1256 }
1257 std::string new_tablespace_name;
1258 std::unique_ptr<dd::Tablespace> dd_space(
1259 dd::create_object<dd::Tablespace>());
1260
1261 upgrade_space_t upgrade_space;
1262 upgrade_space.id = space->id;
1263 upgrade_space.flags = space->flags;
1264 dd_space->set_engine(innobase_hton_name);
1265
1266 new_tablespace_name.assign(tablespace_name);
1267 upgrade_space.name = new_tablespace_name.c_str();
1268
1269 fil_node_t *node = &space->files.front();
1270 std::string file_path(node->name);
1271 upgrade_space.path = file_path.c_str();
1272
1273 if (dd_upgrade_register_tablespace(dd_client, dd_space.get(),
1274 &upgrade_space)) {
1275 mem_heap_free(heap);
1276 return HA_ERR_GENERIC;
1277 }
1278 }
1279
1280 mem_heap_free(heap);
1281
1282 return 0;
1283 }
1284
1285 /** Add server and space version number to tablespace while upgrading.
1286 @param[in] space_id space id of tablespace
1287 @param[in] server_version_only leave space version unchanged
1288 @return false on success, true on failure. */
1289 bool upgrade_space_version(const uint32_t space_id, bool server_version_only) {
1290 buf_block_t *block;
1291 page_t *page;
1292 mtr_t mtr;
1293
1294 fil_space_t *space = fil_space_acquire_silent(space_id);
1295
1296 if (space == nullptr) {
1297 return (true);
1298 }
1299
1300 const page_size_t page_size(space->flags);
1301
1302 mtr_start(&mtr);
1303
1304 /* No logging for temporary tablespace. */
1305 if (fsp_is_system_temporary(space_id)) {
1306 mtr.set_log_mode(MTR_LOG_NO_REDO);
1307 }
1308
1309 block = buf_page_get(page_id_t(space_id, 0), page_size, RW_SX_LATCH,
1310 UT_LOCATION_HERE, &mtr);
1311
1312 page = buf_block_get_frame(block);
1313
1314 mlog_write_ulint(page + FIL_PAGE_SRV_VERSION, DD_SPACE_CURRENT_SRV_VERSION,
1315 MLOG_4BYTES, &mtr);
1316 if (!server_version_only) {
1317 mlog_write_ulint(page + FIL_PAGE_SPACE_VERSION,
1318 DD_SPACE_CURRENT_SPACE_VERSION, MLOG_4BYTES, &mtr);
1319 }
1320
1321 mtr_commit(&mtr);
1322 fil_space_release(space);
1323 return (false);
1324 }
1325
1326 /** Add server version number to tablespace while upgrading.
1327 @param[in] tablespace dd::Tablespace
1328 @return false on success, true on failure. */
1329 bool upgrade_space_version(dd::Tablespace *tablespace) {
1330 uint32_t space_id;
1331
1332 if (tablespace->se_private_data().get("id", &space_id)) {
1333 /* error, attribute not found */
1334 ut_d(ut_error);
1335 ut_o(return (true));
1336 }
1337 /* Upgrade both server and space version */
1338 return (upgrade_space_version(space_id, false));
1339 }
1340
1341 int dd_upgrade_logs(THD *) {
1342 int error = 0; /* return zero for success */
1343 DBUG_TRACE;
1344
1345 mtr_t mtr;
1346 mtr.start();
1347 dict_hdr_t *dict_hdr = dict_hdr_get(&mtr);
1348 table_id_t table_id = mach_read_from_8(dict_hdr + DICT_HDR_TABLE_ID);
1349
1350 DBUG_EXECUTE_IF("dd_upgrade",
1351 ib::info(ER_IB_MSG_265)
1352 << "Incrementing table_id from: " << table_id << " to "
1353 << table_id + DICT_MAX_DD_TABLES;);
1354
1355 /* Increase the offset of table_id by DICT_MAX_DD_TABLES */
1356 mlog_write_ull(dict_hdr + DICT_HDR_TABLE_ID, table_id + DICT_MAX_DD_TABLES,
1357 &mtr);
1358 mtr.commit();
1359
1360 log_buffer_flush_to_disk();
1361
1362 return error;
1363 }
1364
1365 /** Drop all InnoDB Dictionary tables (SYS_*). This is done only at
1366 the end of successful upgrade */
1367 static void dd_upgrade_drop_sys_tables() {
1368 ut_ad(srv_is_upgrade_mode);
1369
1370 dict_sys_mutex_enter();
1371
1372 bool found;
1373 const page_size_t page_size(
1374 fil_space_get_page_size(SYSTEM_TABLE_SPACE, &found));
1375 ut_ad(found);
1376 ut_ad(page_size.equals_to(univ_page_size));
1377
1378 for (uint32_t i = 0; i < SYS_NUM_SYSTEM_TABLES; i++) {
1379 dict_table_t *system_table = dict_table_get_low(SYSTEM_TABLE_NAME[i]);
1380 ut_ad(system_table != nullptr);
1381 ut_ad(system_table->space == SYSTEM_TABLE_SPACE);
1382
1383 for (dict_index_t *index = system_table->first_index(); index != nullptr;
1384 index = index->next()) {
1385 ut_ad(index->space == system_table->space);
1386
1387 const page_id_t root(index->space, index->page);
1388
1389 mtr_t mtr;
1390 mtr_start(&mtr);
1391
1392 btr_free_if_exists(root, page_size, index->id, &mtr);
1393
1394 mtr_commit(&mtr);
1395 }
1396 dict_table_remove_from_cache(system_table);
1397 }
1398
1399 dict_sys->sys_tables = nullptr;
1400 dict_sys->sys_columns = nullptr;
1401 dict_sys->sys_indexes = nullptr;
1402 dict_sys->sys_fields = nullptr;
1403 dict_sys->sys_virtual = nullptr;
1404
1405 dict_sys_mutex_exit();
1406 }
1407
1408 /** Stat backup tables(innodb_*_stats_backup57) are created by server before
1409 upgrade and dropped after upgrade is successful. Innodb tablespaces for
1410 these tables still exists because InnoDB post DDL hook is skipped on
1411 bootstrap thread. This is a work around to cleanup the Innodb tablespaces till
1412 the time server could enable post DDL hook while dropping these tables. */
1413 static void dd_upgrade_drop_57_backup_spaces() {
1414 ut_ad(srv_is_upgrade_mode);
1415
1416 static std::array<const char *, 2> backup_space_names = {
1417 "mysql/innodb_table_stats_backup57", "mysql/innodb_index_stats_backup57"};
1418
1419 for (auto space_name : backup_space_names) {
1420 auto space_id = fil_space_get_id_by_name(space_name);
1421
1422 /* Skip, if space is already deleted. */
1423 if (space_id == SPACE_UNKNOWN) {
1424 continue;
1425 }
1426
1427 auto err = fil_delete_tablespace(space_id, BUF_REMOVE_NONE);
1428
1429 if (err != DB_SUCCESS) {
1430 ib::warn(ER_IB_MSG_57_STAT_SPACE_DELETE_FAIL, space_name);
1431 }
1432 }
1433 }
1434
1435 /** Rename back the FTS AUX tablespace names from 8.0 format to 5.7
1436 format on upgrade failure, else mark FTS aux tables evictable
1437 @param[in] failed_upgrade true on upgrade failure, else
1438 false */
1439 static void dd_upgrade_fts_rename_cleanup(bool failed_upgrade) {
1440 for (std::string &name : tables_with_fts) {
1441 dict_table_t *ib_table = dict_table_open_on_name(name.c_str(), false, true,
1442 DICT_ERR_IGNORE_NONE);
1443 ut_ad(ib_table != nullptr);
1444 if (ib_table != nullptr) {
1445 fts_upgrade_rename(ib_table, failed_upgrade);
1446
1447 dict_sys_mutex_enter();
1448
1449 /* Do not mark the table ready for eviction if there is
1450 a foreign key relationship on this table */
1451 if (ib_table->foreign_set.empty() && ib_table->referenced_set.empty()) {
1452 dict_table_allow_eviction(ib_table);
1453 }
1454 dict_table_close(ib_table, true, false);
1455 dict_sys_mutex_exit();
1456 }
1457 }
1458 }
1459
1460 int dd_upgrade_finish(THD *, bool failed_upgrade) {
1461 DBUG_TRACE;
1462
1463 dd_upgrade_fts_rename_cleanup(failed_upgrade);
1464
1465 if (failed_upgrade) {
1466 srv_downgrade_logs = true;
1467 srv_downgrade_partition_files = true;
1468 } else {
1469 /* Delete the old undo tablespaces and the references to them
1470 in the TRX_SYS page. */
1471 srv_undo_tablespaces_upgrade();
1472
1473 /* Drop InnoDB Dictionary tables (SYS_*) */
1474 dd_upgrade_drop_sys_tables();
1475
1476 /* Flush entire buffer pool. */
1477 buf_flush_sync_all_buf_pools();
1478
1479 /* Checkpoint to discard redo logs for earlier changes. */
1480 log_make_latest_checkpoint();
1481
1482 /* Drop the backup stats tablespaces */
1483 dd_upgrade_drop_57_backup_spaces();
1484 }
1485
1486 tables_with_fts.clear();
1487 tables_with_fts.shrink_to_fit();
1488 srv_is_upgrade_mode = false;
1489
1490 return 0;
1491 }