Skip to content

Commit

Permalink
Merge pull request #994 from dolthub/taylor/rebase-ignore
Browse files Browse the repository at this point in the history
Tests and fixes for dolt_rebase and dolt_ignore
  • Loading branch information
tbantle22 authored Nov 22, 2024
2 parents 6335fdf + 12769b4 commit 02d5011
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/cockroachdb/apd/v2 v2.0.3-0.20200518165714-d020e156310a
github.com/cockroachdb/errors v1.7.5
github.com/dolthub/dolt/go v0.40.5-0.20241120194629-dd444f46ef9c
github.com/dolthub/dolt/go v0.40.5-0.20241122183655-9c1cc4c67583
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20241119094239-f4e529af734d
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dolthub/dolt/go v0.40.5-0.20241120194629-dd444f46ef9c h1:DLSUUsGpUYCbzTBznjx2cfDVBy8DxIVhOf3bn4CvmRE=
github.com/dolthub/dolt/go v0.40.5-0.20241120194629-dd444f46ef9c/go.mod h1:aFv4w3D2nxTV1GD5OJ4/CEOnleES7E9AwLj59fjIxBo=
github.com/dolthub/dolt/go v0.40.5-0.20241122183655-9c1cc4c67583 h1:dw0mBCmqoDFvh66OhTak5i1mwqpZzQ9B5cIi2VIQsSQ=
github.com/dolthub/dolt/go v0.40.5-0.20241122183655-9c1cc4c67583/go.mod h1:aFv4w3D2nxTV1GD5OJ4/CEOnleES7E9AwLj59fjIxBo=
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20241119094239-f4e529af734d h1:gO9+wrmNHXukPNCO1tpfCcXIdMlW/qppbUStfLvqz/U=
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20241119094239-f4e529af734d/go.mod h1:L5RDYZbC9BBWmoU2+TjTekeqqhFXX5EqH9ln00O0stY=
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 h1:u3PMzfF8RkKd3lB9pZ2bfn0qEG+1Gms9599cr0REMww=
Expand Down
57 changes: 57 additions & 0 deletions server/tables/dtables/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dtables

import (
"fmt"

"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/store/val"
"github.com/dolthub/go-mysql-server/sql"

pgtypes "github.com/dolthub/doltgresql/server/types"
)

// getDoltIgnoreSchema returns the schema for the dolt_ignore table.
func getDoltIgnoreSchema() sql.Schema {
return []*sql.Column{
{Name: "pattern", Type: pgtypes.Text, Source: doltdb.IgnoreTableName, PrimaryKey: true},
{Name: "ignored", Type: pgtypes.Bool, Source: doltdb.IgnoreTableName, PrimaryKey: false, Nullable: false},
}
}

// convertTupleToIgnoreBoolean reads a boolean from a tuple and returns it.
func convertTupleToIgnoreBoolean(valueDesc val.TupleDesc, valueTuple val.Tuple) (bool, error) {
extendedTuple := val.NewTupleDescriptorWithArgs(
val.TupleDescriptorArgs{Comparator: valueDesc.Comparator(), Handlers: valueDesc.Handlers},
val.Type{Enc: val.ExtendedEnc, Nullable: false},
)
if !valueDesc.Equals(extendedTuple) {
return false, fmt.Errorf("dolt_ignore had unexpected value type, this should never happen")
}
extended, ok := valueDesc.GetExtended(0, valueTuple)
if !ok {
return false, fmt.Errorf("could not read boolean")
}
val, err := valueDesc.Handlers[0].DeserializeValue(extended)
if err != nil {
return false, err
}
ignore, ok := val.(bool)
if !ok {
return false, fmt.Errorf("could not read boolean")
}
return ignore, nil
}
9 changes: 9 additions & 0 deletions server/tables/dtables/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package dtables

import (
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dprocedures"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dtables"
)

