Skip to content

Commit d7cf4b6

Browse files
committed
add dolt adapter to standardize table overwrites by integrators
1 parent 7342810 commit d7cf4b6

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

go/libraries/doltcore/doltdb/system_table.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,6 @@ var GetSchemaConflictsTableName = func() string {
367367
return SchemaConflictsTableName
368368
}
369369

370-
// GetStatusTableName returns the status system table name.
371-
var GetStatusTableName = func() string {
372-
return StatusTableName
373-
}
374-
375370
// GetTagsTableName returns the tags table name
376371
var GetTagsTableName = func() string {
377372
return TagsTableName
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package adapters
2+
3+
import (
4+
"github.com/dolthub/go-mysql-server/sql"
5+
6+
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
7+
"github.com/dolthub/dolt/go/libraries/doltcore/env"
8+
)
9+
10+
// TableAdapter provides a hook for extensions to customize or wrap table implementations. For example, this allows
11+
// libraries like Doltgres to intercept system table creation and apply type conversions, schema modifications, or other
12+
// customizations without modifying the core Dolt implementation for their compatibility.
13+
type TableAdapter interface {
14+
// CreateTable creates or wraps a system table. The function receives all necessary parameters to construct the
15+
// table and can either build it from scratch or call the default Dolt constructor and wrap it.
16+
CreateTable(ctx *sql.Context, tableName string, dDb *doltdb.DoltDB, workingSet *doltdb.WorkingSet, rootsProvider env.RootsProvider[*sql.Context]) sql.Table
17+
18+
// TableName returns the preferred name for the adapter's table. This allows extensions to rename tables while
19+
// preserving the underlying implementation. For example, Doltgres uses "status" while Dolt uses "dolt_status",
20+
// enabling cleaner Postgres-style naming.
21+
TableName() string
22+
}
23+
24+
// TableAdapters is a registry for TableAdapter implementations, keyed by table name. It is populated during package
25+
// initialization and intended to be read-only thereafter.
26+
var TableAdapters = make(map[string]TableAdapter)

go/libraries/doltcore/sqle/database.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"github.com/dolthub/dolt/go/libraries/doltcore/rebase"
4545
"github.com/dolthub/dolt/go/libraries/doltcore/ref"
4646
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
47+
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/adapters"
4748
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dprocedures"
4849
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
4950
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dtables"
@@ -747,7 +748,7 @@ func (db Database) getTableInsensitiveWithRoot(ctx *sql.Context, head *doltdb.Co
747748
if !resolve.UseSearchPath || isDoltgresSystemTable {
748749
dt, found = dtables.NewCommitAncestorsTable(ctx, db.Name(), lwrName, db.ddb), true
749750
}
750-
case doltdb.GetStatusTableName(), doltdb.StatusTableName:
751+
case doltdb.StatusTableName, adapters.TableAdapters[doltdb.StatusTableName].TableName():
751752
isDoltgresSystemTable, err := resolve.IsDoltgresSystemTable(ctx, tname, root)
752753
if err != nil {
753754
return nil, false, err
@@ -804,7 +805,12 @@ func (db Database) getTableInsensitiveWithRoot(ctx *sql.Context, head *doltdb.Co
804805
}
805806
}
806807

807-
dt, found = dtables.NewStatusTable(ctx, lwrName, db.ddb, ws, rootsProvider), true
808+
if tableAdapter, ok := adapters.TableAdapters[lwrName]; ok {
809+
dt, found = tableAdapter.CreateTable(ctx, lwrName, db.ddb, ws, rootsProvider), true
810+
}
811+
if !ok {
812+
return nil, false, sql.ErrTableNotFound.New(tblName)
813+
}
808814
}
809815
case doltdb.MergeStatusTableName, doltdb.GetMergeStatusTableName():
810816
isDoltgresSystemTable, err := resolve.IsDoltgresSystemTable(ctx, tname, root)

go/libraries/doltcore/sqle/dtables/status_table.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,35 @@ import (
2525
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
2626
"github.com/dolthub/dolt/go/libraries/doltcore/env"
2727
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
28+
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/adapters"
2829
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/index"
2930
)
3031

3132
const statusDefaultRowCount = 10
3233

34+
func init() {
35+
adapters.TableAdapters[doltdb.StatusTableName] = DoltStatusTableAdapter{}
36+
}
37+
38+
// DoltStatusTableAdapter implements the adapters.TableAdapter interface. It serves as the default dolt_status table
39+
// implementation, applying zero modifications. This exists in case no adapters are initialized by the integrator, i.e.
40+
// Doltgres.
41+
type DoltStatusTableAdapter struct{}
42+
43+
var _ adapters.TableAdapter = DoltStatusTableAdapter{}
44+
45+
// CreateTable implements the adapters.TableAdapter interface. It returns the default constructor for the dolt_status
46+
// table, applying zero modifications.
47+
func (d DoltStatusTableAdapter) CreateTable(ctx *sql.Context, tableName string, dDb *doltdb.DoltDB, workingSet *doltdb.WorkingSet, rootsProvider env.RootsProvider[*sql.Context]) sql.Table {
48+
return NewStatusTable(ctx, tableName, dDb, workingSet, rootsProvider)
49+
}
50+
51+
// TableName implements the adapters.TableAdapter interface. It returns the default name for the dolt_status table,
52+
// applying zero modifications.
53+
func (d DoltStatusTableAdapter) TableName() string {
54+
return doltdb.StatusTableName
55+
}
56+
3357
// StatusTable is a sql.Table implementation that implements a system table which shows the dolt branches
3458
type StatusTable struct {
3559
rootsProvider env.RootsProvider[*sql.Context]
@@ -61,22 +85,14 @@ func (st StatusTable) String() string {
6185
return st.tableName
6286
}
6387

64-
func getDoltStatusSchema(tableName string) sql.Schema {
88+
func (st StatusTable) Schema() sql.Schema {
6589
return []*sql.Column{
66-
{Name: "table_name", Type: types.Text, Source: tableName, PrimaryKey: true, Nullable: false},
67-
{Name: "staged", Type: types.Boolean, Source: tableName, PrimaryKey: true, Nullable: false},
68-
{Name: "status", Type: types.Text, Source: tableName, PrimaryKey: true, Nullable: false},
90+
{Name: "table_name", Type: types.Text, Source: doltdb.StatusTableName, PrimaryKey: true, Nullable: false},
91+
{Name: "staged", Type: types.Boolean, Source: doltdb.StatusTableName, PrimaryKey: true, Nullable: false},
92+
{Name: "status", Type: types.Text, Source: doltdb.StatusTableName, PrimaryKey: true, Nullable: false},
6993
}
7094
}
7195

72-
// GetDoltStatusSchema returns the schema of the dolt_status system table. This is used
73-
// by Doltgres to update the dolt_status schema using Doltgres types.
74-
var GetDoltStatusSchema = getDoltStatusSchema
75-
76-
func (st StatusTable) Schema() sql.Schema {
77-
return GetDoltStatusSchema(st.tableName)
78-
}
79-
8096
func (st StatusTable) Collation() sql.CollationID {
8197
return sql.Collation_Default
8298
}

0 commit comments

Comments
 (0)