Skip to content

Commit a7e14d8

Browse files
authored
Merge pull request #3152 from dolthub/fulghum/trigger_name_length
Validate trigger name length is not over 96 chars
2 parents c27a5b0 + 729f4f5 commit a7e14d8

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

enginetest/queries/trigger_queries.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4839,4 +4839,15 @@ var TriggerErrorTests = []ScriptTest{
48394839
Query: "create trigger trig before insert on v for each row set b = 1",
48404840
ExpectedErr: sql.ErrExpectedTableFoundView,
48414841
},
4842+
{
4843+
// NOTE: We limit trigger names to 96 chars. This is more than MySQL allows (64 chars).
4844+
Name: "trigger name length over limit",
4845+
SetUpScript: []string{
4846+
"CREATE TABLE example_table (id INT AUTO_INCREMENT PRIMARY KEY, value VARCHAR(50));",
4847+
},
4848+
Query: `CREATE TRIGGER this_is_a_very_long_trigger_name_that_is_purposefully_made_to_be_exactly_one_hundred_characters_long
4849+
BEFORE INSERT ON example_table
4850+
FOR EACH ROW BEGIN SET NEW.value = UPPER(NEW.value); END;`,
4851+
ExpectedErr: sql.ErrIdentifierIsTooLong,
4852+
},
48424853
}

sql/analyzer/triggers.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ import (
2828
"github.com/dolthub/go-mysql-server/sql/transform"
2929
)
3030

31+
// maxTriggerNameLength is the maximum length for the name of a trigger. Note that MySQL applies a limit of 64 characters,
32+
// but we allow 96 characters to avoid breaking customers who were relying on the previous behavior where we didn't
33+
// check the length.
34+
const maxTriggerNameLength = 96
35+
3136
// validateCreateTrigger handles CreateTrigger nodes, resolving references to "old" and "new" table references in
3237
// the trigger body. Also validates that these old and new references are being used appropriately -- they are only
3338
// valid for certain kinds of triggers and certain statements.
@@ -37,6 +42,10 @@ func validateCreateTrigger(ctx *sql.Context, a *Analyzer, node sql.Node, scope *
3742
return node, transform.SameTree, nil
3843
}
3944

45+
if len(ct.TriggerName) > maxTriggerNameLength {
46+
return node, transform.SameTree, sql.ErrIdentifierIsTooLong.New(ct.TriggerName)
47+
}
48+
4049
// We just want to verify that the trigger is correctly defined before creating it. If it is, we replace the
4150
// UnresolvedColumn expressions with placeholder expressions that say they are Resolved().
4251
// TODO: this might work badly for databases with tables named new and old. Needs tests.

sql/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ var (
499499
// ErrInvalidIdentifier is returned when an identifier is invalid
500500
ErrInvalidIdentifier = errors.NewKind("invalid identifier: `%s`")
501501

502+
// ErrIdentifierIsTooLong is returned when creating a resource, but the identifier is longer than a name limit
503+
ErrIdentifierIsTooLong = errors.NewKind("Identifier name '%s' is too long")
504+
502505
// ErrInvalidArgument is returned when an argument to a function is invalid.
503506
ErrInvalidArgument = errors.NewKind("Invalid argument to %s")
504507

sql/information_schema/information_schema.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,9 @@ var tablespacesExtensionsSchema = Schema{
698698
var triggersSchema = Schema{
699699
{Name: "TRIGGER_CATALOG", Type: types.MustCreateString(sqltypes.VarChar, 64, Collation_Information_Schema_Default), Default: nil, Nullable: true, Source: TriggersTableName},
700700
{Name: "TRIGGER_SCHEMA", Type: types.MustCreateString(sqltypes.VarChar, 64, Collation_Information_Schema_Default), Default: nil, Nullable: true, Source: TriggersTableName},
701-
{Name: "TRIGGER_NAME", Type: types.MustCreateString(sqltypes.VarChar, 64, Collation_Information_Schema_Default), Default: nil, Nullable: false, Source: TriggersTableName},
701+
// NOTE: MySQL limits trigger names to 64 characters, but we limit them to 96 chars to avoid breaking existing customers who were
702+
// relying on us not enforcing the 64 character limit. This is a good candidate to change in a future major version bump.
703+
{Name: "TRIGGER_NAME", Type: types.MustCreateString(sqltypes.VarChar, 96, Collation_Information_Schema_Default), Default: nil, Nullable: false, Source: TriggersTableName},
702704
{Name: "EVENT_MANIPULATION", Type: types.MustCreateEnumType([]string{"INSERT", "UPDATE", "DELETE"}, Collation_Information_Schema_Default), Default: nil, Nullable: false, Source: TriggersTableName},
703705
{Name: "EVENT_OBJECT_CATALOG", Type: types.MustCreateString(sqltypes.VarChar, 64, Collation_Information_Schema_Default), Default: nil, Nullable: true, Source: TriggersTableName},
704706
{Name: "EVENT_OBJECT_SCHEMA", Type: types.MustCreateString(sqltypes.VarChar, 64, Collation_Information_Schema_Default), Default: nil, Nullable: true, Source: TriggersTableName},

0 commit comments

Comments
 (0)