mirror of
https://github.com/dolthub/dolt.git
synced 2025-12-30 16:12:39 -06:00
Merge pull request #8634 from dolthub/taylor/dg-types
Add pointers for schemas for `dolt_workspace_*` and `dolt_diff` system tables for doltgres
This commit is contained in:
@@ -86,18 +86,26 @@ func (dt *UnscopedDiffTable) String() string {
|
||||
return dt.tableName
|
||||
}
|
||||
|
||||
func getUnscopedDoltDiffSchema(dbName, tableName string) sql.Schema {
|
||||
return []*sql.Column{
|
||||
{Name: "commit_hash", Type: types.Text, Source: tableName, PrimaryKey: true, DatabaseSource: dbName},
|
||||
{Name: "table_name", Type: types.Text, Source: tableName, PrimaryKey: true, DatabaseSource: dbName},
|
||||
{Name: "committer", Type: types.Text, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
|
||||
{Name: "email", Type: types.Text, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
|
||||
{Name: "date", Type: types.Datetime, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
|
||||
{Name: "message", Type: types.Text, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
|
||||
{Name: "data_change", Type: types.Boolean, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
|
||||
{Name: "schema_change", Type: types.Boolean, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
|
||||
}
|
||||
}
|
||||
|
||||
// GetUnscopedDoltDiffSchema returns the schema of the dolt_diff system table. This is used
|
||||
// by Doltgres to update the dolt_diff schema using Doltgres types.
|
||||
var GetUnscopedDoltDiffSchema = getUnscopedDoltDiffSchema
|
||||
|
||||
// Schema is a sql.Table interface function that returns the sql.Schema for this system table.
|
||||
func (dt *UnscopedDiffTable) Schema() sql.Schema {
|
||||
return []*sql.Column{
|
||||
{Name: "commit_hash", Type: types.Text, Source: dt.tableName, PrimaryKey: true, DatabaseSource: dt.dbName},
|
||||
{Name: "table_name", Type: types.Text, Source: dt.tableName, PrimaryKey: true, DatabaseSource: dt.dbName},
|
||||
{Name: "committer", Type: types.Text, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
|
||||
{Name: "email", Type: types.Text, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
|
||||
{Name: "date", Type: types.Datetime, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
|
||||
{Name: "message", Type: types.Text, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
|
||||
{Name: "data_change", Type: types.Boolean, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
|
||||
{Name: "schema_change", Type: types.Boolean, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
|
||||
}
|
||||
return GetUnscopedDoltDiffSchema(dt.dbName, dt.tableName)
|
||||
}
|
||||
|
||||
// Collation implements the sql.Table interface.
|
||||
|
||||
@@ -21,7 +21,8 @@ import (
|
||||
"io"
|
||||
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
sqltypes "github.com/dolthub/go-mysql-server/sql/types"
|
||||
gmstypes "github.com/dolthub/go-mysql-server/sql/types"
|
||||
"github.com/dolthub/vitess/go/sqltypes"
|
||||
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/diff"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
|
||||
@@ -406,7 +407,10 @@ func NewWorkspaceTable(ctx *sql.Context, workspaceTableName string, tableName do
|
||||
fromSch = toSch
|
||||
}
|
||||
|
||||
totalSch, err := workspaceSchema(fromSch, toSch)
|
||||
sch := sql.NewPrimaryKeySchema(GetDoltWorkspaceBaseSqlSchema())
|
||||
baseDoltSch, err := sqlutil.ToDoltSchema(ctx, head, tableName, sch, head, sql.Collation_Default)
|
||||
|
||||
totalSch, err := workspaceSchema(fromSch, toSch, baseDoltSch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -438,9 +442,21 @@ func (wt *WorkspaceTable) Schema() sql.Schema {
|
||||
return wt.sqlSchema
|
||||
}
|
||||
|
||||
// CalculateDiffSchema returns the schema for the dolt_diff table based on the schemas from the from and to tables.
|
||||
// GetDoltWorkspaceBaseSqlSchema returns the base schema for the dolt_workspace_* system table.
|
||||
// This is used by Doltgres to update the dolt_workspace_* schema using Doltgres types.
|
||||
var GetDoltWorkspaceBaseSqlSchema = getDoltWorkspaceBaseSqlSchema
|
||||
|
||||
func getDoltWorkspaceBaseSqlSchema() sql.Schema {
|
||||
return []*sql.Column{
|
||||
{Name: "id", Type: gmstypes.Uint64, PrimaryKey: true, Nullable: false},
|
||||
{Name: "staged", Type: gmstypes.Boolean, Nullable: false},
|
||||
{Name: "diff_type", Type: gmstypes.MustCreateStringWithDefaults(sqltypes.VarChar, 1023), Nullable: false},
|
||||
}
|
||||
}
|
||||
|
||||
// workspaceSchema returns the schema for the dolt_workspace table based on the schemas from the from and to tables.
|
||||
// Either may be nil, in which case the nil argument will use the schema of the non-nil argument
|
||||
func workspaceSchema(fromSch, toSch schema.Schema) (schema.Schema, error) {
|
||||
func workspaceSchema(fromSch, toSch, baseSch schema.Schema) (schema.Schema, error) {
|
||||
if fromSch == nil && toSch == nil {
|
||||
return nil, errors.New("Runtime error:non-nil argument required to CalculateDiffSchema")
|
||||
} else if fromSch == nil {
|
||||
@@ -449,13 +465,10 @@ func workspaceSchema(fromSch, toSch schema.Schema) (schema.Schema, error) {
|
||||
toSch = fromSch
|
||||
}
|
||||
|
||||
cols := make([]schema.Column, 0, 3+toSch.GetAllCols().Size()+fromSch.GetAllCols().Size())
|
||||
|
||||
cols = append(cols,
|
||||
schema.NewColumn("id", 0, types.UintKind, true, schema.NotNullConstraint{}),
|
||||
schema.NewColumn("staged", 0, types.BoolKind, false, schema.NotNullConstraint{}),
|
||||
schema.NewColumn("diff_type", 0, types.StringKind, false, schema.NotNullConstraint{}),
|
||||
)
|
||||
baseColColl := baseSch.GetAllCols()
|
||||
baseCols := baseColColl.GetColumns()
|
||||
cols := make([]schema.Column, 0, baseColColl.Size()+toSch.GetAllCols().Size()+fromSch.GetAllCols().Size())
|
||||
cols = append(cols, baseCols...)
|
||||
|
||||
transformer := func(sch schema.Schema, namer func(string) string) error {
|
||||
return sch.GetAllCols().Iter(func(tag uint64, col schema.Column) (stop bool, err error) {
|
||||
@@ -643,7 +656,7 @@ func getWorkspaceTableRow(
|
||||
|
||||
row = make(sql.Row, 3+tLen+fLen)
|
||||
|
||||
row[0] = rowId
|
||||
row[0] = int64(rowId)
|
||||
row[1] = staged
|
||||
row[2] = diffTypeString(dif)
|
||||
|
||||
@@ -868,10 +881,9 @@ func (e emptyWorkspaceTable) String() string {
|
||||
}
|
||||
|
||||
func (e emptyWorkspaceTable) Schema() sql.Schema {
|
||||
return []*sql.Column{
|
||||
{Name: "id", Type: sqltypes.Int32, Nullable: false},
|
||||
{Name: "staged", Type: sqltypes.Boolean, Nullable: false},
|
||||
}
|
||||
sch := GetDoltWorkspaceBaseSqlSchema()
|
||||
// Only return the "id" and "staged" columns.
|
||||
return sch[0:2]
|
||||
}
|
||||
|
||||
func (e emptyWorkspaceTable) Collation() sql.CollationID { return sql.Collation_Default }
|
||||
|
||||
@@ -20,6 +20,26 @@ import (
|
||||
)
|
||||
|
||||
var DoltWorkspaceScriptTests = []queries.ScriptTest{
|
||||
{
|
||||
Name: "dolt_workspace_* empty table",
|
||||
SetUpScript: []string{
|
||||
"create table tbl (pk int primary key, val int);",
|
||||
"call dolt_commit('-Am', 'creating table t');",
|
||||
},
|
||||
Assertions: []queries.ScriptTestAssertion{
|
||||
{
|
||||
Query: "select * from dolt_workspace_tbl",
|
||||
Expected: []sql.Row{},
|
||||
},
|
||||
{
|
||||
Query: "describe dolt_workspace_tbl",
|
||||
Expected: []sql.Row{
|
||||
{"id", "bigint unsigned", "NO", "PRI", nil, ""},
|
||||
{"staged", "tinyint(1)", "NO", "", nil, ""},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "dolt_workspace_* multiple edits of a single row",
|
||||
SetUpScript: []string{
|
||||
@@ -40,6 +60,18 @@ var DoltWorkspaceScriptTests = []queries.ScriptTest{
|
||||
{0, true, "modified", 42, 51, 42, 42},
|
||||
},
|
||||
},
|
||||
{
|
||||
Query: "describe dolt_workspace_tbl",
|
||||
Expected: []sql.Row{
|
||||
{"id", "bigint unsigned", "NO", "PRI", nil, ""},
|
||||
{"staged", "tinyint(1)", "NO", "", nil, ""},
|
||||
{"diff_type", "varchar(1023)", "NO", "", nil, ""},
|
||||
{"to_pk", "int", "YES", "", nil, ""},
|
||||
{"to_val", "int", "YES", "", nil, ""},
|
||||
{"from_pk", "int", "YES", "", nil, ""},
|
||||
{"from_val", "int", "YES", "", nil, ""},
|
||||
},
|
||||
},
|
||||
{
|
||||
// Test case-insensitive table name
|
||||
Query: "select * from dolt_workspace_TBL",
|
||||
|
||||
Reference in New Issue
Block a user