Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: get design schema supports mysql partition and function (#11488)

h3n4l422766680cde
diff --git a/backend/plugin/parser/mysql/equal.go b/backend/plugin/parser/mysql/equal.go
index a9fc0502b2..32af338fdd 100644
--- a/backend/plugin/parser/mysql/equal.go
+++ b/backend/plugin/parser/mysql/equal.go
@@ -44,8 +44,8 @@ func (l *isViewTailEqualViewStmtListener) EnterCreateView(ctx *parser.CreateView
}
}
-// IsViewTailEqualViewStmt returns true if the view tail is equal to the view statement.
-func IsViewTailEqualViewStmt(a parser.IViewTailContext, b string) (bool, error) {
+// IsViewTailEqualCreateViewStmt returns true if the view tail is equal to the view statement.
+func IsViewTailEqualCreateViewStmt(a parser.IViewTailContext, b string) (bool, error) {
parseResults, err := ParseMySQL(b)
if err != nil {
return false, err
@@ -65,3 +65,62 @@ func IsViewTailEqualViewStmt(a parser.IViewTailContext, b string) (bool, error)
return listener.equal, nil
}
+
+// IsCompoundStmtEqual returns true if the two compound statements are equal.
+func IsCompoundStmtEqual(a, b parser.ICompoundStatementContext) bool {
+ return a.GetText() == b.GetText()
+}
+
+type isCompoundEqualFuncStmtListener struct {
+ *parser.BaseMySQLParserListener
+
+ target parser.ICompoundStatementContext
+ equal bool
+ err error
+}
+
+func (l *isCompoundEqualFuncStmtListener) EnterCreateFunction(ctx *parser.CreateFunctionContext) {
+ if l.err != nil {
+ return
+ }
+ p := ctx.GetParent()
+ if _, ok := p.(*parser.CreateStatementContext); !ok {
+ l.err = errors.New("Expecting CreateFunctionStatementContext as parent")
+ return
+ }
+ pp := p.GetParent()
+ if _, ok := pp.(*parser.SimpleStatementContext); !ok {
+ l.err = errors.New("Expecting SimpleStatementContext as parent")
+ return
+ }
+ ppp := pp.GetParent()
+ if _, ok := ppp.(*parser.QueryContext); !ok {
+ l.err = errors.New("Expecting QueryContext as parent")
+ return
+ }
+
+ if IsCompoundStmtEqual(l.target, ctx.CompoundStatement()) {
+ l.equal = true
+ }
+}
+
+// IsCompoundEqualCreateFuncStmt returns true if the compound statement is equal to the create function statement.
+func IsCompoundEqualCreateFuncStmt(a parser.ICompoundStatementContext, b string) (bool, error) {
+ parseResults, err := ParseMySQL(b)
+ if err != nil {
+ return false, err
+ }
+ if len(parseResults) != 1 {
+ return false, errors.Errorf("Expecting one statement, but got %d", len(parseResults))
+ }
+
+ stmt := parseResults[0]
+ listener := &isCompoundEqualFuncStmtListener{
+ target: a,
+ }
+ antlr.ParseTreeWalkerDefault.Walk(listener, stmt.Tree)
+ if listener.err != nil {
+ return false, listener.err
+ }
+ return listener.equal, nil
+}
diff --git a/backend/plugin/parser/mysql/mysql.go b/backend/plugin/parser/mysql/mysql.go
index 3ff3e8ee7a..5f84dc1e41 100644
--- a/backend/plugin/parser/mysql/mysql.go
+++ b/backend/plugin/parser/mysql/mysql.go
@@ -55,7 +55,7 @@ func DealWithDelimiter(statement string, options ...tokenizer.Option) (string, e
continue
}
// TODO(rebelice): after deal with delimiter, we may cannot get the right line number, fix it.
- if delimiter != ";" {
+ if delimiter != ";" && !sql.Empty {
result = append(result, fmt.Sprintf("%s;", strings.TrimSuffix(sql.Text, delimiter)))
} else {
result = append(result, sql.Text)
@@ -75,6 +75,7 @@ func DealWithDelimiter(statement string, options ...tokenizer.Option) (string, e
// SELECT 1;
// SELECT 2;
// -- DELIMITER ;.
+// If the current delimiter is `;`, and meet `-- DELIMITER ;`, we would skip this delimiter comment.
func RestoreDelimiter(statement string) (string, error) {
delimiterRegexp := regexp.MustCompile(`(?i)^-- DELIMITER\s+(?P<DELIMITER>[^\s\\]+)\s*`)
lines := strings.Split(statement, "\n")
@@ -126,8 +127,10 @@ func RestoreDelimiter(statement string) (string, error) {
}
}
- if _, err := result.WriteString(fmt.Sprintf("DELIMITER %s\n", matchList[1])); err != nil {
- return "", errors.Wrapf(err, "failed to write string %q into builder", line)
+ if matchList[1] != previousDelimiter {
+ if _, err := result.WriteString(fmt.Sprintf("DELIMITER %s\n", matchList[1])); err != nil {
+ return "", errors.Wrapf(err, "failed to write string %q into builder", line)
+ }
}
previousDelimiter = matchList[1]
}
diff --git a/backend/plugin/parser/mysql/mysql_test.go b/backend/plugin/parser/mysql/mysql_test.go
index 5e40486862..705883e0e7 100644
--- a/backend/plugin/parser/mysql/mysql_test.go
+++ b/backend/plugin/parser/mysql/mysql_test.go
@@ -144,6 +144,51 @@ func TestMySQLParser(t *testing.T) {
}
}
+// func TestDealWithDelimiter_SplitCommentBeforeDelimiter(t *testing.T) {
+// tests := []struct {
+// input string
+// want string
+// }{
+// {
+// input: `
+// DELIMITER ;;
+// CREATE FUNCTION hello(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4
+// DETERMINISTIC
+// RETURN CONCAT('Hello, ',s,'!') ;;
+// DELIMITER ;
+// --
+// -- Function structure for hello2
+// --
+// DELIMITER ;;
+// CREATE FUNCTION hello2(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4
+// DETERMINISTIC
+// RETURN CONCAT('Hello, ',s,'!') ;;
+// DELIMITER ;
+// `,
+// want: `-- DELIMITER ;;
+// CREATE FUNCTION hello(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4
+// DETERMINISTIC
+// RETURN CONCAT('Hello, ',s,'!') ;
+// -- DELIMITER ;
+// --
+// -- Function structure for hello2
+// --
+// -- DELIMITER ;;
+// CREATE FUNCTION hello2(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4
+// DETERMINISTIC
+// RETURN CONCAT('Hello, ',s,'!') ;
+// -- DELIMITER ;`,
+// },
+// }
+
+// a := require.New(t)
+// for i, test := range tests {
+// got, err := DealWithDelimiter(test.input, tokenizer.SplitCommentBeforeDelimiter())
+// a.NoError(err, i)
+// a.Equal(test.want, got, i)
+// }
+// }
+
func TestRestoreDelimiter(t *testing.T) {
testCases := []struct {
input string
@@ -363,6 +408,25 @@ CREATE FUNCTION hello3(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4
RETURN CONCAT('Hello, ',s,'!') ??
DELIMITER ;
DROP FUNCTION hello3;
+`,
+ },
+
+ // Ignore redundant DELIMITER statements.
+ {
+ input: `
+-- DELIMITER ;;
+CREATE FUNCTION hello(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4
+ DETERMINISTIC
+RETURN CONCAT('Hello, ',s,'!') ;
+-- DELIMITER ;
+-- DELIMITER ;
+`,
+ want: `
+DELIMITER ;;
+CREATE FUNCTION hello(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4
+ DETERMINISTIC
+RETURN CONCAT('Hello, ',s,'!') ;;
+DELIMITER ;
`,
},
}
diff --git a/backend/plugin/parser/tokenizer/testdata/test_split_tidb_multi_sql.yaml b/backend/plugin/parser/tokenizer/testdata/test_split_tidb_multi_sql.yaml
index 8f88faf2bd..1b56ada27e 100644
--- a/backend/plugin/parser/tokenizer/testdata/test_split_tidb_multi_sql.yaml
+++ b/backend/plugin/parser/tokenizer/testdata/test_split_tidb_multi_sql.yaml
@@ -1,5 +1,24 @@
+- description: Split comment and delimiter
+ statement: |-
+ -- comment
+ DELIMITER ;
+ expected:
+ - text: -- comment
+ baseline: 0
+ firststatementline: 0
+ firststatementcolumn: 0
+ lastline: 1
+ lastcolumn: 0
+ empty: true
+ - text: DELIMITER ;
+ baseline: 0
+ firststatementline: 0
+ firststatementcolumn: 0
+ lastline: 2
+ lastcolumn: 0
+ empty: false
- description: empty statement
- statement: " "
+ statement: " "
expected: []
- description: multi-newline
statement: |-
@@ -36,7 +55,7 @@
empty: false
- text: |4+
-
+
baseline: 3
firststatementline: 0
firststatementcolumn: 0
diff --git a/backend/plugin/parser/tokenizer/tokenizer.go b/backend/plugin/parser/tokenizer/tokenizer.go
index 38307edc3c..b9e2038d6a 100644
--- a/backend/plugin/parser/tokenizer/tokenizer.go
+++ b/backend/plugin/parser/tokenizer/tokenizer.go
@@ -59,6 +59,12 @@ func KeepEmptyBlocks() Option {
}
}
+func SplitCommentBeforeDelimiter() Option {
+ return func(t *Tokenizer) {
+ t.splitCommentBeforeDelimiter = true
+ }
+}
+
type Tokenizer struct {
buffer []rune
cursor uint
@@ -74,6 +80,8 @@ type Tokenizer struct {
// Options.
// keepEmptyBlocks is used to keep empty lines between the blocks.
keepEmptyBlocks bool
+ // splitCommentBeforeDelimiter is used to split comment before delimiter.
+ splitCommentBeforeDelimiter bool
}
// NewTokenizer creates a new tokenizer.
@@ -483,6 +491,23 @@ func (t *Tokenizer) SplitTiDBMultiSQL() ([]base.SingleSQL, error) {
t.emptyStatement = true
// deal with the DELIMITER statement, see https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html
case t.equalWordCaseInsensitive(delimiterRuneList):
+ if t.splitCommentBeforeDelimiter {
+ if v := t.getString(startPos, t.pos()-startPos); v != "" {
+ line := t.line
+ // Any better way to handle comment before delimiter, and we want to get two
+ // blocks of SQL, one is the comment, the other is the delimiter.
+ if strings.HasSuffix(v, "\n") {
+ v = v[:len(v)-1]
+ line--
+ }
+ res = append(res, base.SingleSQL{
+ Text: v,
+ LastLine: line,
+ Empty: true,
+ })
+ }
+ startPos = t.pos()
+ }
t.skip(uint(len(delimiterRuneList)))
t.skipBlank()
delimiterStart := t.pos()
diff --git a/backend/plugin/parser/tokenizer/tokenizer_test.go b/backend/plugin/parser/tokenizer/tokenizer_test.go
index 7a78f037f3..18bd88d81e 100644
--- a/backend/plugin/parser/tokenizer/tokenizer_test.go
+++ b/backend/plugin/parser/tokenizer/tokenizer_test.go
@@ -37,7 +37,7 @@ func TestSplitTiDBMultiSQL(t *testing.T) {
a.NoError(err)
for idx, tc := range testCases {
- tokenizer := NewTokenizer(tc.Statement, KeepEmptyBlocks())
+ tokenizer := NewTokenizer(tc.Statement, KeepEmptyBlocks(), SplitCommentBeforeDelimiter())
list, err := tokenizer.SplitTiDBMultiSQL()
a.NoError(err)
diff --git a/backend/plugin/schema/mysql/get_design_schema.go b/backend/plugin/schema/mysql/get_design_schema.go
index 4b04f0a585..edba93e7e4 100644
--- a/backend/plugin/schema/mysql/get_design_schema.go
+++ b/backend/plugin/schema/mysql/get_design_schema.go
@@ -24,7 +24,7 @@ func init() {
func GetDesignSchema(_, baselineSchema string, to *storepb.DatabaseSchemaMetadata) (string, error) {
toState := convertToDatabaseState(to)
- list, err := mysqlparser.ParseMySQL(baselineSchema, tokenizer.KeepEmptyBlocks())
+ list, err := mysqlparser.ParseMySQL(baselineSchema, tokenizer.KeepEmptyBlocks(), tokenizer.SplitCommentBeforeDelimiter())
if err != nil {
return "", err
}
@@ -47,21 +47,77 @@ func GetDesignSchema(_, baselineSchema string, to *storepb.DatabaseSchemaMetadat
listener.finalViewTail = finalViewTailGetter.finalViewTails
listener.finalViewStatementIndex = finalViewTailGetter.finalViewStatementIndex
+ groups, err := groupStatement(list)
+ if err != nil {
+ return "", err
+ }
+
+ groupIdx := 0
+ previousDeleteFunctionBlockBuf, previousDeleteProcedureBlockBuf := make([]bool, len(groups)), make([]bool, len(groups))
for i, stmt := range list {
+ plainText := stmt.Tree.(*mysql.ScriptContext).GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: 0,
+ Stop: stmt.Tokens.Size() - 1,
+ })
+ _ = fmt.Sprintf("Statement %d: %s", i, plainText)
+ for groupIdx < len(groups) && groups[groupIdx].endIdx < i {
+ groupIdx++
+ }
+
+ // We write the remaining text of the stmt except the following cases:
+ // 1. The current group is CREATE FUNCTION/CREATE PROCEDURE, and they had been deleted in the desired schema.
+ var inDeleteFunctionBlock, inCreateFunctionBlock bool
+ var inDeleteProcedureBlock, inCreateProcedureBlock bool
+
+ if groupIdx < len(groups) && groups[groupIdx].tp == groupTypeCreateFunction {
+ inDeleteFunctionBlock = true
+ schema := toState.schemas[""]
+ if schema != nil {
+ if _, ok := schema.functions[groups[groupIdx].objectName]; ok {
+ inDeleteFunctionBlock = false
+ }
+ }
+ inCreateFunctionBlock = !inDeleteFunctionBlock
+ }
+ if groupIdx < len(groups) && groups[groupIdx].tp == groupTypeCreateProcedure {
+ inDeleteProcedureBlock = true
+ schema := toState.schemas[""]
+ if schema != nil {
+ if _, ok := schema.procedures[groups[groupIdx].objectName]; ok {
+ inDeleteProcedureBlock = false
+ }
+ }
+ inCreateProcedureBlock = !inDeleteProcedureBlock
+ }
+
listener.lastTokenIndex = 0
listener.currentStatementIndex = i
+ listener.inCreateFunctionBlock = inCreateFunctionBlock
+ listener.inDeleteFunctionBlock = inDeleteFunctionBlock
+ listener.inCreateProcedureBlock = inCreateProcedureBlock
+ listener.inDeleteProcedureBlock = inDeleteProcedureBlock
+ previousDeleteFunctionBlockBuf[groupIdx] = inDeleteFunctionBlock
+ previousDeleteProcedureBlockBuf[groupIdx] = inDeleteProcedureBlock
+ listener.firstStatementInBlock = i == groups[groupIdx].beginIdx
+ if groupIdx > 0 {
+ listener.previousDeleteFunctionBlock = previousDeleteFunctionBlockBuf[groupIdx-1]
+ listener.previousDeleteProcedureBlock = previousDeleteProcedureBlockBuf[groupIdx-1]
+ }
+
antlr.ParseTreeWalkerDefault.Walk(listener, stmt.Tree)
if listener.err != nil {
break
}
- if _, err := listener.result.WriteString(
- stmt.Tokens.GetTextFromInterval(antlr.Interval{
- Start: listener.lastTokenIndex,
- Stop: stmt.Tokens.Size() - 1,
- }),
- ); err != nil {
- return "", err
+ if !inDeleteProcedureBlock && !inDeleteFunctionBlock {
+ if _, err := listener.result.WriteString(
+ stmt.Tokens.GetTextFromInterval(antlr.Interval{
+ Start: listener.lastTokenIndex,
+ Stop: stmt.Tokens.Size() - 1,
+ }),
+ ); err != nil {
+ return "", err
+ }
}
}
if listener.err != nil {
@@ -79,6 +135,14 @@ func GetDesignSchema(_, baselineSchema string, to *storepb.DatabaseSchemaMetadat
return "", err
}
+ if err := writeRemainingFunctions(&listener.result, to, toState); err != nil {
+ return "", err
+ }
+
+ if err := writeRemainingProcedures(&listener.result, to, toState); err != nil {
+ return "", err
+ }
+
s := listener.result.String()
if !strings.HasSuffix(s, "\n") {
// The last statement of the result is SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
@@ -134,6 +198,15 @@ type mysqlDesignSchemaGenerator struct {
finalViewStatementIndex map[string]int
hasTemporaryView bool
+ inCreateFunctionBlock bool
+ inDeleteFunctionBlock bool
+ inCreateProcedureBlock bool
+ inDeleteProcedureBlock bool
+
+ previousDeleteFunctionBlock bool
+ previousDeleteProcedureBlock bool
+ firstStatementInBlock bool
+
to *databaseState
result strings.Builder
currentTable *tableState
@@ -192,7 +265,7 @@ func (g *mysqlDesignSchemaGenerator) EnterCreateView(ctx *mysql.CreateViewContex
g.err = err
return
}
- equal, err := mysqlparser.IsViewTailEqualViewStmt(viewTail, buf.String())
+ equal, err := mysqlparser.IsViewTailEqualCreateViewStmt(viewTail, buf.String())
if err != nil {
g.err = err
return
@@ -1115,11 +1188,97 @@ func (g *mysqlDesignSchemaGenerator) EnterDropView(ctx *mysql.DropViewContext) {
}
}
-func (g *mysqlDesignSchemaGenerator) EnterCreateProcedure(*mysql.CreateProcedureContext) {
+func (g *mysqlDesignSchemaGenerator) EnterCreateFunction(ctx *mysql.CreateFunctionContext) {
if g.err != nil {
return
}
+ p := ctx.GetParent()
+ pCtx, ok := p.(*mysql.CreateStatementContext)
+ if !ok {
+ return
+ }
+ pp := p.GetParent()
+ if _, ok := pp.(*mysql.SimpleStatementContext); !ok {
+ return
+ }
+ ppp := pp.GetParent()
+ if _, ok := ppp.(*mysql.QueryContext); !ok {
+ return
+ }
+
+ // The create function we wrote does not contain the set statement, so the delimiter may appear before the create function statement.
+ // But be carefully, the set statement in this block may had been keep/drop delemeter, so we dont't need to keep/drop it again.
+ // Before the create function statement, there may be heading delimiter comment, it belongs to the previous block. We follow the table below to handle it:
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | Current Delete | Previous Delete | Operation |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | True | False | Keep the heading delimiter, and skip the current statement |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | True | True | Just skip the current statement |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | False | False | Nothing todo |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | False | True | Delete the heading delimiter, and remaining others. |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ var remainningText string
+ deleteRoutine := g.inDeleteFunctionBlock || g.inDeleteProcedureBlock
+ previousDeleteRoutine := g.previousDeleteFunctionBlock || g.previousDeleteProcedureBlock
+ if deleteRoutine {
+ if !previousDeleteRoutine && g.firstStatementInBlock {
+ headingTokens := pCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: pCtx.GetStart().GetTokenIndex() - 1,
+ })
+ delimiterRegexp := regexp.MustCompile(`(?i)^-- DELIMITER\s+(?P<DELIMITER>[^\s\\]+)\s*`)
+ headingNewLine := strings.HasPrefix(headingTokens, "\n")
+ if v := strings.Split(strings.TrimSpace(headingTokens), "\n"); len(v) > 0 && delimiterRegexp.MatchString(v[0]) {
+ s := v[0]
+ if headingNewLine {
+ s = "\n" + s
+ }
+ if _, err := g.result.WriteString(s); err != nil {
+ g.err = err
+ return
+ }
+ }
+ g.lastTokenIndex = pCtx.GetParser().GetTokenStream().Size() - 1
+ }
+ g.lastTokenIndex = pCtx.GetParser().GetTokenStream().Size() - 1
+ } else if previousDeleteRoutine && g.firstStatementInBlock {
+ headingTokens := pCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: pCtx.GetStart().GetTokenIndex() - 1,
+ })
+ delimiterRegexp := regexp.MustCompile(`(?i)^-- DELIMITER\s+(?P<DELIMITER>[^\s\\]+)\s*`)
+ headingNewLineNumber := len(headingTokens) - len(strings.TrimLeft(headingTokens, "\n"))
+ trailingNewLine := len(headingTokens) - len(strings.TrimRight(headingTokens, "\n"))
+ if v := strings.Split(strings.TrimSpace(headingTokens), "\n"); len(v) > 0 && delimiterRegexp.MatchString(v[0]) {
+ remainningText = strings.Repeat("\n", headingNewLineNumber)
+ remainningText += strings.Join(v[1:], "\n")
+ remainningText += strings.Repeat("\n", trailingNewLine)
+ g.lastTokenIndex = pCtx.GetStart().GetTokenIndex()
+ }
+ } else if !previousDeleteRoutine && g.firstStatementInBlock {
+ headingTokens := pCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: pCtx.GetStart().GetTokenIndex() - 1,
+ })
+ delimiterRegexp := regexp.MustCompile(`(?i)^-- DELIMITER\s+(?P<DELIMITER>[^\s\\]+)\s*`)
+ headingNewLineNumber := len(headingTokens) - len(strings.TrimLeft(headingTokens, "\n"))
+ trailingNewLine := len(headingTokens) - len(strings.TrimRight(headingTokens, "\n"))
+ if v := strings.Split(strings.TrimSpace(headingTokens), "\n"); len(v) > 0 && delimiterRegexp.MatchString(v[0]) {
+ if _, err := g.result.WriteString(fmt.Sprintf("%s%s", strings.Repeat("\n", headingNewLineNumber), v[0])); err != nil {
+ g.err = err
+ return
+ }
+ remainningText = "\n" // The \n between v[0] and v[1]
+ remainningText += strings.Join(v[1:], "\n")
+ remainningText += strings.Repeat("\n", trailingNewLine)
+ g.lastTokenIndex = pCtx.GetStart().GetTokenIndex()
+ }
+ }
+
if err := writeRemainingTables(&g.result, g.desired, g.to); err != nil {
g.err = err
return
@@ -1129,13 +1288,152 @@ func (g *mysqlDesignSchemaGenerator) EnterCreateProcedure(*mysql.CreateProcedure
g.err = err
return
}
+
+ if _, err := g.result.WriteString(remainningText); err != nil {
+ g.err = err
+ return
+ }
+
+ _, funcName := mysqlparser.NormalizeMySQLFunctionName(pCtx.CreateFunction().FunctionName())
+ schema, ok := g.to.schemas[""]
+ if !ok || schema == nil {
+ g.lastTokenIndex = ctx.GetParser().GetTokenStream().Size() - 1
+ return
+ }
+ // We follow the strategies below to handle the function definition:
+ // 1. If the function do not appear in the desired schema, we drop the function definition.
+ // TODO(zp): We should also remove the heading set statement.
+ // 2. If the function can be found in the desired schema, we compare the final function definition with the desired function definition.
+ // i. If they are the same, we keep the function definition.
+ // ii. Otherwise, we should drop the function definition and write the desired function definition.
+ funcDef, ok := schema.functions[funcName]
+ if ok {
+ // Compare the function definition.
+ defInParser := pCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: pCtx.GetStart().GetTokenIndex(),
+ Stop: pCtx.GetStop().GetTokenIndex(),
+ })
+ if funcDef.definition == defInParser {
+ // Function do not change, keep the function definition.
+ if _, err := g.result.WriteString(pCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: pCtx.GetStop().GetTokenIndex(),
+ })); err != nil {
+ g.err = err
+ return
+ }
+ g.lastTokenIndex = pCtx.GetStop().GetTokenIndex() + 1
+ delete(schema.functions, funcName)
+ return
+ }
+ if _, err := g.result.WriteString(pCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: pCtx.GetStart().GetTokenIndex() - 1,
+ })); err != nil {
+ g.err = err
+ return
+ }
+ g.lastTokenIndex = pCtx.GetStop().GetTokenIndex() + 1
+ if _, err := g.result.WriteString(funcDef.definition); err != nil {
+ g.err = err
+ return
+ }
+ delete(schema.functions, funcName)
+ return
+ }
+ g.lastTokenIndex = pCtx.GetParser().GetTokenStream().Size() - 1
}
-func (g *mysqlDesignSchemaGenerator) EnterCreateFunction(*mysql.CreateFunctionContext) {
+func (g *mysqlDesignSchemaGenerator) EnterCreateProcedure(ctx *mysql.CreateProcedureContext) {
if g.err != nil {
return
}
+ p := ctx.GetParent()
+ pCtx, ok := p.(*mysql.CreateStatementContext)
+ if !ok {
+ return
+ }
+ pp := p.GetParent()
+ if _, ok := pp.(*mysql.SimpleStatementContext); !ok {
+ return
+ }
+ ppp := pp.GetParent()
+ if _, ok := ppp.(*mysql.QueryContext); !ok {
+ return
+ }
+
+ // The create procedure we wrote does not contain the set statement, so the delimiter may appear before the create procedure statement.
+ // Before the create procedure statement, there may be heading delimiter comment, it belongs to the previous block. We follow the table below to handle it:
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | Current Delete | Previous Delete | Operation |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | True | False | Keep the heading delimiter, and skip the current statement |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | True | True | Just skip the current statement |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | False | False | Write the possible delimiter before writing other objects |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | False | True | Delete the heading delimiter, and remaining others. |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ var remainningText string
+ deleteRoutine := g.inDeleteFunctionBlock || g.inDeleteProcedureBlock
+ previousDeleteRoutine := g.previousDeleteFunctionBlock || g.previousDeleteProcedureBlock
+ if deleteRoutine {
+ if !previousDeleteRoutine && g.firstStatementInBlock {
+ headingTokens := ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: ctx.GetStart().GetTokenIndex() - 1,
+ })
+ delimiterRegexp := regexp.MustCompile(`(?i)^-- DELIMITER\s+(?P<DELIMITER>[^\s\\]+)\s*`)
+ headingNewLine := strings.HasPrefix(headingTokens, "\n")
+ if v := strings.Split(strings.TrimSpace(headingTokens), "\n"); len(v) > 0 && delimiterRegexp.MatchString(v[0]) {
+ s := v[0]
+ if headingNewLine {
+ s = "\n" + s
+ }
+ if _, err := g.result.WriteString(s); err != nil {
+ g.err = err
+ return
+ }
+ }
+ g.lastTokenIndex = ctx.GetParser().GetTokenStream().Size() - 1
+ }
+ g.lastTokenIndex = ctx.GetParser().GetTokenStream().Size() - 1
+ } else if previousDeleteRoutine && g.firstStatementInBlock {
+ headingTokens := pCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: pCtx.GetStart().GetTokenIndex() - 1,
+ })
+ delimiterRegexp := regexp.MustCompile(`(?i)^-- DELIMITER\s+(?P<DELIMITER>[^\s\\]+)\s*`)
+ headingNewLineNumber := len(headingTokens) - len(strings.TrimLeft(headingTokens, "\n"))
+ trailingNewLine := len(headingTokens) - len(strings.TrimRight(headingTokens, "\n"))
+ if v := strings.Split(strings.TrimSpace(headingTokens), "\n"); len(v) > 0 && delimiterRegexp.MatchString(v[0]) {
+ remainningText = strings.Repeat("\n", headingNewLineNumber)
+ remainningText += strings.Join(v[1:], "\n")
+ remainningText += strings.Repeat("\n", trailingNewLine)
+ g.lastTokenIndex = pCtx.GetStart().GetTokenIndex()
+ }
+ } else if !previousDeleteRoutine && g.firstStatementInBlock {
+ headingTokens := pCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: pCtx.GetStart().GetTokenIndex() - 1,
+ })
+ delimiterRegexp := regexp.MustCompile(`(?i)^-- DELIMITER\s+(?P<DELIMITER>[^\s\\]+)\s*`)
+ headingNewLineNumber := len(headingTokens) - len(strings.TrimLeft(headingTokens, "\n"))
+ trailingNewLine := len(headingTokens) - len(strings.TrimRight(headingTokens, "\n"))
+ if v := strings.Split(strings.TrimSpace(headingTokens), "\n"); len(v) > 0 && delimiterRegexp.MatchString(v[0]) {
+ if _, err := g.result.WriteString(fmt.Sprintf("%s%s", strings.Repeat("\n", headingNewLineNumber), v[0])); err != nil {
+ g.err = err
+ return
+ }
+ remainningText = "\n" // The \n between v[0] and v[1]
+ remainningText += strings.Join(v[1:], "\n")
+ remainningText += strings.Repeat("\n", trailingNewLine)
+ g.lastTokenIndex = pCtx.GetStart().GetTokenIndex()
+ }
+ }
+
if err := writeRemainingTables(&g.result, g.desired, g.to); err != nil {
g.err = err
return
@@ -1145,6 +1443,65 @@ func (g *mysqlDesignSchemaGenerator) EnterCreateFunction(*mysql.CreateFunctionCo
g.err = err
return
}
+
+ if err := writeRemainingFunctions(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ return
+ }
+
+ if _, err := g.result.WriteString(remainningText); err != nil {
+ g.err = err
+ return
+ }
+
+ _, procedureName := mysqlparser.NormalizeMySQLProcedureName(pCtx.CreateProcedure().ProcedureName())
+ schema, ok := g.to.schemas[""]
+ if !ok || schema == nil {
+ return
+ }
+
+ // We follow the strategies below to handle the procedure definition:
+ // 1. If the procedure do not appear in the desired schema, we drop the procedure definition.
+ // TODO(zp): We should also remove the heading set statement.
+ // 2. If the procedure can be found in the desired schema, we compare the procedure definition with the desired procedure definition.
+ // i. If they are the same, we keep the procedure definition.
+ // ii. Otherwise, we should drop the procedure definition and write the desired procedure definition.
+ procedureDef, ok := schema.procedures[procedureName]
+ if ok {
+ // Compare the procedure definition.
+ defInParser := pCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: pCtx.GetStart().GetTokenIndex(),
+ Stop: pCtx.GetStop().GetTokenIndex(),
+ })
+ if procedureDef.definition == defInParser {
+ // procedure do not change, keep the procedure definition.
+ if _, err := g.result.WriteString(pCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: pCtx.GetStop().GetTokenIndex(),
+ })); err != nil {
+ g.err = err
+ return
+ }
+ g.lastTokenIndex = pCtx.GetStop().GetTokenIndex() + 1
+ delete(schema.procedures, procedureName)
+ return
+ }
+ if _, err := g.result.WriteString(pCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: pCtx.GetStart().GetTokenIndex() - 1,
+ })); err != nil {
+ g.err = err
+ return
+ }
+ g.lastTokenIndex = pCtx.GetStop().GetTokenIndex() + 1
+ if _, err := g.result.WriteString(procedureDef.definition); err != nil {
+ g.err = err
+ return
+ }
+ delete(schema.procedures, procedureName)
+ return
+ }
+ g.lastTokenIndex = pCtx.GetParser().GetTokenStream().Size() - 1
}
func (g *mysqlDesignSchemaGenerator) EnterCreateEvent(*mysql.CreateEventContext) {
@@ -1161,6 +1518,16 @@ func (g *mysqlDesignSchemaGenerator) EnterCreateEvent(*mysql.CreateEventContext)
g.err = err
return
}
+
+ if err := writeRemainingFunctions(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ return
+ }
+
+ if err := writeRemainingProcedures(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ return
+ }
}
func (g *mysqlDesignSchemaGenerator) EnterCreateTriggers(*mysql.CreateTriggerContext) {
@@ -1177,6 +1544,15 @@ func (g *mysqlDesignSchemaGenerator) EnterCreateTriggers(*mysql.CreateTriggerCon
g.err = err
return
}
+
+ if err := writeRemainingFunctions(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ }
+
+ if err := writeRemainingProcedures(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ return
+ }
}
// EnterSetStatement is called when production setStatement is entered.
@@ -1188,6 +1564,68 @@ func (g *mysqlDesignSchemaGenerator) EnterSetStatement(ctx *mysql.SetStatementCo
if g.err != nil {
return
}
+ if _, ok := ctx.GetParent().(*mysql.SimpleStatementContext); !ok {
+ return
+ }
+ if _, ok := ctx.GetParent().GetParent().(*mysql.QueryContext); !ok {
+ return
+ }
+ // Before the set statement, there may be heading delimiter comment, it belongs to the previous block. We follow the table below to handle it:
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | Current Delete | Previous Delete | Operation |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | True | False | Keep the heading delimiter, and skip the current set statement |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | True | True | Just skip the current set statement |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | False | False | Nothing todo |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ // | False | True | Delete the heading delimiter, and remaining others. |
+ // +----------------+-----------------+----------------------------------------------------------------+
+ deleteRoutine := g.inDeleteFunctionBlock || g.inDeleteProcedureBlock
+ previousDeleteRoutine := g.previousDeleteFunctionBlock || g.previousDeleteProcedureBlock
+ if deleteRoutine {
+ if !previousDeleteRoutine && g.firstStatementInBlock {
+ headingTokens := ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: ctx.GetStart().GetTokenIndex() - 1,
+ })
+ delimiterRegexp := regexp.MustCompile(`(?i)^-- DELIMITER\s+(?P<DELIMITER>[^\s\\]+)\s*`)
+ headingNewLineNumber := len(headingTokens) - len(strings.TrimLeft(headingTokens, "\n"))
+ if v := strings.Split(strings.TrimSpace(headingTokens), "\n"); len(v) > 0 && delimiterRegexp.MatchString(v[0]) {
+ s := v[0]
+ s = strings.Repeat("\n", headingNewLineNumber) + s
+ if _, err := g.result.WriteString(s); err != nil {
+ g.err = err
+ return
+ }
+ }
+ }
+ g.lastTokenIndex = ctx.GetParser().GetTokenStream().Size() - 1
+ } else if previousDeleteRoutine && g.firstStatementInBlock {
+ headingTokens := ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: ctx.GetStart().GetTokenIndex() - 1,
+ })
+ delimiterRegexp := regexp.MustCompile(`(?i)^-- DELIMITER\s+(?P<DELIMITER>[^\s\\]+)\s*`)
+ headingNewLine := len(headingTokens) - len(strings.TrimRight(headingTokens, "\n"))
+ trailingNewLine := len(headingTokens) - len(strings.TrimRight(headingTokens, "\n"))
+ if v := strings.Split(strings.TrimSpace(headingTokens), "\n"); len(v) > 0 && delimiterRegexp.MatchString(v[0]) {
+ if _, err := g.result.WriteString(strings.Repeat("\n", headingNewLine)); err != nil {
+ g.err = err
+ return
+ }
+ remainning := strings.Join(v[1:], "\n")
+ if _, err := g.result.WriteString(remainning); err != nil {
+ g.err = err
+ return
+ }
+ if _, err := g.result.WriteString(strings.Repeat("\n", trailingNewLine)); err != nil {
+ g.err = err
+ }
+ g.lastTokenIndex = ctx.GetStart().GetTokenIndex()
+ }
+ }
curSet := strings.TrimSpace(ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx))
if curSet != `SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS` && !strings.HasPrefix(curSet, "SET character_set_client") {
@@ -1203,6 +1641,19 @@ func (g *mysqlDesignSchemaGenerator) EnterSetStatement(ctx *mysql.SetStatementCo
g.err = err
return
}
+
+ if (curSet == `SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS`) || (strings.HasPrefix(curSet, "SET character_set_client") && (g.inDeleteProcedureBlock || g.inCreateProcedureBlock)) {
+ if err := writeRemainingFunctions(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ return
+ }
+ }
+ if curSet == `SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS` {
+ if err := writeRemainingProcedures(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ return
+ }
+ }
}
func writeRemainingTables(w io.StringWriter, to *storepb.DatabaseSchemaMetadata, state *databaseState) error {
@@ -1297,6 +1748,94 @@ func writeRemainingViews(w io.StringWriter, to *storepb.DatabaseSchemaMetadata,
return nil
}
+func writeRemainingProcedures(w io.StringWriter, to *storepb.DatabaseSchemaMetadata, state *databaseState) error {
+ firstProcedure := true
+ // Follow the order of the input schemas.
+ for _, schema := range to.Schemas {
+ schemaState, ok := state.schemas[schema.Name]
+ if !ok {
+ continue
+ }
+ // Follow the order of the input procedures.
+ for idx, procedure := range schema.Procedures {
+ procedure, ok := schemaState.procedures[procedure.Name]
+ if !ok {
+ continue
+ }
+ if firstProcedure {
+ firstProcedure = false
+ if _, err := w.WriteString("\n"); err != nil {
+ return err
+ }
+ }
+ if _, err := w.WriteString(getProcedureAnnouncement(procedure.name)); err != nil {
+ return err
+ }
+
+ // Avoid new line.
+ buf := &strings.Builder{}
+ if err := procedure.toString(buf); err != nil {
+ return err
+ }
+ if idx == len(schema.Procedures)-1 && buf.String()[len(buf.String())-1] == '\n' {
+ if _, err := w.WriteString(buf.String()[:len(buf.String())-1]); err != nil {
+ return err
+ }
+ } else {
+ if _, err := w.WriteString(buf.String()); err != nil {
+ return err
+ }
+ }
+ delete(schemaState.procedures, procedure.name)
+ }
+ }
+ return nil
+}
+
+func writeRemainingFunctions(w io.StringWriter, to *storepb.DatabaseSchemaMetadata, state *databaseState) error {
+ firstFunction := true
+ // Follow the order of the input schemas.
+ for _, schema := range to.Schemas {
+ schemaState, ok := state.schemas[schema.Name]
+ if !ok {
+ continue
+ }
+ // Follow the order of the input functions.
+ for idx, function := range schema.Functions {
+ function, ok := schemaState.functions[function.Name]
+ if !ok {
+ continue
+ }
+ if firstFunction {
+ firstFunction = false
+ if _, err := w.WriteString("\n"); err != nil {
+ return err
+ }
+ }
+ if _, err := w.WriteString(getFunctionAnnouncement(function.name)); err != nil {
+ return err
+ }
+
+ // Avoid new line.
+ buf := &strings.Builder{}
+ if err := function.toString(buf); err != nil {
+ return err
+ }
+ if idx == len(schema.Functions)-1 && buf.String()[len(buf.String())-1] == '\n' {
+ if _, err := w.WriteString(buf.String()[:len(buf.String())-1]); err != nil {
+ return err
+ }
+ } else {
+ if _, err := w.WriteString(buf.String()); err != nil {
+ return err
+ }
+ }
+ delete(schemaState.functions, function.name)
+ }
+ }
+ return nil
+}
+
func getViewAnnouncement(name string) string {
return fmt.Sprintf("\nDROP VIEW IF EXISTS `%s`;\n--\n-- View structure for `%s`\n--\n", name, name)
}
@@ -1305,6 +1844,14 @@ func getTableAnnouncement(name string) string {
return fmt.Sprintf("\n--\n-- Table structure for `%s`\n--\n", name)
}
+func getFunctionAnnouncement(name string) string {
+ return fmt.Sprintf("\n--\n-- Function structure for `%s`\n--\n", name)
+}
+
+func getProcedureAnnouncement(name string) string {
+ return fmt.Sprintf("\n--\n-- Procedure structure for `%s`\n--\n", name)
+}
+
// getDataTypePlainText returns the plain text of the data type,
// which excludes the charset candidate.
// For example, for "varchar(10) CHARACTER SET utf8mb4",
diff --git a/backend/plugin/schema/mysql/get_design_schema_test.go b/backend/plugin/schema/mysql/get_design_schema_test.go
index fdd231e4f7..5517d58e8b 100644
--- a/backend/plugin/schema/mysql/get_design_schema_test.go
+++ b/backend/plugin/schema/mysql/get_design_schema_test.go
@@ -21,6 +21,8 @@ func TestGetDesignSchema_Debug(t *testing.T) {
TestGetDesignSchema_Basic(t)
TestGetDesignSchema_Partition(t)
TestGetDesignSchema_View(t)
+ TestGetDesignSchema_Function(t)
+ TestGetDesignSchema_Procedure(t)
}
func TestGetDesignSchema_Basic(t *testing.T) {
@@ -49,6 +51,24 @@ func TestGetDesignSchema_View(t *testing.T) {
}, false)
}
+func TestGetDesignSchema_Function(t *testing.T) {
+ testGetDesignSchema(t, []string{
+ "testdata/get-design-schema/function/create-new-function.yaml",
+ "testdata/get-design-schema/function/delete-function.yaml",
+ "testdata/get-design-schema/function/delete-function-without-set.yaml",
+ "testdata/get-design-schema/function/modify-function.yaml",
+ }, false)
+}
+
+func TestGetDesignSchema_Procedure(t *testing.T) {
+ testGetDesignSchema(t, []string{
+ "testdata/get-design-schema/procedure/create-new-procedure.yaml",
+ "testdata/get-design-schema/procedure/delete-procedure.yaml",
+ "testdata/get-design-schema/procedure/delete-procedure-without-set.yaml",
+ "testdata/get-design-schema/procedure/modify-procedure.yaml",
+ }, false)
+}
+
type designTest struct {
Description string
Baseline string
diff --git a/backend/plugin/schema/mysql/state.go b/backend/plugin/schema/mysql/state.go
index 0ce9571572..f41fdcc107 100644
--- a/backend/plugin/schema/mysql/state.go
+++ b/backend/plugin/schema/mysql/state.go
@@ -65,6 +65,8 @@ type schemaState struct {
tables map[string]*tableState
views map[string]*viewState
handledViews map[string]bool
+ functions map[string]*functionState
+ procedures map[string]*procedureState
}
func newSchemaState() *schemaState {
@@ -72,6 +74,8 @@ func newSchemaState() *schemaState {
tables: make(map[string]*tableState),
views: make(map[string]*viewState),
handledViews: make(map[string]bool),
+ functions: make(map[string]*functionState),
+ procedures: make(map[string]*procedureState),
}
}
@@ -84,6 +88,12 @@ func convertToSchemaState(schema *storepb.SchemaMetadata) *schemaState {
for i, view := range schema.Views {
state.views[view.Name] = convertToViewState(i, view)
}
+ for i, function := range schema.Functions {
+ state.functions[function.Name] = convertToFunctionState(i, function)
+ }
+ for i, procedure := range schema.Procedures {
+ state.procedures[procedure.Name] = convertToProcedureState(i, procedure)
+ }
return state
}
@@ -103,13 +113,22 @@ func (s *schemaState) convertToSchemaMetadata() *storepb.SchemaMetadata {
for _, view := range s.views {
views = append(views, view.convertToViewMetadata())
}
+ var functions []*storepb.FunctionMetadata
+ for _, function := range s.functions {
+ functions = append(functions, function.convertToFunctionMetadata())
+ }
+ var procedures []*storepb.ProcedureMetadata
+ for _, procedure := range s.procedures {
+ procedures = append(procedures, procedure.convertToProcedureMetadata())
+ }
return &storepb.SchemaMetadata{
- Name: s.name,
- Tables: tables,
- Views: views,
+ Name: s.name,
+ Tables: tables,
+ Views: views,
+ Functions: functions,
+ Procedures: procedures,
// Unsupported, for tests only.
- Functions: []*storepb.FunctionMetadata{},
Streams: []*storepb.StreamMetadata{},
Tasks: []*storepb.TaskMetadata{},
MaterializedViews: []*storepb.MaterializedViewMetadata{},
@@ -425,6 +444,74 @@ func (v *viewState) toString(buf io.StringWriter) error {
return nil
}
+type procedureState struct {
+ id int
+ name string
+ definition string
+}
+
+func (f *procedureState) convertToProcedureMetadata() *storepb.ProcedureMetadata {
+ return &storepb.ProcedureMetadata{
+ Name: f.name,
+ Definition: f.definition,
+ }
+}
+
+func convertToProcedureState(id int, procedure *storepb.ProcedureMetadata) *procedureState {
+ return &procedureState{
+ id: id,
+ name: procedure.Name,
+ definition: procedure.Definition,
+ }
+}
+
+func (f *procedureState) toString(buf io.StringWriter) error {
+ def := f.definition
+ if !strings.HasSuffix(def, " ;;") {
+ def += " ;;"
+ }
+
+ stmt := fmt.Sprintf("DELIMITER ;;\n%s\nDELIMITER ;", def)
+ if _, err := buf.WriteString(stmt); err != nil {
+ return err
+ }
+ return nil
+}
+
+type functionState struct {
+ id int
+ name string
+ definition string
+}
+
+func (f *functionState) convertToFunctionMetadata() *storepb.FunctionMetadata {
+ return &storepb.FunctionMetadata{
+ Name: f.name,
+ Definition: f.definition,
+ }
+}
+
+func convertToFunctionState(id int, function *storepb.FunctionMetadata) *functionState {
+ return &functionState{
+ id: id,
+ name: function.Name,
+ definition: function.Definition,
+ }
+}
+
+func (f *functionState) toString(buf io.StringWriter) error {
+ def := f.definition
+ if !strings.HasSuffix(def, " ;;") {
+ def += " ;;"
+ }
+
+ stmt := fmt.Sprintf("DELIMITER ;;\n%s\nDELIMITER ;", def)
+ if _, err := buf.WriteString(stmt); err != nil {
+ return err
+ }
+ return nil
+}
+
type indexState struct {
id int
name string
diff --git a/backend/plugin/schema/mysql/stmt_group.go b/backend/plugin/schema/mysql/stmt_group.go
new file mode 100644
index 0000000000..6841bed65c
--- /dev/null
+++ b/backend/plugin/schema/mysql/stmt_group.go
@@ -0,0 +1,175 @@
+package mysql
+
+import (
+ "github.com/antlr4-go/antlr/v4"
+ mysql "github.com/bytebase/mysql-parser"
+
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+)
+
+type stmtType int
+
+const (
+ currentStmtTypeUnknown stmtType = iota
+ currentStmtTypeCreateFunction
+ currentStmtTypeCreateProcedure
+ currentStmtTypeSet
+)
+
+type stmtInfo struct {
+ tp stmtType
+ normalizedObjectName string
+}
+
+// stmtInfoGetter is a listener to get the statement type and normalized object name.
+// Currently, it only support CREATE FUNCTION, CREATE PROCEDURE and SET statement,
+// and it will regard other statements as unknown type.
+type stmtInfoGetter struct {
+ *mysql.BaseMySQLParserListener
+
+ tp stmtType
+ normalizedObjectName string
+
+ result []*stmtInfo
+}
+
+func (g *stmtInfoGetter) ExitQuery(*mysql.QueryContext) {
+ g.result = append(g.result, &stmtInfo{
+ tp: g.tp,
+ normalizedObjectName: g.normalizedObjectName,
+ })
+ g.tp = currentStmtTypeUnknown
+ g.normalizedObjectName = ""
+}
+
+func (g *stmtInfoGetter) EnterSetStatement(ctx *mysql.SetStatementContext) {
+ p := ctx.GetParent()
+ if _, ok := p.(*mysql.SimpleStatementContext); !ok {
+ return
+ }
+ pp := p.GetParent()
+ if _, ok := pp.(*mysql.QueryContext); !ok {
+ return
+ }
+ g.tp = currentStmtTypeSet
+}
+
+func (g *stmtInfoGetter) EnterCreateProcedure(ctx *mysql.CreateProcedureContext) {
+ p := ctx.GetParent()
+ if _, ok := p.(*mysql.CreateStatementContext); !ok {
+ return
+ }
+ pp := p.GetParent()
+ if _, ok := pp.(*mysql.SimpleStatementContext); !ok {
+ return
+ }
+ ppp := pp.GetParent()
+ if _, ok := ppp.(*mysql.QueryContext); !ok {
+ return
+ }
+ g.tp = currentStmtTypeCreateProcedure
+ _, g.normalizedObjectName = mysqlparser.NormalizeMySQLProcedureName(ctx.ProcedureName())
+}
+
+func (g *stmtInfoGetter) EnterCreateFunction(ctx *mysql.CreateFunctionContext) {
+ p := ctx.GetParent()
+ if _, ok := p.(*mysql.CreateStatementContext); !ok {
+ return
+ }
+ pp := p.GetParent()
+ if _, ok := pp.(*mysql.SimpleStatementContext); !ok {
+ return
+ }
+ ppp := pp.GetParent()
+ if _, ok := ppp.(*mysql.QueryContext); !ok {
+ return
+ }
+ g.tp = currentStmtTypeCreateFunction
+ _, g.normalizedObjectName = mysqlparser.NormalizeMySQLFunctionName(ctx.FunctionName())
+}
+
+type groupType int
+
+const (
+ groupTypeUnknown groupType = iota
+ groupTypeCreateFunction // CREATE FUNCTION
+ groupTypeCreateProcedure // CREATE PROCEDURE
+)
+
+type stmtGroup struct {
+ tp groupType
+ beginIdx int
+ endIdx int
+ objectName string
+}
+
+func groupStatement(parseResults []*mysqlparser.ParseResult) ([]*stmtGroup, error) {
+ var groups []*stmtGroup
+ infoGetter := &stmtInfoGetter{}
+ for _, parseResult := range parseResults {
+ antlr.ParseTreeWalkerDefault.Walk(infoGetter, parseResult.Tree)
+ }
+
+ infos := infoGetter.result
+ for i := 0; i < len(infos); i++ {
+ nextIdx := i
+ if infos[i].tp == currentStmtTypeSet {
+ // Find the next SET/CREATE FUNCTION/CREATE PROCEDURE statement.
+ j := i + 1
+ for ; j < len(infos); j++ {
+ if infos[j].tp == currentStmtTypeSet {
+ continue
+ }
+ if infos[j].tp == currentStmtTypeCreateFunction {
+ nextIdx = j + 1
+ groups = append(groups, &stmtGroup{
+ tp: groupTypeCreateFunction,
+ beginIdx: i,
+ endIdx: j,
+ objectName: infos[j].normalizedObjectName,
+ })
+ break
+ }
+ if infos[j].tp == currentStmtTypeCreateProcedure {
+ nextIdx = j + 1
+ groups = append(groups, &stmtGroup{
+ tp: groupTypeCreateProcedure,
+ beginIdx: i,
+ endIdx: j,
+ objectName: infos[j].normalizedObjectName,
+ })
+ break
+ }
+ break
+ }
+ }
+ if nextIdx > i {
+ i = nextIdx - 1
+ }
+ if infos[i].tp == currentStmtTypeCreateFunction {
+ groups = append(groups, &stmtGroup{
+ tp: groupTypeCreateFunction,
+ beginIdx: i,
+ endIdx: i,
+ objectName: infos[i].normalizedObjectName,
+ })
+ continue
+ }
+ if infos[i].tp == currentStmtTypeCreateProcedure {
+ groups = append(groups, &stmtGroup{
+ tp: groupTypeCreateProcedure,
+ beginIdx: i,
+ endIdx: i,
+ objectName: infos[i].normalizedObjectName,
+ })
+ continue
+ }
+ groups = append(groups, &stmtGroup{
+ tp: groupTypeUnknown,
+ beginIdx: i,
+ endIdx: i,
+ })
+ }
+
+ return groups, nil
+}
diff --git a/backend/plugin/schema/mysql/testdata/get-design-schema/function/create-new-function.yaml b/backend/plugin/schema/mysql/testdata/get-design-schema/function/create-new-function.yaml
new file mode 100644
index 0000000000..5c98047055
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/get-design-schema/function/create-new-function.yaml
@@ -0,0 +1,258 @@
+- description: Create new function should before the procedure
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+- description: Create new function in schema without function
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
diff --git a/backend/plugin/schema/mysql/testdata/get-design-schema/function/delete-function-without-set.yaml b/backend/plugin/schema/mysql/testdata/get-design-schema/function/delete-function-without-set.yaml
new file mode 100644
index 0000000000..8da218ebf6
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/get-design-schema/function/delete-function-without-set.yaml
@@ -0,0 +1,678 @@
+- description: Delete the the first function in schema
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome2`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome3`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome2",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ },
+ {
+ "name": "CalcIncome3",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome2`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome3`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+- description: Delete the the middle function in schema
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome2`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome3`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome4",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome4`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ },
+ {
+ "name": "CalcIncome3",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome3`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome4`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome4`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+- description: Delete the the last function in schema
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome2`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome3`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ },
+ {
+ "name": "CalcIncome2",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome2`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
diff --git a/backend/plugin/schema/mysql/testdata/get-design-schema/function/delete-function.yaml b/backend/plugin/schema/mysql/testdata/get-design-schema/function/delete-function.yaml
new file mode 100644
index 0000000000..fe49b0bf8a
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/get-design-schema/function/delete-function.yaml
@@ -0,0 +1,762 @@
+- description: Delete the the first function in schema
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome3`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome2",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ },
+ {
+ "name": "CalcIncome3",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome3`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+- description: Delete the the middle function in schema
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome3`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ },
+ {
+ "name": "CalcIncome3",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome3`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+- description: Delete the the last function in schema
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome3`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome3`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ },
+ {
+ "name": "CalcIncome2",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
diff --git a/backend/plugin/schema/mysql/testdata/get-design-schema/function/modify-function.yaml b/backend/plugin/schema/mysql/testdata/get-design-schema/function/modify-function.yaml
new file mode 100644
index 0000000000..9977ada7d9
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/get-design-schema/function/modify-function.yaml
@@ -0,0 +1,200 @@
+- description: Modify function and delete another funcion
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Function structure for `CalcIncome2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
diff --git a/backend/plugin/schema/mysql/testdata/get-design-schema/get_design_schema.yaml b/backend/plugin/schema/mysql/testdata/get-design-schema/get_design_schema.yaml
index a88267903b..a24dbacaff 100644
--- a/backend/plugin/schema/mysql/testdata/get-design-schema/get_design_schema.yaml
+++ b/backend/plugin/schema/mysql/testdata/get-design-schema/get_design_schema.yaml
@@ -91,6 +91,12 @@
}
]
}
+ ],
+ "functions": [
+ {
+ "name": "hello",
+ "definition": "CREATE DEFINER=`root`@`localhost` FUNCTION `hello`(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4\n DETERMINISTIC\nRETURN CONCAT ('Hello, ', s, '!')"
+ }
]
}
]
@@ -204,6 +210,12 @@
],
"collation": "utf8mb4_0900_ai_ci"
}
+ ],
+ "functions": [
+ {
+ "name": "hello",
+ "definition": "CREATE DEFINER=`root`@`localhost` FUNCTION `hello`(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4\n DETERMINISTIC\nRETURN CONCAT ('Hello, ', s, '!')"
+ }
]
}
]
diff --git a/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/create-new-procedure.yaml b/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/create-new-procedure.yaml
new file mode 100644
index 0000000000..e15a3dc34c
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/create-new-procedure.yaml
@@ -0,0 +1,252 @@
+- description: Create new procedure in schema without procedure
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+- description: Create procedure in schema with function
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ },
+ {
+ "name": "citycount2",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
diff --git a/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/delete-procedure-without-set.yaml b/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/delete-procedure-without-set.yaml
new file mode 100644
index 0000000000..f2e1ec7222
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/delete-procedure-without-set.yaml
@@ -0,0 +1,663 @@
+- description: Delete first procedures in schema
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount3`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount3`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount2",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ },
+ {
+ "name": "citycount3",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount3`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount3`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount3`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+- description: Delete second procedures in schema
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount3`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount3`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ },
+ {
+ "name": "citycount3",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount3`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount3`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount3`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+- description: Delete last procedures in schema
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount3`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount3`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ },
+ {
+ "name": "citycount2",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
diff --git a/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/delete-procedure.yaml b/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/delete-procedure.yaml
new file mode 100644
index 0000000000..39bce0c966
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/delete-procedure.yaml
@@ -0,0 +1,324 @@
+- description: Delete all procedures in schema
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+- description: Delete all procedures in schema
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
diff --git a/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/modify-procedure.yaml b/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/modify-procedure.yaml
new file mode 100644
index 0000000000..90ba9b3d5b
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/get-design-schema/procedure/modify-procedure.yaml
@@ -0,0 +1,207 @@
+- description: Modify procedure
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "name": "test_function",
+ "schemas": [
+ {
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ }
+ ],
+ "tables": [
+ {
+ "name": "t",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "nullable": true,
+ "position": 1,
+ "defaultNull": true
+ }
+ ],
+ "dataSize": "16384",
+ "collation": "utf8mb4_0900_ai_ci"
+ }
+ ],
+ "functions": [
+ {
+ "name": "CalcIncome",
+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"
+ }
+ ],
+ "procedures": [
+ {
+ "name": "citycount",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"
+ },
+ {
+ "name": "citycount2",
+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 100000 INTO cities;\n END"
+ }
+ ]
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci",
+ "characterSet": "utf8mb4"
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Temporary view structure for `v1`
+ --
+ CREATE VIEW `v1` AS SELECT
+ 1 AS `id`;
+
+
+ --
+ -- Table structure for `t`
+ --
+ CREATE TABLE `t` (
+ `id` int DEFAULT NULL
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ --
+ -- Function structure for `CalcIncome`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)
+ DETERMINISTIC
+ BEGIN
+
+ DECLARE income INT;
+
+ SET income = 0;
+
+ label1: WHILE income <= 3000 DO
+ SET income = income + starting_value;
+ END WHILE label1;
+
+ RETURN income;
+
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 1000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ --
+ -- Procedure structure for `citycount2`
+ --
+ SET character_set_client = latin1;
+ SET character_set_results = latin1;
+ SET collation_connection = latin1_swedish_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`%` PROCEDURE `citycount2`(IN country CHAR(3), OUT cities INT)
+ BEGIN
+ SELECT 100000 INTO cities;
+ END ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;