Expand All @@ -40,6 +42,13 @@ func Init() {

// Schemas
dtables.GetDocsSchema = getDocsSchema
dtables.GetDoltIgnoreSchema = getDoltIgnoreSchema
dprocedures.GetDoltRebaseSystemTableSchema = getRebaseSchema

// Conversions
doltdb.ConvertTupleToIgnoreBoolean = convertTupleToIgnoreBoolean
sqle.ConvertRebasePlanStepToRow = convertRebasePlanStepToRow
sqle.ConvertRowToRebasePlanStep = convertRowToRebasePlanStep
}

// getBranchesTableName returns the name of the branches table.
Expand Down
77 changes: 77 additions & 0 deletions server/tables/dtables/rebase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dtables

import (
"fmt"
"strings"

"github.com/dolthub/dolt/go/libraries/doltcore/rebase"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dprocedures"
"github.com/dolthub/go-mysql-server/sql"
"github.com/shopspring/decimal"

pgtypes "github.com/dolthub/doltgresql/server/types"
)

// getRebaseSchema returns the schema for the rebase table.
func getRebaseSchema() sql.Schema {
return []*sql.Column{
{Name: "rebase_order", Type: pgtypes.Float32, Nullable: false, PrimaryKey: true}, // TODO: cannot have numeric key
{Name: "action", Type: pgtypes.VarCharType{MaxChars: 6}, Nullable: false}, // TODO: Should be enum(pick, squash, fixup, drop, reword)
{Name: "commit_hash", Type: pgtypes.Text, Nullable: false},
{Name: "commit_message", Type: pgtypes.Text, Nullable: false},
}
}

// convertRebasePlanStepToRow converts a RebasePlanStep to a sql.Row.
func convertRebasePlanStepToRow(planMember rebase.RebasePlanStep) (sql.Row, error) {
actionEnumValue := dprocedures.RebaseActionEnumType.IndexOf(strings.ToLower(planMember.Action))
if actionEnumValue == -1 {
return nil, fmt.Errorf("invalid rebase action: %s", planMember.Action)
}

return sql.Row{
planMember.RebaseOrderAsFloat(),
planMember.Action,
planMember.CommitHash,
planMember.CommitMsg,
}, nil
}

