Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions enginetest/queries/trigger_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -4839,4 +4839,15 @@ var TriggerErrorTests = []ScriptTest{
Query: "create trigger trig before insert on v for each row set b = 1",
ExpectedErr: sql.ErrExpectedTableFoundView,
},
{
// NOTE: We limit trigger names to 96 chars. This is more than MySQL allows (64 chars).
Name: "trigger name length over limit",
SetUpScript: []string{
"CREATE TABLE example_table (id INT AUTO_INCREMENT PRIMARY KEY, value VARCHAR(50));",
},
Query: `CREATE TRIGGER this_is_a_very_long_trigger_name_that_is_purposefully_made_to_be_exactly_one_hundred_characters_long
BEFORE INSERT ON example_table
FOR EACH ROW BEGIN SET NEW.value = UPPER(NEW.value); END;`,
ExpectedErr: sql.ErrIdentifierIsTooLong,
},
}
9 changes: 9 additions & 0 deletions sql/analyzer/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import (
"github.com/dolthub/go-mysql-server/sql/transform"
)

// maxTriggerNameLength is the maximum length for the name of a trigger. Note that MySQL applies a limit of 64 characters,
// but we allow 96 characters to avoid breaking customers who were relying on the previous behavior where we didn't
// check the length.
const maxTriggerNameLength = 96

// validateCreateTrigger handles CreateTrigger nodes, resolving references to "old" and "new" table references in
// the trigger body. Also validates that these old and new references are being used appropriately -- they are only
// valid for certain kinds of triggers and certain statements.
Expand All @@ -37,6 +42,10 @@ func validateCreateTrigger(ctx *sql.Context, a *Analyzer, node sql.Node, scope *
return node, transform.SameTree, nil
}

if len(ct.TriggerName) > maxTriggerNameLength {
return node, transform.SameTree, sql.ErrIdentifierIsTooLong.New(ct.TriggerName)
}

// We just want to verify that the trigger is correctly defined before creating it. If it is, we replace the
// UnresolvedColumn expressions with placeholder expressions that say they are Resolved().
// TODO: this might work badly for databases with tables named new and old. Needs tests.
Expand Down
3 changes: 3 additions & 0 deletions sql/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ var (
// ErrInvalidIdentifier is returned when an identifier is invalid
ErrInvalidIdentifier = errors.NewKind("invalid identifier: `%s`")

// ErrIdentifierIsTooLong is returned when creating a resource, but the identifier is longer than a name limit
ErrIdentifierIsTooLong = errors.NewKind("Identifier name '%s' is too long")

// ErrInvalidArgument is returned when an argument to a function is invalid.
ErrInvalidArgument = errors.NewKind("Invalid argument to %s")

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