File: schema.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (223 lines) | stat: -rw-r--r-- 7,559 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Copyright (c) 2017, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include "sql/dd/upgrade_57/schema.h"

#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "lex_string.h"
#include "m_ctype.h"
#include "my_dir.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_loglevel.h"
#include "my_sys.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/psi/mysql_file.h"  // mysql_file_open
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql/dd/cache/dictionary_client.h"  // Auto_releaser
#include "sql/dd/dd_schema.h"                // Schema_MDL_locker
#include "sql/dd/impl/utils.h"               // is_string_in_lowercase
#include "sql/log.h"                         // LogErr()
#include "sql/mysqld.h"                      // key_file_dbopt
#include "sql/sql_class.h"                   // THD
#include "sql/sql_table.h"                   // build_tablename
#include "sql/system_variables.h"
#include "sql/thd_raii.h"
#include "sql/transaction.h"  // trans_commit
#include "sql_string.h"

namespace dd {
namespace upgrade_57 {

/**
  Returns the collation id for the database specified.

  @param[in]  thd                        Thread handle.
  @param[in]  db_opt_path                Path for database.
  @param[out] schema_charset             Character set of database.

  @retval false  ON SUCCESS
  @retval true   ON FAILURE

*/
static bool load_db_schema_collation(THD *thd, const LEX_STRING *db_opt_path,
                                     const CHARSET_INFO **schema_charset) {
  IO_CACHE cache;
  File file;
  char buf[256];
  uint nbytes;

  if ((file = mysql_file_open(key_file_dbopt, db_opt_path->str, O_RDONLY,
                              MYF(0))) < 0) {
    LogErr(WARNING_LEVEL, ER_CANT_OPEN_DB_OPT_USING_DEFAULT_CHARSET,
           db_opt_path->str);
    return false;
  }

  if (init_io_cache(&cache, file, IO_SIZE, READ_CACHE, 0, false, MYF(0))) {
    LogErr(ERROR_LEVEL, ER_CANT_CREATE_CACHE_FOR_DB_OPT, db_opt_path->str);
    goto err;
  }

  while ((int)(nbytes = my_b_gets(&cache, (char *)buf, sizeof(buf))) > 0) {
    char *pos = buf + nbytes - 1;

    /* Remove end space and control characters */
    while (pos > buf && !my_isgraph(&my_charset_latin1, pos[-1])) pos--;

    *pos = 0;
    if ((pos = strrchr(buf, '='))) {
      if (!strncmp(buf, "default-character-set", (pos - buf))) {
        /*
           Try character set name, and if it fails try collation name, probably
           it's an old 4.1.0 db.opt file, which didn't have separate
           default-character-set and default-collation commands.
        */
        if (!(*schema_charset =
                  get_charset_by_csname(pos + 1, MY_CS_PRIMARY, MYF(0))) &&
            !(*schema_charset = get_charset_by_name(pos + 1, MYF(0)))) {
          LogErr(WARNING_LEVEL, ER_CANT_IDENTIFY_CHARSET_USING_DEFAULT,
                 db_opt_path->str);

          *schema_charset = thd->variables.collation_server;
        }
      } else if (!strncmp(buf, "default-collation", (pos - buf))) {
        if (!(*schema_charset = get_charset_by_name(pos + 1, MYF(0)))) {
          LogErr(WARNING_LEVEL, ER_CANT_IDENTIFY_CHARSET_USING_DEFAULT,
                 db_opt_path->str);
          *schema_charset = thd->variables.collation_server;
        }
      }
    }
  }

  end_io_cache(&cache);
  mysql_file_close(file, MYF(0));
  return false;

err:
  mysql_file_close(file, MYF(0));
  return true;
}

/**
   Update the Schemata:DD for every database present
   in the data directory.
*/

bool migrate_schema_to_dd(THD *thd, const char *dbname) {
  char dbopt_path_buff[FN_REFLEN + 1];
  char schema_name[NAME_LEN + 1];
  LEX_STRING dbopt_file_name;
  const CHARSET_INFO *schema_charset = thd->variables.collation_server;
  dd::cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());

  // Construct the schema name from its canonical format.
  bool has_invalid_name = false;
  filename_to_tablename(dbname, schema_name, sizeof(schema_name), false,
                        &has_invalid_name);

  // If the filename of the database is invalid, stop the upgrade.
  if (has_invalid_name) return true;

  dbopt_file_name.str = dbopt_path_buff;
  dbopt_file_name.length = build_table_filename(dbopt_path_buff, FN_REFLEN - 1,
                                                schema_name, "db", ".opt", 0);

  if (!my_access(dbopt_file_name.str, F_OK)) {
    // Get the collation id for the database.
    if (load_db_schema_collation(thd, &dbopt_file_name, &schema_charset))
      return true;
  } else {
    LogErr(WARNING_LEVEL, ER_DB_OPT_NOT_FOUND_USING_DEFAULT_CHARSET, dbname);
  }

  // See comments regarding l_c_t_n in migrate_all_frm_to_dd().
  if (lower_case_table_names == 0)
    // Supported only for case sensitive file systems.
    assert(!lower_case_file_system);
  else if (lower_case_table_names == 1) {
    // Supported for any file system. All names must be in lower case.
    if (!is_string_in_lowercase(schema_name, system_charset_info)) {
      LogErr(ERROR_LEVEL, ER_SCHEMA_NAME_IN_UPPER_CASE_NOT_ALLOWED,
             schema_name);
      return true;
    }
  } else if (lower_case_table_names == 2)
    // Supported only for case insensitive file systems.
    assert(lower_case_file_system);
  else
    assert(false);

  // Disable autocommit option
  Disable_autocommit_guard autocommit_guard(thd);

  if (dd::create_schema(thd, schema_name, schema_charset, false)) {
    trans_rollback_stmt(thd);
    // Full rollback in case we have THD::transaction_rollback_request.
    trans_rollback(thd);
    return true;
  }

  if (trans_commit_stmt(thd) || trans_commit(thd)) return true;

  return false;
}

/**
  Scans datadir for databases and lists all the database names.
*/

bool find_schema_from_datadir(std::vector<String_type> *db_name) {
  MY_DIR *a;
  uint i;
  FILEINFO *file;

  if (!(a = my_dir(mysql_real_data_home, MYF(MY_WANT_STAT)))) return true;

  for (i = 0; i < (uint)a->number_off_files; i++) {
    file = a->dir_entry + i;

    if (file->name[0] == '.') continue;

    if (MY_S_ISDIR(a->dir_entry[i].mystat->st_mode) &&
        strcmp(a->dir_entry[i].name, "#innodb_temp") != 0 &&
        strcmp(a->dir_entry[i].name, "#innodb_redo") != 0) {
      db_name->push_back(a->dir_entry[i].name);
      continue;
    }
  }

  my_dirend(a);
  return false;
}

}  // namespace upgrade_57
}  // namespace dd