Fixing the dolt_blame_<tablename> view to backtick quote identifiers in case they need it

This commit is contained in:
Jason Fulghum
2023-12-13 12:12:01 -08:00
parent 561f3aa652
commit de8e5a8c9a
3 changed files with 30 additions and 8 deletions

View File

@@ -1130,7 +1130,7 @@ func (db Database) GetViewDefinition(ctx *sql.Context, viewName string) (sql.Vie
if err != nil {
return sql.ViewDefinition{}, false, err
}
return sql.ViewDefinition{Name: viewName, TextDefinition: blameViewTextDef, CreateViewStatement: fmt.Sprintf("CREATE VIEW %s AS %s", viewName, blameViewTextDef)}, true, nil
return sql.ViewDefinition{Name: viewName, TextDefinition: blameViewTextDef, CreateViewStatement: fmt.Sprintf("CREATE VIEW `%s` AS %s", viewName, blameViewTextDef)}, true, nil
}
key, err := doltdb.NewDataCacheKey(root)

View File

@@ -22,6 +22,7 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlfmt"
)
var errUnblameableTable = errors.New("unable to generate blame view for table without primary key")
@@ -41,7 +42,7 @@ const (
coalesce(to_commit_date, from_commit_date) DESC
) row_num
FROM
dolt_diff_%s -- tableName
` + "`dolt_diff_%s`" + ` -- tableName
)
SELECT
%s -- pksSelectExpression
@@ -107,10 +108,13 @@ func createDoltBlameViewExpression(tableName string, pks []schema.Column) (strin
pksOrderByExpression += ", "
}
allToPks += "to_" + pk.Name
pksPartitionByExpression += "coalesce(to_" + pk.Name + ", from_" + pk.Name + ")"
pksOrderByExpression += "sd.to_" + pk.Name + " ASC "
pksSelectExpression += "sd.to_" + pk.Name + " AS " + pk.Name + ", "
toPk := sqlfmt.QuoteIdentifier("to_" + pk.Name)
fromPk := sqlfmt.QuoteIdentifier("from_" + pk.Name)
allToPks += toPk
pksPartitionByExpression += fmt.Sprintf("coalesce(%s, %s)", toPk, fromPk)
pksOrderByExpression += fmt.Sprintf("sd.%s ASC ", toPk)
pksSelectExpression += fmt.Sprintf("sd.%s AS %s, ", toPk, sqlfmt.QuoteIdentifier(pk.Name))
}
return fmt.Sprintf(viewExpressionTemplate, allToPks, pksPartitionByExpression, tableName,

View File

@@ -1004,8 +1004,7 @@ var DoltScripts = []queries.ScriptTest{
"CREATE TABLE t(pk varchar(20), val int)",
"ALTER TABLE t ADD PRIMARY KEY (pk, val)",
"INSERT INTO t VALUES ('zzz',4),('mult',1),('sub',2),('add',5)",
"CALL dolt_add('.');",
"CALL dolt_commit('-am', 'add rows');",
"CALL dolt_commit('-Am', 'add rows');",
"INSERT INTO t VALUES ('dolt',0),('alt',12),('del',8),('ctl',3)",
"CALL dolt_commit('-am', 'add more rows');",
},
@@ -1025,6 +1024,25 @@ var DoltScripts = []queries.ScriptTest{
},
},
},
{
Name: "blame: table and pk require identifier quoting",
SetUpScript: []string{
"create table `t-1` (`p-k` int primary key, col1 varchar(100));",
"insert into `t-1` values (1, 'one');",
"CALL dolt_commit('-Am', 'adding table t-1');",
"insert into `t-1` values (2, 'two');",
"CALL dolt_commit('-Am', 'adding another row to t-1');",
},
Assertions: []queries.ScriptTestAssertion{
{
Query: "SELECT `p-k`, message FROM `dolt_blame_t-1`;",
Expected: []sql.Row{
{1, "adding table t-1"},
{2, "adding another row to t-1"},
},
},
},
},
{
Name: "Nautobot FOREIGN KEY panic repro",
SetUpScript: []string{