Skip to content

Commit

Permalink
WIP - Add SetDefaultSchemaVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Sep 15, 2023
1 parent b21e0a9 commit 4f5b69e
Show file tree
Hide file tree
Showing 12 changed files with 530 additions and 1 deletion.
9 changes: 9 additions & 0 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ type Store interface {
// [FieldKindStringToEnumMapping].
PatchSchema(context.Context, string) error

// SetDefaultSchemaVersion sets the default schema version to the ID provided. It will be applied to all
// collections using the schema.
//
// This will affect all operations interacting with the schema where a schema version is not explicitly
// provided. This includes GQL queries and Collection operations.
//
// It will return an error if the provided schema version ID does not exist.
SetDefaultSchemaVersion(context.Context, string) error

// SetMigration sets the migration for the given source-destination schema version IDs. Is equivilent to
// calling `LensRegistry().SetMigration(ctx, cfg)`.
//
Expand Down
43 changes: 43 additions & 0 deletions client/mocks/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (db *db) updateCollection(
return nil, err
}

err = db.setDefaultSchemaVersion(ctx, txn, desc.Name, desc.Schema.SchemaID, schemaVersionID)
err = db.setDefaultSchemaVersionExplicit(ctx, txn, desc.Name, desc.Schema.SchemaID, schemaVersionID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -585,6 +585,30 @@ func validateUpdateCollectionIndexes(
}

func (db *db) setDefaultSchemaVersion(
ctx context.Context,
txn datastore.Txn,
schemaVersionID string,
) error {
col, err := db.getCollectionByVersionID(ctx, txn, schemaVersionID)
if err != nil {
return err
}

desc := col.Description()
err = db.setDefaultSchemaVersionExplicit(ctx, txn, desc.Name, desc.Schema.SchemaID, schemaVersionID)
if err != nil {
return err
}

cols, err := db.getCollectionDescriptions(ctx, txn)
if err != nil {
return err
}

return db.parser.SetSchema(ctx, txn, cols)
}

func (db *db) setDefaultSchemaVersionExplicit(
ctx context.Context,
txn datastore.Txn,
collectionName string,
Expand Down
19 changes: 19 additions & 0 deletions db/txn_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,25 @@ func (db *explicitTxnDB) PatchSchema(ctx context.Context, patchString string) er
return db.patchSchema(ctx, db.txn, patchString)
}

func (db *implicitTxnDB) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error {
txn, err := db.NewTxn(ctx, false)
if err != nil {
return err
}
defer txn.Discard(ctx)

err = db.setDefaultSchemaVersion(ctx, txn, schemaVersionID)
if err != nil {
return err
}

return txn.Commit(ctx)
}

func (db *explicitTxnDB) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error {
return db.setDefaultSchemaVersion(ctx, db.txn, schemaVersionID)
}

func (db *implicitTxnDB) SetMigration(ctx context.Context, cfg client.LensConfig) error {
txn, err := db.NewTxn(ctx, false)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ func (c *Client) PatchSchema(ctx context.Context, patch string) error {
return err
}

func (c *Client) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error {
methodURL := c.http.baseURL.JoinPath("schema", "default")

req, err := http.NewRequestWithContext(ctx, http.MethodPost, methodURL.String(), strings.NewReader(schemaVersionID))
if err != nil {
return err
}
_, err = c.http.request(req)
return err
}

func (c *Client) SetMigration(ctx context.Context, config client.LensConfig) error {
return c.LensRegistry().SetMigration(ctx, config)
}
Expand Down
16 changes: 16 additions & 0 deletions http/handler_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ func (s *storeHandler) PatchSchema(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
}

func (s *storeHandler) SetDefaultSchemaVersion(rw http.ResponseWriter, req *http.Request) {
store := req.Context().Value(storeContextKey).(client.Store)

schemaVersionID, err := io.ReadAll(req.Body)
if err != nil {
responseJSON(rw, http.StatusBadRequest, errorResponse{err})
return
}
err = store.SetDefaultSchemaVersion(req.Context(), string(schemaVersionID))
if err != nil {
responseJSON(rw, http.StatusBadRequest, errorResponse{err})
return
}
rw.WriteHeader(http.StatusOK)
}

func (s *storeHandler) GetCollection(rw http.ResponseWriter, req *http.Request) {
store := req.Context().Value(storeContextKey).(client.Store)

Expand Down
1 change: 1 addition & 0 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func NewServer(db client.DB) *Server {
api.Route("/schema", func(schema chi.Router) {
schema.Post("/", store_handler.AddSchema)
schema.Patch("/", store_handler.PatchSchema)
schema.Post("/default", store_handler.SetDefaultSchemaVersion)
})
api.Route("/collections", func(collections chi.Router) {
collections.Get("/", store_handler.GetCollection)
Expand Down
4 changes: 4 additions & 0 deletions http/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func (w *Wrapper) PatchSchema(ctx context.Context, patch string) error {
return w.client.PatchSchema(ctx, patch)
}

func (w *Wrapper) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error {
return w.client.SetDefaultSchemaVersion(ctx, schemaVersionID)
}

func (w *Wrapper) SetMigration(ctx context.Context, config client.LensConfig) error {
return w.client.SetMigration(ctx, config)
}
Expand Down
Loading

0 comments on commit 4f5b69e

Please sign in to comment.