// convertRowToRebasePlanStep converts a sql.Row to a RebasePlanStep.
func convertRowToRebasePlanStep(row sql.Row) (rebase.RebasePlanStep, error) {
order, ok := row[0].(float32)
if !ok {
return rebase.RebasePlanStep{}, fmt.Errorf("invalid order value in rebase plan: %v (%T)", row[0], row[0])
}

rebaseAction, ok := row[1].(string)
if !ok {
return rebase.RebasePlanStep{}, fmt.Errorf("invalid enum value in rebase plan: %v (%T)", row[1], row[1])
}

rebaseIdx := dprocedures.RebaseActionEnumType.IndexOf(rebaseAction)
if rebaseIdx < 0 {
return rebase.RebasePlanStep{}, fmt.Errorf("invalid enum value in rebase plan: %v (%T)", row[1], row[1])
}

return rebase.RebasePlanStep{
RebaseOrder: decimal.NewFromFloat32(order),
Action: rebaseAction,
CommitHash: row[2].(string),
CommitMsg: row[3].(string),
}, nil
}
143 changes: 143 additions & 0 deletions testing/go/dolt_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,53 @@ func TestUserSpaceDoltTables(t *testing.T) {
},
},
},
{
Name: "dolt ignore",
SetUpScript: []string{},
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM dolt_ignore`,
Expected: []sql.Row{},
},
{
Query: "INSERT INTO dolt_ignore VALUES ('generated_*', true), ('generated_exception', false)",
Expected: []sql.Row{},
},
{
Query: `SELECT * FROM dolt_ignore`,
Expected: []sql.Row{
{"generated_*", "t"},
{"generated_exception", "f"},
},
},
{
Query: "CREATE TABLE foo (pk int);",
Expected: []sql.Row{},
},
{
Query: "CREATE TABLE generated_foo (pk int);",
Expected: []sql.Row{},
},
{
Query: "CREATE TABLE generated_exception (pk int);",
Expected: []sql.Row{},
},
{
Query: "SELECT dolt_add('-A');",
Expected: []sql.Row{{"{0}"}},
},
{
Query: "SELECT * FROM dolt_status;",
Expected: []sql.Row{
{"dolt_ignore", 1, "new table"},
{"public.foo", 1, "new table"},
{"public.generated_exception", 1, "new table"},
{"public.generated_foo", 0, "new table"},
},
},
// TODO: Test tables in different schemas
},
},
{
Name: "dolt log",
Assertions: []ScriptTestAssertion{
Expand Down Expand Up @@ -1648,6 +1695,102 @@ func TestUserSpaceDoltTables(t *testing.T) {
},
},
},
{
Name: "dolt rebase",
SetUpScript: []string{
// create a simple table
"create table t (pk int primary key);",
"select dolt_commit('-Am', 'creating table t');",

// create a new branch that we'll add more commits to later
"select dolt_branch('branch1');",

// create another commit on the main branch, right after where branch1 branched off
"insert into t values (0);",
"select dolt_commit('-am', 'inserting row 0');",

// switch to branch1 and create three more commits that each insert one row
"select dolt_checkout('branch1');",
"insert into t values (1);",
"select dolt_commit('-am', 'inserting row 1');",
"insert into t values (2);",
"select dolt_commit('-am', 'inserting row 2');",
"insert into t values (3);",
"select dolt_commit('-am', 'inserting row 3');",
},
Assertions: []ScriptTestAssertion{
{
Query: "select message from dolt_log;",
Expected: []sql.Row{
{"inserting row 3"},
{"inserting row 2"},
{"inserting row 1"},
{"creating table t"},
{"CREATE DATABASE"},
{"Initialize data repository"},
},
},
{
Query: `select dolt_rebase('-i', 'main');`,
Expected: []sql.Row{{"{0,\"interactive rebase started on branch dolt_rebase_branch1; adjust the rebase plan in the dolt_rebase table, then continue rebasing by calling dolt_rebase('--continue')\"}"}},
},
{
Query: "select rebase_order, action, commit_message from dolt_rebase order by rebase_order;",
Expected: []sql.Row{
{float64(1), "pick", "inserting row 1"},
{float64(2), "pick", "inserting row 2"},
{float64(3), "pick", "inserting row 3"},
},
},
{
Skip: true, // TODO: Support dolt.rebase
Query: "select rebase_order, action, commit_message from dolt.rebase order by rebase_order;",
Expected: []sql.Row{
{float64(1), "pick", "inserting row 1"},
{float64(2), "pick", "inserting row 2"},
{float64(3), "pick", "inserting row 3"},
},
},
{
Query: "update dolt_rebase set action='reword', commit_message='insert rows' where rebase_order=1;",
Expected: []sql.Row{},
},
{
Query: "update dolt_rebase set action='drop' where rebase_order=2;",
Expected: []sql.Row{},
},
{
Query: "update dolt_rebase set action='fixup' where rebase_order=3;",
Expected: []sql.Row{},
},
{
Query: "update dolt_rebase set action='fixup' where rebase_order=3;",
Expected: []sql.Row{},
},
{
Query: "select dolt_rebase('--continue');",
Expected: []sql.Row{{"{0,\"Successfully rebased and updated refs/heads/branch1\"}"}},
},
{
Query: "select message from dolt_log;",
Expected: []sql.Row{
{"insert rows"},
{"inserting row 0"},
{"creating table t"},
{"CREATE DATABASE"},
{"Initialize data repository"},
},
},
{
Query: "select * from dolt_rebase;",
ExpectedErr: "table not found: dolt_rebase",
},
{
Query: "select * from dolt.rebase;",
ExpectedErr: "table not found: rebase",
},
},
},
{
Name: "dolt remote branches",
Assertions: []ScriptTestAssertion{
Expand Down

0 comments on commit 02d5011

Please sign in to comment.