mirror of
https://github.com/dolthub/dolt.git
synced 2026-02-11 10:33:08 -06:00
Extended naming of foreign keys
This commit is contained in:
committed by
Daylon Wilkins
parent
3a311d7a14
commit
5ab2a6b2ae
@@ -38,12 +38,18 @@ const (
|
||||
|
||||
// TableNameRegexStr is the regular expression that valid tables must match.
|
||||
TableNameRegexStr = `^[a-zA-Z]{1}$|^[a-zA-Z]+[-_0-9a-zA-Z]*[0-9a-zA-Z]+$`
|
||||
// ForeignKeyNameRegexStr is the regular expression that valid foreign keys must match.
|
||||
// From the unquoted identifiers: https://dev.mysql.com/doc/refman/8.0/en/identifiers.html
|
||||
ForeignKeyNameRegexStr = `^[-$_0-9a-zA-Z]+$`
|
||||
)
|
||||
|
||||
var tableNameRegex, _ = regexp.Compile(TableNameRegexStr)
|
||||
var (
|
||||
tableNameRegex = regexp.MustCompile(TableNameRegexStr)
|
||||
foreignKeyNameRegex = regexp.MustCompile(ForeignKeyNameRegexStr)
|
||||
|
||||
var ErrNoConflictsResolved = errors.New("no conflicts resolved")
|
||||
var ErrNoAutoIncrementValue = fmt.Errorf("auto increment set for non-numeric column type")
|
||||
ErrNoConflictsResolved = errors.New("no conflicts resolved")
|
||||
ErrNoAutoIncrementValue = fmt.Errorf("auto increment set for non-numeric column type")
|
||||
)
|
||||
|
||||
// IsValidTableName returns true if the name matches the regular expression TableNameRegexStr.
|
||||
// Table names must be composed of 1 or more letters and non-initial numerals, as well as the characters _ and -
|
||||
@@ -51,6 +57,11 @@ func IsValidTableName(name string) bool {
|
||||
return tableNameRegex.MatchString(name)
|
||||
}
|
||||
|
||||
// IsValidForeignKeyName returns true if the name matches the regular expression ForeignKeyNameRegexStr.
|
||||
func IsValidForeignKeyName(name string) bool {
|
||||
return foreignKeyNameRegex.MatchString(name)
|
||||
}
|
||||
|
||||
// Table is a struct which holds row data, as well as a reference to it's schema.
|
||||
type Table struct {
|
||||
vrw types.ValueReadWriter
|
||||
|
||||
@@ -1023,8 +1023,8 @@ func (t *AlterableDoltTable) CreateForeignKey(
|
||||
refTblName string,
|
||||
refColumns []string,
|
||||
onUpdate, onDelete sql.ForeignKeyReferenceOption) error {
|
||||
if fkName != "" && !doltdb.IsValidTableName(fkName) {
|
||||
return fmt.Errorf("invalid foreign key name `%s` as it must match the regular expression %s", fkName, doltdb.TableNameRegexStr)
|
||||
if fkName != "" && !doltdb.IsValidForeignKeyName(fkName) {
|
||||
return fmt.Errorf("invalid foreign key name `%s` as it must match the regular expression %s", fkName, doltdb.ForeignKeyNameRegexStr)
|
||||
}
|
||||
//TODO: move this into go-mysql-server
|
||||
if len(columns) != len(refColumns) {
|
||||
|
||||
@@ -1511,6 +1511,27 @@ SQL
|
||||
[[ "$output" =~ "Warning" ]] || false
|
||||
}
|
||||
|
||||
@test "foreign-keys: extended names supported" {
|
||||
dolt sql <<SQL
|
||||
CREATE TABLE parent2 (
|
||||
pk BIGINT PRIMARY KEY,
|
||||
v1 BIGINT,
|
||||
INDEX idx_v1 (v1)
|
||||
);
|
||||
CREATE TABLE child2 (
|
||||
pk BIGINT PRIMARY KEY,
|
||||
v1 BIGINT,
|
||||
INDEX idx_v1 (v1),
|
||||
CONSTRAINT circuits_123abc4d_fk_circuits_ FOREIGN KEY (pk) REFERENCES parent2 (pk)
|
||||
);
|
||||
ALTER TABLE child2 ADD CONSTRAINT \`\$not-possible-before_\` FOREIGN KEY (v1) REFERENCES parent2 (v1);
|
||||
SQL
|
||||
run dolt schema show child2
|
||||
[ "$status" -eq "0" ]
|
||||
[[ "$output" =~ "circuits_123abc4d_fk_circuits_" ]] || false
|
||||
[[ "$output" =~ '`$not-possible-before_`' ]] || false
|
||||
}
|
||||
|
||||
@test "foreign-keys: self-referential same column(s)" {
|
||||
dolt sql <<SQL
|
||||
CREATE INDEX v1v2 ON parent(v1, v2);
|
||||
|
||||
Reference in New Issue
Block a user