github.com/matrixorigin/matrixone@v1.2.0/pkg/util/sysview/sysview.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package sysview 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 "github.com/matrixorigin/matrixone/pkg/common/moerr" 23 "github.com/matrixorigin/matrixone/pkg/util/executor" 24 25 "github.com/matrixorigin/matrixone/pkg/logutil" 26 ) 27 28 const ( 29 MysqlDBConst = "mysql" 30 InformationDBConst = "information_schema" 31 CreateDatabaseFormat = "create database if not exists " 32 ) 33 34 var ( 35 InitMysqlSysTables = []string{ 36 MysqlUserDDL, 37 MysqlDbDDL, 38 MysqlProcsPrivDDL, 39 MysqlColumnsPrivDDL, 40 MysqlTablesPrivDDL, 41 MysqlRoleEdgesDDL, 42 } 43 InitInformationSchemaSysTables = []string{ 44 InformationSchemaKeyColumnUsageDDL, 45 InformationSchemaColumnsDDL, 46 InformationSchemaProfilingDDL, 47 InformationSchemaProcesslistDDL, 48 InformationSchemaUserPrivilegesDDL, 49 InformationSchemaSchemataDDL, 50 InformationSchemaCharacterSetsDDL, 51 InformationSchemaTriggersDDL, 52 InformationSchemaTablesDDL, 53 InformationSchemaPartitionsDDL, 54 InformationSchemaViewsDDL, 55 InformationSchemaStatisticsDDL, 56 InformationSchemaReferentialConstraintsDDL, 57 InformationSchemaEnginesDDL, 58 InformationSchemaRoutinesDDL, 59 InformationSchemaParametersDDL, 60 InformationSchemaKeywordsDDL, 61 InformationSchemaSchemaPrivilegesDDL, 62 InformationSchemaTablePrivilegesDDL, 63 InformationSchemaColumnPrivilegesDDL, 64 InformationSchemaCollationsDDL, 65 InformationSchemaTableConstraintsDDL, 66 InformationSchemaEventsDDL, 67 informationSchemaKeywordsData, 68 } 69 ) 70 71 func InitSchema(ctx context.Context, txn executor.TxnExecutor) error { 72 if err := initMysqlTables(ctx, txn); err != nil { 73 return err 74 } 75 if err := initInformationSchemaTables(ctx, txn); err != nil { 76 return err 77 } 78 return nil 79 } 80 81 // Initialize system tables under the `mysql` database for compatibility with MySQL 82 func initMysqlTables(ctx context.Context, txn executor.TxnExecutor) error { 83 _, err := txn.Exec(CreateDatabaseFormat+MysqlDBConst, executor.StatementOption{}) 84 if err != nil { 85 return err 86 } 87 88 txn.Use(MysqlDBConst) 89 90 var timeCost time.Duration 91 defer func() { 92 logutil.Debugf("[Mysql] init mysql tables: create cost %d ms", timeCost.Milliseconds()) 93 }() 94 95 begin := time.Now() 96 for _, sql := range InitMysqlSysTables { 97 if _, err = txn.Exec(sql, executor.StatementOption{}); err != nil { 98 return moerr.NewInternalError(ctx, "[Mysql] init mysql tables error: %v, sql: %s", err, sql) 99 } 100 } 101 timeCost = time.Since(begin) 102 return nil 103 } 104 105 // Initialize the system view under the `information_schema` database for compatibility with MySQL 106 func initInformationSchemaTables(ctx context.Context, txn executor.TxnExecutor) error { 107 _, err := txn.Exec(CreateDatabaseFormat+InformationDBConst, executor.StatementOption{}) 108 if err != nil { 109 return err 110 } 111 112 txn.Use(InformationDBConst) 113 114 var timeCost time.Duration 115 defer func() { 116 logutil.Debugf("[information_schema] init information_schema tables: create cost %d ms", timeCost.Milliseconds()) 117 }() 118 119 begin := time.Now() 120 for _, sql := range InitInformationSchemaSysTables { 121 if _, err = txn.Exec(sql, executor.StatementOption{}); err != nil { 122 return moerr.NewInternalError(ctx, fmt.Sprintf("[information_schema] init information_schema tables error: %v, sql: %s", err, sql)) 123 } 124 } 125 timeCost = time.Since(begin) 126 return nil 127 }