Skip to content

Commit

Permalink
Progress on schemas table
Browse files Browse the repository at this point in the history
  • Loading branch information
tbantle22 committed Nov 13, 2024
1 parent cc081c4 commit 7a8d094
Show file tree
Hide file tree
Showing 24 changed files with 220 additions and 130 deletions.
3 changes: 2 additions & 1 deletion go/cmd/dolt/commands/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,8 @@ func printStagedDiffs(wr io.Writer, stagedRows []sql.Row, printHelp bool) int {

lines := make([]string, 0, len(stagedRows))
for _, row := range stagedRows {
if !doltdb.IsReadOnlySystemTable(row[0].(string)) {
tblName := row[0].(string)
if !doltdb.IsReadOnlySystemTable(tblName, doltdb.HasDoltPrefix(tblName)) {
switch row[1].(string) {
case "new table":
lines = append(lines, fmt.Sprintf(statusFmt, tblDiffTypeToLabel[diff.AddedTable], row[0].(string)))
Expand Down
7 changes: 4 additions & 3 deletions go/cmd/dolt/commands/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,8 @@ func diffUserTables(queryist cli.Queryist, sqlCtx *sql.Context, dArgs *diffArgs)

doltSchemasChanged := false
for _, delta := range deltas {
if doltdb.IsFullTextTable(delta.TableName.Name) {
tableName := delta.TableName.Name
if doltdb.IsFullTextTable(tableName, doltdb.HasDoltPrefix(tableName)) {
continue
}

Expand Down Expand Up @@ -852,7 +853,7 @@ func shouldPrintTableDelta(tablesToPrint *set.StrSet, toTableName, fromTableName
}

func isDoltSchemasTable(toTableName, fromTableName string) bool {
return fromTableName == doltdb.SchemasTableName || toTableName == doltdb.SchemasTableName
return fromTableName == doltdb.GetSchemasTableName() || toTableName == doltdb.GetSchemasTableName()
}

func getTableInfoAtRef(queryist cli.Queryist, sqlCtx *sql.Context, tableName string, ref string) (diff.TableInfo, error) {
Expand Down Expand Up @@ -1204,7 +1205,7 @@ func diffDoltSchemasTable(
query, err := dbr.InterpolateForDialect("select from_name,to_name,from_type,to_type,from_fragment,to_fragment "+
"from dolt_diff(?, ?, ?) "+
"order by coalesce(from_type, to_type), coalesce(from_name, to_name)",
[]interface{}{dArgs.fromRef, dArgs.toRef, doltdb.SchemasTableName}, dialect.MySQL)
[]interface{}{dArgs.fromRef, dArgs.toRef, doltdb.GetSchemasTableName()}, dialect.MySQL)
if err != nil {
return errhand.BuildDError("Error building diff query").AddCause(err).Build()
}
Expand Down
10 changes: 5 additions & 5 deletions go/cmd/dolt/commands/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (cmd DumpCmd) Exec(ctx context.Context, commandStr string, args []string, d
return HandleVErrAndExitCode(verr, usage)
}

tblNames, err := doltdb.GetNonSystemTableNames(ctx, root)
tblNames, err := doltdb.GetNonSystemTableNames(ctx, root, doltdb.HasDoltPrefix)
if err != nil {
return HandleVErrAndExitCode(errhand.BuildDError("error: failed to get tables").AddCause(err).Build(), usage)
}
Expand Down Expand Up @@ -325,7 +325,7 @@ func dumpProcedures(sqlCtx *sql.Context, engine *engine.SqlEngine, root doltdb.R
}

func dumpTriggers(sqlCtx *sql.Context, engine *engine.SqlEngine, root doltdb.RootValue, writer io.WriteCloser) (rerr error) {
_, _, ok, err := doltdb.GetTableInsensitive(sqlCtx, root, doltdb.TableName{Name: doltdb.SchemasTableName})
_, _, ok, err := doltdb.GetTableInsensitive(sqlCtx, root, doltdb.TableName{Name: doltdb.GetSchemasTableName()})
if err != nil {
return err
}
Expand All @@ -334,7 +334,7 @@ func dumpTriggers(sqlCtx *sql.Context, engine *engine.SqlEngine, root doltdb.Roo
return nil
}

sch, iter, _, err := engine.Query(sqlCtx, "select * from "+doltdb.SchemasTableName)
sch, iter, _, err := engine.Query(sqlCtx, "select * from "+doltdb.GetSchemasTableName())
if err != nil {
return err
}
Expand Down Expand Up @@ -391,7 +391,7 @@ func dumpTriggers(sqlCtx *sql.Context, engine *engine.SqlEngine, root doltdb.Roo
}

func dumpViews(ctx *sql.Context, engine *engine.SqlEngine, root doltdb.RootValue, writer io.WriteCloser) (rerr error) {
_, _, ok, err := doltdb.GetTableInsensitive(ctx, root, doltdb.TableName{Name: doltdb.SchemasTableName})
_, _, ok, err := doltdb.GetTableInsensitive(ctx, root, doltdb.TableName{Name: doltdb.GetSchemasTableName()})
if err != nil {
return err
}
Expand All @@ -400,7 +400,7 @@ func dumpViews(ctx *sql.Context, engine *engine.SqlEngine, root doltdb.RootValue
return nil
}

sch, iter, _, err := engine.Query(ctx, "select * from "+doltdb.SchemasTableName)
sch, iter, _, err := engine.Query(ctx, "select * from "+doltdb.GetSchemasTableName())
if err != nil {
return err
}
Expand Down
15 changes: 8 additions & 7 deletions go/cmd/dolt/commands/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,37 +478,38 @@ func calculateMergeStats(queryist cli.Queryist, sqlCtx *sql.Context, mergeStats
// get table operations
for _, summary := range diffSummaries {
// We want to ignore all statistics for Full-Text tables
if doltdb.IsFullTextTable(summary.TableName.Name) {
tableName := summary.TableName.Name
if doltdb.IsFullTextTable(tableName, doltdb.HasDoltPrefix(tableName)) {
continue
}
// Ignore stats for database collation changes
if strings.HasPrefix(summary.TableName.Name, diff.DBPrefix) {
if strings.HasPrefix(tableName, diff.DBPrefix) {
continue
}
if summary.DiffType == "added" {
allUnmodified = false
mergeStats[summary.TableName.Name] = &merge.MergeStats{
mergeStats[tableName] = &merge.MergeStats{
Operation: merge.TableAdded,
}
} else if summary.DiffType == "dropped" {
allUnmodified = false
mergeStats[summary.TableName.Name] = &merge.MergeStats{
mergeStats[tableName] = &merge.MergeStats{
Operation: merge.TableRemoved,
}
} else if summary.DiffType == "modified" || summary.DiffType == "renamed" {
allUnmodified = false
mergeStats[summary.TableName.Name] = &merge.MergeStats{
mergeStats[tableName] = &merge.MergeStats{
Operation: merge.TableModified,
}
tableStats, err := getTableDiffStats(queryist, sqlCtx, summary.TableName.Name, fromRef, toRef)
tableStats, err := getTableDiffStats(queryist, sqlCtx, tableName, fromRef, toRef)
if err != nil {
return nil, false, err
}
if tableStats != nil && len(tableStats) > 0 {
diffStats[tableStats[0].TableName] = tableStats[0]
}
} else {
mergeStats[summary.TableName.Name] = &merge.MergeStats{
mergeStats[tableName] = &merge.MergeStats{
Operation: merge.TableUnmodified,
}
}
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/dolt/commands/schcmds/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func exportSchemas(ctx context.Context, apr *argparser.ArgParseResults, root dol
}
tablesToExport = []string{tblName}
} else {
tablesToExport, err = doltdb.GetNonSystemTableNames(ctx, root)
tablesToExport, err = doltdb.GetNonSystemTableNames(ctx, root, doltdb.HasDoltPrefix)
if err != nil {
return errhand.BuildDError("error retrieving table names").AddCause(err).Build()
}
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/dolt/commands/schcmds/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func printSchemas(ctx context.Context, apr *argparser.ArgParseResults, dEnv *env

var notFound []string
for _, tblName := range tables {
if doltdb.IsFullTextTable(tblName) {
if doltdb.IsFullTextTable(tblName, doltdb.HasDoltPrefix(tblName)) {
continue
}
ok, err := root.HasTable(ctx, doltdb.TableName{Name: tblName})
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/dolt/commands/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func createPrintData(err error, queryist cli.Queryist, sqlCtx *sql.Context, show
}
shouldIgnoreTable = ignored == doltdb.Ignore
}
shouldIgnoreTable = shouldIgnoreTable || doltdb.IsFullTextTable(tableName)
shouldIgnoreTable = shouldIgnoreTable || doltdb.IsFullTextTable(tableName, doltdb.HasDoltPrefix(tableName))

switch status {
case "renamed":
Expand Down Expand Up @@ -536,7 +536,7 @@ and have %v and %v different commits each, respectively.
cli.Println(stagedHeader)
cli.Println(stagedHeaderHelp)
for tableName, status := range data.stagedTables {
if !doltdb.IsReadOnlySystemTable(tableName) {
if !doltdb.IsReadOnlySystemTable(tableName, doltdb.HasDoltPrefix(tableName)) {
text := fmt.Sprintf(statusFmt, status+":", tableName)
greenText := color.GreenString(text)
cli.Println(greenText)
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/dolt/commands/tblcmds/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (cmd RmCmd) Exec(ctx context.Context, commandStr string, args []string, dEn
}

for _, tableName := range apr.Args {
if doltdb.IsReadOnlySystemTable(tableName) {
if doltdb.IsReadOnlySystemTable(tableName, doltdb.HasDoltPrefix(tableName)) {
return commands.HandleVErrAndExitCode(
errhand.BuildDError("error removing table %s", tableName).AddCause(doltdb.ErrSystemTableCannotBeModified).Build(), usage)
}
Expand Down
8 changes: 8 additions & 0 deletions go/libraries/doltcore/diff/table_deltas.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,14 @@ func (td TableDelta) CurName() string {
return td.FromName.String()
}

// CurTableName returns the most recent TableName of the table.
func (td TableDelta) CurTableName() doltdb.TableName {
if td.ToName.Name != "" {
return td.ToName
}
return td.FromName
}

func (td TableDelta) HasFKChanges() bool {
if len(td.FromFks) != len(td.ToFks) {
return true
Expand Down
31 changes: 15 additions & 16 deletions go/libraries/doltcore/doltdb/system_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,48 +93,47 @@ func HasDoltCIPrefix(s string) bool {

// IsFullTextTable returns a boolean stating whether the given table is one of the pseudo-index tables used by Full-Text
// indexes.
// TODO: Schema name
func IsFullTextTable(name string) bool {
return HasDoltPrefix(name) && (strings.HasSuffix(name, "_fts_config") ||
func IsFullTextTable(name string, isSystemTable bool) bool {
return isSystemTable && (strings.HasSuffix(name, "_fts_config") ||
strings.HasSuffix(name, "_fts_position") ||
strings.HasSuffix(name, "_fts_doc_count") ||
strings.HasSuffix(name, "_fts_global_count") ||
strings.HasSuffix(name, "_fts_row_count"))
}

// IsDoltCITable returns whether the table name given is a dolt-ci table
func IsDoltCITable(name string) bool {
return HasDoltCIPrefix(name) && set.NewStrSet(getWriteableSystemTables()).Contains(name) && !IsFullTextTable(name)
func IsDoltCITable(name string, isSystemTable bool) bool {
return HasDoltCIPrefix(name) && set.NewStrSet(getWriteableSystemTables()).Contains(name) && !IsFullTextTable(name, isSystemTable)
}

// IsReadOnlySystemTable returns whether the table name given is a system table that should not be included in command line
// output (e.g. dolt status) by default.
func IsReadOnlySystemTable(name string) bool {
return HasDoltPrefix(name) && !set.NewStrSet(getWriteableSystemTables()).Contains(name) && !IsFullTextTable(name)
func IsReadOnlySystemTable(name string, isSystemTable bool) bool {
return isSystemTable && !set.NewStrSet(getWriteableSystemTables()).Contains(name) && !IsFullTextTable(name, isSystemTable)
}

// IsNonAlterableSystemTable returns whether the table name given is a system table that cannot be dropped or altered
// by the user.
func IsNonAlterableSystemTable(name string) bool {
return (IsReadOnlySystemTable(name) && !IsFullTextTable(name)) || strings.EqualFold(name, SchemasTableName)
func IsNonAlterableSystemTable(name string, isSystemTable bool) bool {
return (IsReadOnlySystemTable(name, isSystemTable) && !IsFullTextTable(name, isSystemTable)) || strings.EqualFold(name, GetSchemasTableName())
}

// GetNonSystemTableNames gets non-system table names
func GetNonSystemTableNames(ctx context.Context, root RootValue) ([]string, error) {
func GetNonSystemTableNames(ctx context.Context, root RootValue, isSystemTableFn func(n string) bool) ([]string, error) {
tn, err := root.GetTableNames(ctx, DefaultSchemaName)
if err != nil {
return nil, err
}
tn = funcitr.FilterStrings(tn, func(n string) bool {
return !HasDoltPrefix(n) && !HasDoltCIPrefix(n)
return !isSystemTableFn(n) && !HasDoltCIPrefix(n)
})
sort.Strings(tn)
return tn, nil
}

// GetSystemTableNames gets system table names
func GetSystemTableNames(ctx context.Context, root RootValue) ([]string, error) {
p, err := GetPersistedSystemTables(ctx, root)
func GetSystemTableNames(ctx context.Context, root RootValue, isSystemTableFn func(n string) bool) ([]string, error) {
p, err := GetPersistedSystemTables(ctx, root, isSystemTableFn)
if err != nil {
return nil, err
}
Expand All @@ -151,13 +150,13 @@ func GetSystemTableNames(ctx context.Context, root RootValue) ([]string, error)
}

// GetPersistedSystemTables returns table names of all persisted system tables.
func GetPersistedSystemTables(ctx context.Context, root RootValue) ([]string, error) {
func GetPersistedSystemTables(ctx context.Context, root RootValue, isSystemTableFn func(n string) bool) ([]string, error) {
tn, err := root.GetTableNames(ctx, DefaultSchemaName)
if err != nil {
return nil, err
}
sort.Strings(tn)
return funcitr.FilterStrings(tn, HasDoltPrefix), nil
return funcitr.FilterStrings(tn, isSystemTableFn), nil
}

// GetGeneratedSystemTables returns table names of all generated system tables.
Expand All @@ -183,7 +182,7 @@ var getWriteableSystemTables = func() []string {
return []string{
GetDocTableName(),
DoltQueryCatalogTableName,
SchemasTableName,
GetSchemasTableName(),
GetProceduresTableName(),
IgnoreTableName,
RebaseTableName,
Expand Down
11 changes: 6 additions & 5 deletions go/libraries/doltcore/merge/fulltext_rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ type rebuildableFulltextTable struct {
Schema schema.Schema
}

// rebuildFullTextIndexes scans the mergedRoot and rebuilds all of the pseudo-index tables that were modified by both
// roots (ours and theirs), or had parents that were modified by both roots.
// rebuildFullTextIndexes scans the mergedRoot and rebuilds all of the
// pseudo-index tables that were modified by both roots (ours and theirs), or
// had parents that were modified by both roots.
func rebuildFullTextIndexes(ctx *sql.Context, mergedRoot, ourRoot, theirRoot doltdb.RootValue, visitedTables map[string]struct{}) (doltdb.RootValue, error) {
// Grab a list of all tables on the root
allTableNames, err := mergedRoot.GetTableNames(ctx, doltdb.DefaultSchemaName)
Expand Down Expand Up @@ -75,7 +76,7 @@ func rebuildFullTextIndexes(ctx *sql.Context, mergedRoot, ourRoot, theirRoot dol
// involved in an actual three-way merge and the full-text index
// pseudo-tables could be out of date.
for _, tblName := range allTableNames {
if doltdb.IsFullTextTable(tblName) {
if doltdb.IsFullTextTable(tblName, doltdb.HasDoltPrefix(tblName)) {
continue
}
// Add this table to the non-deletion set tables, since it's not a pseudo-index table.
Expand Down Expand Up @@ -121,7 +122,7 @@ func rebuildFullTextIndexes(ctx *sql.Context, mergedRoot, ourRoot, theirRoot dol

// Our last loop removes any orphaned pseudo-index tables
for _, tblName := range allTableNames {
if _, doNotDelete := doNotDeleteTables[tblName]; doNotDelete || !doltdb.IsFullTextTable(tblName) {
if _, doNotDelete := doNotDeleteTables[tblName]; doNotDelete || !doltdb.IsFullTextTable(tblName, doltdb.HasDoltPrefix(tblName)) {
continue
}
// TODO: schema name
Expand Down Expand Up @@ -313,7 +314,7 @@ func createRowIterForTable(ctx *sql.Context, t *doltdb.Table, sch schema.Schema)
// Also ignores Full-Text config tables. Returns the updated root with the tables purged.
func purgeFulltextTableData(ctx *sql.Context, root doltdb.RootValue, tableNames ...string) (doltdb.RootValue, error) {
for _, tableName := range tableNames {
if !doltdb.IsFullTextTable(tableName) {
if !doltdb.IsFullTextTable(tableName, doltdb.HasDoltPrefix(tableName)) {
continue
} else if strings.HasSuffix(tableName, "config") {
// We don't want to purge the config table, we'll just roll with whatever is there for now
Expand Down
4 changes: 2 additions & 2 deletions go/libraries/doltcore/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func MergeRoots(
for _, tblName := range tblNames {
mergedTable, stats, err := merger.MergeTable(ctx, tblName, opts, mergeOpts)

if errors.Is(ErrTableDeletedAndModified, err) && doltdb.IsFullTextTable(tblName.Name) {
if errors.Is(ErrTableDeletedAndModified, err) && doltdb.IsFullTextTable(tblName.Name, doltdb.HasDoltPrefix(tblName.Name)) {
// If a Full-Text table was both modified and deleted, then we want to ignore the deletion.
// If there's a true conflict, then the parent table will catch the conflict.
stats = &MergeStats{Operation: TableModified}
Expand All @@ -277,7 +277,7 @@ func MergeRoots(
if stats.Operation != TableUnmodified {
visitedTables[tblName.Name] = struct{}{}
}
if doltdb.IsFullTextTable(tblName.Name) && (stats.Operation == TableModified || stats.Operation == TableRemoved) {
if doltdb.IsFullTextTable(tblName.Name, doltdb.HasDoltPrefix(tblName.Name)) && (stats.Operation == TableModified || stats.Operation == TableRemoved) {
// We handle removal and modification later in the rebuilding process, so we'll skip those.
// We do not handle adding new tables, so we allow that to proceed.
continue
Expand Down
Loading

0 comments on commit 7a8d094

Please sign in to comment.