add constraint violations to dolt status (#8209)

This commit is contained in:
James Cor
2024-08-06 10:26:09 -07:00
committed by GitHub
parent fac6cd4a8c
commit e8053d5649
5 changed files with 97 additions and 19 deletions

View File

@@ -259,6 +259,10 @@ func createPrintData(err error, queryist cli.Queryist, sqlCtx *sql.Context, show
case "schema conflict":
conflictsPresent = true
schemaConflictTables[tableName] = true
case "constraint violation":
constraintViolationTables[tableName] = true
default:
panic(fmt.Sprintf("table %s, unexpected merge status: %s", tableName, status))
}

View File

@@ -100,6 +100,15 @@ type statusTableRow struct {
status string
}
func contains(str string, strs []string) bool {
for _, s := range strs {
if s == str {
return true
}
}
return false
}
func newStatusItr(ctx *sql.Context, st *StatusTable) (*StatusItr, error) {
rp := st.rootsProvider
@@ -114,26 +123,16 @@ func newStatusItr(ctx *sql.Context, st *StatusTable) (*StatusItr, error) {
}
rows := make([]statusTableRow, 0, len(stagedTables)+len(unstagedTables))
for _, td := range stagedTables {
tblName := tableName(td)
if doltdb.IsFullTextTable(tblName) {
continue
}
rows = append(rows, statusTableRow{
tableName: tblName,
isStaged: true,
status: statusString(td),
})
cvTables, err := doltdb.TablesWithConstraintViolations(ctx, roots.Working)
if err != nil {
return nil, err
}
for _, td := range unstagedTables {
tblName := tableName(td)
if doltdb.IsFullTextTable(tblName) {
continue
}
for _, tbl := range cvTables {
rows = append(rows, statusTableRow{
tableName: tblName,
isStaged: false,
status: statusString(td),
tableName: tbl,
status: "constraint violation",
})
}
@@ -167,6 +166,35 @@ func newStatusItr(ctx *sql.Context, st *StatusTable) (*StatusItr, error) {
})
}
for _, td := range stagedTables {
tblName := tableName(td)
if doltdb.IsFullTextTable(tblName) {
continue
}
if contains(tblName, cvTables) {
continue
}
rows = append(rows, statusTableRow{
tableName: tblName,
isStaged: true,
status: statusString(td),
})
}
for _, td := range unstagedTables {
tblName := tableName(td)
if doltdb.IsFullTextTable(tblName) {
continue
}
if contains(tblName, cvTables) {
continue
}
rows = append(rows, statusTableRow{
tableName: tblName,
isStaged: false,
status: statusString(td),
})
}
return &StatusItr{rows: rows}, nil
}

View File

@@ -173,6 +173,12 @@ var MergeScripts = []queries.ScriptTest{
Query: "SELECT * FROM dolt_constraint_violations;",
Expected: []sql.Row{{"aTable", uint64(1)}},
},
{
Query: "select * from dolt_status;",
Expected: []sql.Row{
{"aTable", false, "constraint violation"},
},
},
{
Query: "SELECT from_root_ish, violation_type, hex(dolt_row_hash), aColumn, bColumn, CAST(violation_info as CHAR) FROM dolt_constraint_violations_aTable;",
Expected: []sql.Row{
@@ -1269,6 +1275,12 @@ var MergeScripts = []queries.ScriptTest{
Query: "SELECT violation_type, pk, parent_fk from dolt_constraint_violations_child;",
Expected: []sql.Row{{"foreign key", 1, 1}},
},
{
Query: "select * from dolt_status;",
Expected: []sql.Row{
{"child", false, "constraint violation"},
},
},
},
},
{
@@ -1305,6 +1317,10 @@ var MergeScripts = []queries.ScriptTest{
Query: "SELECT * from dolt_constraint_violations_child",
Expected: []sql.Row{},
},
{
Query: "select * from dolt_status;",
Expected: []sql.Row{},
},
{
Query: "SELECT * from parent;",
Expected: []sql.Row{{10, 1}, {30, 2}},
@@ -1349,6 +1365,12 @@ var MergeScripts = []queries.ScriptTest{
Query: "SELECT is_merging, source, target, unmerged_tables FROM DOLT_MERGE_STATUS;",
Expected: []sql.Row{{true, "right", "refs/heads/main", "t"}},
},
{
Query: "select * from dolt_status;",
Expected: []sql.Row{
{"t", false, "constraint violation"},
},
},
},
},
{
@@ -1381,6 +1403,12 @@ var MergeScripts = []queries.ScriptTest{
Query: "SELECT violation_type, pk, col1 from dolt_constraint_violations_t;",
Expected: []sql.Row{{"unique index", 2, 3}, {"unique index", 3, 3}},
},
{
Query: "select * from dolt_status;",
Expected: []sql.Row{
{"t", false, "constraint violation"},
},
},
},
},
{
@@ -1413,6 +1441,12 @@ var MergeScripts = []queries.ScriptTest{
Query: "SELECT violation_type, pk, col1 from dolt_constraint_violations_t;",
Expected: []sql.Row{{"unique index", 2, 3}, {"unique index", 3, 3}},
},
{
Query: "select * from dolt_status;",
Expected: []sql.Row{
{"t", false, "constraint violation"},
},
},
},
},
{
@@ -1445,6 +1479,12 @@ var MergeScripts = []queries.ScriptTest{
Query: "SELECT violation_type, pk, col1, col2 from dolt_constraint_violations_t;",
Expected: []sql.Row{{"unique index", 1, 1, 1}, {"unique index", 2, 1, 1}},
},
{
Query: "select * from dolt_status;",
Expected: []sql.Row{
{"t", false, "constraint violation"},
},
},
},
},
// Behavior between new and old format diverges in the case where right adds

View File

@@ -242,7 +242,7 @@ teardown() {
# Assert that only 'test' is staged for commit ('other' has a constraint violation)
run dolt sql -q "SELECT * from dolt_status;"
[ $status -eq 0 ]
[[ $output =~ "| other | false | modified " ]] || false
[[ $output =~ "| other | false | constraint violation " ]] || false
[[ $output =~ "| test | true | modified " ]] || false
[[ $output =~ "| generated_foo | false | new table " ]] || false

View File

@@ -3006,6 +3006,12 @@ SQL
[[ "$output" =~ "aTable,2" ]] || false
[[ "${#lines[@]}" = "2" ]] || false
run dolt sql -q "SELECT * from dolt_status" -r=csv
log_status_eq "0"
[[ "$output" =~ "table_name,staged,status" ]] || false
[[ "$output" =~ "aTable,false,constraint violation" ]] || false
[[ "${#lines[@]}" = "2" ]] || false
run dolt sql -q "SELECT from_root_ish,violation_type,hex(dolt_row_hash) as dolt_row_hash,aColumn,bColumn,violation_info from dolt_constraint_violations_aTable" -r=csv
log_status_eq "0"
[[ "$output" =~ "from_root_ish,violation_type,dolt_row_hash,aColumn,bColumn,violation_info" ]] || false