Skip to content

Commit

Permalink
Merge pull request #8582 from dolthub/taylor/dolt-rebase
Browse files Browse the repository at this point in the history
Support dolt.rebase for doltgres
  • Loading branch information
tbantle22 authored Nov 22, 2024
2 parents 60f1fff + 1323151 commit 4ad1957
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 36 deletions.
2 changes: 1 addition & 1 deletion go/cmd/dolt/commands/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ 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)) {
if !doltdb.IsReadOnlySystemTable(doltdb.TableName{Name: row[0].(string)}) {
switch row[1].(string) {
case "new table":
lines = append(lines, fmt.Sprintf(statusFmt, tblDiffTypeToLabel[diff.AddedTable], row[0].(string)))
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/dolt/commands/status.go
Original file line number Diff line number Diff line change
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(doltdb.TableName{Name: 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(doltdb.TableName{Name: tableName}) {
return commands.HandleVErrAndExitCode(
errhand.BuildDError("error removing table %s", tableName).AddCause(doltdb.ErrSystemTableCannotBeModified).Build(), usage)
}
Expand Down
9 changes: 8 additions & 1 deletion go/libraries/doltcore/doltdb/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,17 @@ func resolveConflictingPatterns(trueMatches, falseMatches []string, tableName Ta
return IgnorePatternConflict, DoltIgnoreConflictError{Table: tableName, TruePatterns: conflictingTrueMatches, FalsePatterns: conflictingFalseMatches}
}

func isDoltRebaseTable(tableName TableName) bool {
if strings.EqualFold(tableName.Name, RebaseTableName) {
return true
}
return tableName.Schema == DoltNamespace && tableName.Name == GetRebaseTableName()
}

func (ip *IgnorePatterns) IsTableNameIgnored(tableName TableName) (IgnoreResult, error) {
// The dolt_rebase table is automatically ignored by Dolt – it shouldn't ever
// be checked in to a Dolt database.
if strings.EqualFold(tableName.Name, RebaseTableName) {
if isDoltRebaseTable(tableName) {
return Ignore, nil
}

Expand Down
26 changes: 18 additions & 8 deletions go/libraries/doltcore/doltdb/system_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,26 @@ func IsFullTextTable(name string) bool {
strings.HasSuffix(name, "_fts_row_count"))
}

// IsDoltCITable returns whether the table name given is a dolt-ci table
// 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)
}

// IsSystemTable returns whether the table name given is a Dolt system table.
func IsSystemTable(name TableName) bool {
return HasDoltPrefix(name.Name) || strings.EqualFold(name.Schema, DoltNamespace)
}

// 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 TableName) bool {
return IsSystemTable(name) && !set.NewStrSet(getWriteableSystemTables()).Contains(name.Name) && !IsFullTextTable(name.Name)
}

// 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 TableName) bool {
return (IsReadOnlySystemTable(name) && !IsFullTextTable(name.Name)) || strings.EqualFold(name.Name, SchemasTableName)
}

// GetNonSystemTableNames gets non-system table names
Expand Down Expand Up @@ -186,10 +191,10 @@ var getWriteableSystemTables = func() []string {
SchemasTableName,
ProceduresTableName,
IgnoreTableName,
RebaseTableName,
GetRebaseTableName(),

// todo: find way to make these writable by the dolt process
// todo: but not by user
// TODO: find way to make these writable by the dolt process
// TODO: but not by user
WorkflowsTableName,
WorkflowEventsTableName,
WorkflowEventTriggersTableName,
Expand Down Expand Up @@ -346,6 +351,11 @@ var GetMergeStatusTableName = func() string {
return MergeStatusTableName
}

// GetRebaseTableName returns the rebase system table name
var GetRebaseTableName = func() string {
return RebaseTableName
}

// GetRemoteBranchesTableName returns the all-branches system table name
var GetRemoteBranchesTableName = func() string {
return RemoteBranchesTableName
Expand Down
45 changes: 30 additions & 15 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (db Database) GetTableInsensitiveAsOf(ctx *sql.Context, tableName string, a
return nil, false, nil
}

if doltdb.IsReadOnlySystemTable(tableName) {
if doltdb.IsReadOnlySystemTable(doltdb.TableName{Name: tableName, Schema: db.schemaName}) {
// currently, system tables do not need to be "locked to root"
// see comment below in getTableInsensitive
return table, ok, nil
Expand Down Expand Up @@ -661,7 +661,7 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
}
if !resolve.UseSearchPath || isDoltgresSystemTable {
if resolve.UseSearchPath && lwrName == doltdb.DocTableName {
db.schemaName = "dolt"
db.schemaName = doltdb.DoltNamespace
}
backingTable, _, err := db.getTable(ctx, root, doltdb.GetDocTableName())
if err != nil {
Expand Down Expand Up @@ -701,6 +701,12 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
return dt, found, nil
}

// Converts dolt_rebase to dolt.rebase for doltgres compatibility
if resolve.UseSearchPath && lwrName == doltdb.RebaseTableName {
db.schemaName = doltdb.DoltNamespace
tblName = doltdb.GetRebaseTableName()
}

// TODO: this should reuse the root, not lookup the db state again
table, found, err := db.getTable(ctx, root, tblName)
if err != nil {
Expand Down Expand Up @@ -1032,12 +1038,14 @@ func (db Database) newDoltTable(tableName string, sch schema.Schema, tbl *doltdb
if err != nil {
return nil, err
}

tname := doltdb.TableName{Name: tableName, Schema: db.schemaName}
var table sql.Table
if doltdb.IsReadOnlySystemTable(tableName) {
if doltdb.IsReadOnlySystemTable(tname) {
table = readonlyTable
} else if doltdb.IsDoltCITable(tableName) && !doltdb.IsFullTextTable(tableName) {
table = &AlterableDoltTable{WritableDoltTable{DoltTable: readonlyTable, db: db}}
} else if doltdb.HasDoltPrefix(tableName) && !doltdb.IsFullTextTable(tableName) {
} else if doltdb.IsSystemTable(tname) && !doltdb.IsFullTextTable(tableName) {
table = &WritableDoltTable{DoltTable: readonlyTable, db: db}
} else {
table = &AlterableDoltTable{WritableDoltTable{DoltTable: readonlyTable, db: db}}
Expand Down Expand Up @@ -1124,7 +1132,7 @@ func filterDoltInternalTables(ctx *sql.Context, tblNames []string, schemaName st
if doltdb.DoltCICanBypass(ctx) {
result = append(result, tbl)
}
} else if !doltdb.HasDoltPrefix(tbl) && schemaName != "dolt" {
} else if !doltdb.IsSystemTable(doltdb.TableName{Name: tbl, Schema: schemaName}) {
result = append(result, tbl)
}
}
Expand Down Expand Up @@ -1189,7 +1197,7 @@ func (db Database) DropTable(ctx *sql.Context, tableName string) error {
if err := dsess.CheckAccessForDb(ctx, db, branch_control.Permissions_Write); err != nil {
return err
}
if doltdb.IsNonAlterableSystemTable(tableName) {
if doltdb.IsNonAlterableSystemTable(doltdb.TableName{Name: tableName, Schema: db.schemaName}) {
return ErrSystemTableAlter.New(tableName)
}

Expand Down Expand Up @@ -1306,7 +1314,7 @@ func (db Database) CreateTable(ctx *sql.Context, tableName string, sch sql.Prima
return err
}

if doltdb.HasDoltPrefix(tableName) && !doltdb.IsFullTextTable(tableName) {
if doltdb.IsSystemTable(doltdb.TableName{Name: tableName, Schema: db.schemaName}) && !doltdb.IsFullTextTable(tableName) {
return ErrReservedTableName.New(tableName)
}

Expand All @@ -1333,7 +1341,7 @@ func (db Database) CreateIndexedTable(ctx *sql.Context, tableName string, sch sq
return err
}

if doltdb.HasDoltPrefix(tableName) {
if doltdb.IsSystemTable(doltdb.TableName{Name: tableName, Schema: db.schemaName}) {
return ErrReservedTableName.New(tableName)
}

Expand Down Expand Up @@ -1524,7 +1532,7 @@ func (db Database) createDoltTable(ctx *sql.Context, tableName string, schemaNam

// CreateTemporaryTable creates a table that only exists the length of a session.
func (db Database) CreateTemporaryTable(ctx *sql.Context, tableName string, pkSch sql.PrimaryKeySchema, collation sql.CollationID) error {
if doltdb.HasDoltPrefix(tableName) {
if doltdb.IsSystemTable(doltdb.TableName{Name: tableName, Schema: db.schemaName}) {
return ErrReservedTableName.New(tableName)
}

Expand Down Expand Up @@ -1670,11 +1678,11 @@ func (db Database) RenameTable(ctx *sql.Context, oldName, newName string) error
return err
}

if doltdb.IsNonAlterableSystemTable(oldName) {
if doltdb.IsNonAlterableSystemTable(doltdb.TableName{Name: oldName, Schema: db.schemaName}) {
return ErrSystemTableAlter.New(oldName)
}

if doltdb.HasDoltPrefix(newName) {
if doltdb.IsSystemTable(doltdb.TableName{Name: newName, Schema: db.schemaName}) {
return ErrReservedTableName.New(newName)
}

Expand Down Expand Up @@ -2364,7 +2372,10 @@ func convertRowToRebasePlanStep(row sql.Row) (rebase.RebasePlanStep, error) {

// LoadRebasePlan implements the rebase.RebasePlanDatabase interface
func (db Database) LoadRebasePlan(ctx *sql.Context) (*rebase.RebasePlan, error) {
table, ok, err := db.GetTableInsensitive(ctx, doltdb.RebaseTableName)
if resolve.UseSearchPath {
db.schemaName = doltdb.DoltNamespace
}
table, ok, err := db.GetTableInsensitive(ctx, doltdb.GetRebaseTableName())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2421,19 +2432,23 @@ func convertRebasePlanStepToRow(planMember rebase.RebasePlanStep) (sql.Row, erro

// SaveRebasePlan implements the rebase.RebasePlanDatabase interface
func (db Database) SaveRebasePlan(ctx *sql.Context, plan *rebase.RebasePlan) error {
if resolve.UseSearchPath {
db.schemaName = doltdb.DoltNamespace
}

pkSchema := sql.NewPrimaryKeySchema(dprocedures.GetDoltRebaseSystemTableSchema())
// we use createSqlTable, instead of CreateTable to avoid the "dolt_" reserved prefix table name check
err := db.createSqlTable(ctx, doltdb.RebaseTableName, "", pkSchema, sql.Collation_Default, "")
err := db.createSqlTable(ctx, doltdb.GetRebaseTableName(), db.schemaName, pkSchema, sql.Collation_Default, "")
if err != nil {
return err
}

table, ok, err := db.GetTableInsensitive(ctx, doltdb.RebaseTableName)
table, ok, err := db.GetTableInsensitive(ctx, doltdb.GetRebaseTableName())
if err != nil {
return err
}
if !ok {
return fmt.Errorf("unable to find %s table", doltdb.RebaseTableName)
return fmt.Errorf("unable to find %s table", doltdb.GetRebaseTableName())
}

writeableDoltTable, ok := table.(*WritableDoltTable)
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/database_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func (p *DoltDatabaseProvider) CreateCollatedDatabase(ctx *sql.Context, name str
return err
}
workingRoot, err = workingRoot.CreateDatabaseSchema(ctx, schema.DatabaseSchema{
Name: "dolt",
Name: doltdb.DoltNamespace,
})
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dtables/docs_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (iw *docsWriter) Delete(ctx *sql.Context, r sql.Row) error {

func getDoltDocsTableName() doltdb.TableName {
if resolve.UseSearchPath {
return doltdb.TableName{Schema: "dolt", Name: doltdb.GetDocTableName()}
return doltdb.TableName{Schema: doltdb.DoltNamespace, Name: doltdb.GetDocTableName()}
}
return doltdb.TableName{Name: doltdb.GetDocTableName()}
}
Expand Down
4 changes: 2 additions & 2 deletions go/libraries/doltcore/sqle/resolve/search_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func FirstExistingSchemaOnSearchPath(ctx *sql.Context, root doltdb.RootValue) (s

// IsDoltgresSystemTable returns whether a table is a doltgres system table or not
func IsDoltgresSystemTable(ctx *sql.Context, tableName doltdb.TableName, root doltdb.RootValue) (bool, error) {
if tableName.Schema == "dolt" || doltdb.HasDoltPrefix(tableName.Name) {
if doltdb.IsSystemTable(tableName) {
return true, nil
}
if !UseSearchPath || tableName.Schema != "" {
Expand All @@ -93,7 +93,7 @@ func IsDoltgresSystemTable(ctx *sql.Context, tableName doltdb.TableName, root do
return false, nil
}
for _, schemaName := range schemasToSearch {
if schemaName == "dolt" {
if schemaName == doltdb.DoltNamespace {
return true, nil
}

Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3090,7 +3090,7 @@ func (t *WritableDoltTable) updateFromRoot(ctx *sql.Context, root doltdb.RootVal
return fmt.Errorf("table `%s` cannot find itself", t.tableName)
}
var updatedTable *AlterableDoltTable
if doltdb.HasDoltPrefix(t.tableName) && !doltdb.IsReadOnlySystemTable(t.tableName) && !doltdb.IsDoltCITable(t.tableName) {
if doltdb.IsSystemTable(t.TableName()) && !doltdb.IsReadOnlySystemTable(t.TableName()) && !doltdb.IsDoltCITable(t.tableName) {
updatedTable = &AlterableDoltTable{*updatedTableSql.(*WritableDoltTable)}
} else {
updatedTable = updatedTableSql.(*AlterableDoltTable)
Expand Down
9 changes: 5 additions & 4 deletions go/libraries/doltcore/sqle/user_space_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,19 @@ func NewUserSpaceDatabase(root doltdb.RootValue, editOpts editor.Options) *UserS
}

func (db *UserSpaceDatabase) Name() string {
return "dolt"
return doltdb.DoltNamespace
}

func (db *UserSpaceDatabase) Schema() string {
return ""
}

func (db *UserSpaceDatabase) GetTableInsensitive(ctx *sql.Context, tableName string) (sql.Table, bool, error) {
if doltdb.IsReadOnlySystemTable(tableName) {
tname := doltdb.TableName{Name: tableName}
if doltdb.IsReadOnlySystemTable(tname) {
return nil, false, nil
}
table, tableName, ok, err := doltdb.GetTableInsensitive(ctx, db.RootValue, doltdb.TableName{Name: tableName})
table, tableName, ok, err := doltdb.GetTableInsensitive(ctx, db.RootValue, tname)
if err != nil {
return nil, false, err
}
Expand All @@ -76,7 +77,7 @@ func (db *UserSpaceDatabase) GetTableNames(ctx *sql.Context) ([]string, error) {
}
resultingTblNames := []string{}
for _, tbl := range tableNames {
if !doltdb.IsReadOnlySystemTable(tbl) {
if !doltdb.IsReadOnlySystemTable(doltdb.TableName{Name: tbl}) {
resultingTblNames = append(resultingTblNames, tbl)
}
}
Expand Down

0 comments on commit 4ad1957

Please sign in to comment.