Skip to content

Commit f8fdaf6

Browse files
Cleanup system schema checks
- Be more consistent about how we look for them
1 parent f930393 commit f8fdaf6

8 files changed

Lines changed: 22 additions & 15 deletions

File tree

sql/dd/impl/dictionary_impl.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include "sql/transaction.h" // trans_commit()
7878
#include "storage/perfschema/pfs_dd_version.h" // PFS_DD_VERSION
7979
#include "villagesql/schema/schema_manager.h"
80+
#include "villagesql/schema/util.h"
8081

8182
extern Cost_constant_cache *cost_constant_cache; // defined in
8283
// opt_costconstantcache.cc
@@ -331,8 +332,7 @@ bool Dictionary_impl::is_dd_table_access_allowed(bool is_dd_internal_thread,
331332
size_t schema_length,
332333
const char *table_name) const {
333334
// Lockdown access to villagesql.* tables.
334-
if (strcmp(schema_name, villagesql::SchemaManager::VILLAGESQL_SCHEMA_NAME) ==
335-
0) {
335+
if (villagesql::is_villagesql_schema(schema_name)) {
336336
// Allow access for internal threads or if running a debug build and the
337337
// appropriate session variable is set.
338338
return is_dd_internal_thread ||

sql/table.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
#include "strxnmov.h"
133133
#include "template_utils.h" // down_cast
134134
#include "thr_mutex.h"
135-
#include "villagesql/schema/schema_manager.h"
135+
#include "villagesql/schema/util.h"
136136

137137
/* INFORMATION_SCHEMA name */
138138
LEX_CSTRING INFORMATION_SCHEMA_NAME = {STRING_WITH_LEN("information_schema")};
@@ -360,9 +360,7 @@ TABLE_CATEGORY get_table_category(const LEX_CSTRING &db,
360360
}
361361

362362
// Check for VillageSQL system tables
363-
if (my_strcasecmp(system_charset_info,
364-
villagesql::SchemaManager::VILLAGESQL_SCHEMA_NAME,
365-
db.str) == 0) {
363+
if (villagesql::is_villagesql_schema(db.str)) {
366364
return TABLE_CATEGORY_VILLAGESQL_SYSTEM;
367365
}
368366

storage/innobase/handler/ha_innodb.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4016,9 +4016,8 @@ static void innobase_dict_cache_reset_tables_and_tablespaces() {
40164016

40174017
/* TODO: Remove follow if we have better way to identify
40184018
DD "system table" */
4019-
if (db_str.compare("mysql") == 0 || table->is_dd_table ||
4020-
table->is_corrupted() ||
4021-
villagesql::is_villagesql_system_table(db_str.c_str())) {
4019+
if (villagesql::is_system_schema(db_str.c_str()) || table->is_dd_table ||
4020+
table->is_corrupted()) {
40224021
continue;
40234022
}
40244023

villagesql/schema/ADDING_SYSTEM_TABLES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ constexpr const char *YOUR_TABLE_NAME = "your_table";
189189
- **Wrong initializer order** - Must match member declaration order
190190
- **Missing update_key()** - Must call in read_from_table() after deserializing fields
191191
- **Using public key fields** - Keep key components private with accessors to prevent stale keys
192-
- **Hardcoded table names** - Use `SchemaManager::VILLAGESQL_SCHEMA_NAME` and `SchemaManager::YOUR_TABLE_NAME` instead of string literals in Table_ref and error messages
192+
- **Hardcoded table names** - Use `SchemaManager::VILLAGESQL_SCHEMA_NAME` and `SchemaManager::YOUR_TABLE_NAME` instead of string literals in Table_ref and error messages. To check for system tables, use villagesql::is_system_schema() or villagesql::is_villagesql_schema().
193193

194194
## Usage in Code
195195

villagesql/schema/util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace villagesql {
2424
// that use the constants from SchemaManager to make sure they use this.
2525
bool is_villagesql_system_table(const TABLE *table) {
2626
if (!table || !table->s) return false;
27-
return is_villagesql_system_table(table->s->db.str);
27+
return is_villagesql_schema(table->s->db.str);
2828
}
2929

3030
} // namespace villagesql

villagesql/schema/util.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ namespace villagesql {
2626

2727
// Check if a database name is the 'villagesql' schema. db_name must not be
2828
// nullptr.
29-
inline bool is_villagesql_system_table(const char *db_name) {
29+
inline bool is_villagesql_schema(const char *db_name) {
3030
return (my_strcasecmp(system_charset_info,
3131
SchemaManager::VILLAGESQL_SCHEMA_NAME, db_name) == 0);
3232
}
3333

34+
// Like the above but also considered true if this is in a mysql system schema.
35+
inline bool is_system_schema(const char *db_name) {
36+
return (my_strcasecmp(system_charset_info, "mysql", db_name) == 0 ||
37+
my_strcasecmp(system_charset_info, "sys", db_name) == 0 ||
38+
is_villagesql_schema(db_name));
39+
}
40+
3441
// Check if a TABLE is in the 'villagesql' schema.
3542
// All tables in the villagesql schema are system tables that can be accessed
3643
// with no_read_locking from INFORMATION_SCHEMA views.

villagesql/sql/metadata_modifier.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "villagesql/schema/descriptor/type_descriptor.h"
3535
#include "villagesql/schema/schema_manager.h"
3636
#include "villagesql/schema/systable/extensions.h"
37+
#include "villagesql/schema/util.h"
3738
#include "villagesql/schema/victionary_client.h"
3839
#include "villagesql/sql/custom_vdf.h"
3940
#include "villagesql/sql/func_lookup.h"
@@ -720,7 +721,9 @@ bool Metadata_modifier::store(THD *thd) {
720721
// just need to find it in query_tables and lock it.
721722
Table_ref *columns_table = nullptr;
722723
for (Table_ref *tl = thd->lex->query_tables; tl; tl = tl->next_global) {
723-
if (strcmp(tl->db, SchemaManager::VILLAGESQL_SCHEMA_NAME) == 0 &&
724+
// TODO(villagesql-beta): table names should be using system charset
725+
// comparison
726+
if (villagesql::is_villagesql_schema(tl->db) &&
724727
strcmp(tl->table_name, SchemaManager::COLUMNS_TABLE_NAME) == 0) {
725728
columns_table = tl;
726729
break;

villagesql/types/util.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
#include "villagesql/include/error.h"
4646
#include "villagesql/schema/descriptor/type_context.h"
4747
#include "villagesql/schema/descriptor/type_descriptor.h"
48-
#include "villagesql/schema/schema_manager.h"
4948
#include "villagesql/schema/systable/custom_columns.h"
49+
#include "villagesql/schema/util.h"
5050
#include "villagesql/schema/victionary_client.h"
5151

5252
namespace villagesql {
@@ -70,7 +70,7 @@ bool MaybeInjectCustomType(THD *thd, TABLE_SHARE &share, Field *field) {
7070
std::string db_name = std::string(share.db.str, share.db.length);
7171

7272
// Skip special databases
73-
if (db_name == "mysql" || db_name == SchemaManager::VILLAGESQL_SCHEMA_NAME) {
73+
if (::villagesql::is_system_schema(db_name.c_str())) {
7474
return false;
7575
}
7676

0 commit comments

Comments
 (0)