Skip to content

Commit

Permalink
VTAdmin: Support for schema migrations view/create
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <[email protected]>
  • Loading branch information
beingnoble03 committed Nov 4, 2024
1 parent 1a4f2b9 commit ca76dc7
Show file tree
Hide file tree
Showing 15 changed files with 758 additions and 13 deletions.
35 changes: 28 additions & 7 deletions go/vt/proto/vtadmin/vtadmin.pb.go

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

88 changes: 88 additions & 0 deletions go/vt/proto/vtadmin/vtadmin_vtproto.pb.go

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

26 changes: 26 additions & 0 deletions go/vt/vtadmin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
"vitess.io/vitess/go/vt/vtadmin/rbac"
"vitess.io/vitess/go/vt/vtadmin/sort"
"vitess.io/vitess/go/vt/vtadmin/vtadminproto"
"vitess.io/vitess/go/vt/vtctl/grpcvtctldserver"
"vitess.io/vitess/go/vt/vtctl/workflow"
"vitess.io/vitess/go/vt/vtenv"
"vitess.io/vitess/go/vt/vterrors"
Expand Down Expand Up @@ -487,6 +488,31 @@ func (api *API) ApplySchema(ctx context.Context, req *vtadminpb.ApplySchemaReque
return nil, err
}

// Parser with default options. New() itself initializes with default MySQL version.
parser, err := sqlparser.New(sqlparser.Options{
TruncateUILen: 512,
TruncateErrLen: 0,
})
if err != nil {
return nil, err
}

// Split the sql statement received from request.
sqlParts, err := parser.SplitStatementToPieces(req.Sql)
if err != nil {
return nil, err
}

req.Request.Sql = sqlParts

// Set the callerID if not empty.
if req.CallerId != "" {
req.Request.CallerId = &vtrpcpb.CallerID{Principal: req.CallerId}
}

// Set the default wait replicas timeout.
req.Request.WaitReplicasTimeout = protoutil.DurationToProto(grpcvtctldserver.DefaultWaitReplicasTimeout)

return c.ApplySchema(ctx, req.Request)
}

Expand Down
15 changes: 11 additions & 4 deletions go/vt/vtadmin/http/schema_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,26 @@ func ApplySchema(ctx context.Context, r Request, api *API) *JSONResponse {
decoder := json.NewDecoder(r.Body)
defer r.Body.Close()

var req vtctldatapb.ApplySchemaRequest
if err := decoder.Decode(&req); err != nil {
var body struct {
Sql string `json:"sql"`
CallerId string `json:"caller_id"`
Request vtctldatapb.ApplySchemaRequest `json:"request"`
}

if err := decoder.Decode(&body); err != nil {
return NewJSONResponse(nil, &errors.BadRequest{
Err: err,
})
}

vars := mux.Vars(r.Request)
req.Keyspace = vars["keyspace"]
body.Request.Keyspace = vars["keyspace"]

resp, err := api.server.ApplySchema(ctx, &vtadminpb.ApplySchemaRequest{
ClusterId: vars["cluster_id"],
Request: &req,
Sql: body.Sql,
CallerId: body.CallerId,
Request: &body.Request,
})

return NewJSONResponse(resp, err)
Expand Down
6 changes: 5 additions & 1 deletion proto/vtadmin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,11 @@ message WorkflowSwitchTrafficRequest {

message ApplySchemaRequest {
string cluster_id = 1;
vtctldata.ApplySchemaRequest request = 2;
// Request.Sql will be overriden by this Sql field.
string sql = 2;
// Request.CallerId will be overriden by this CallerId field.
string caller_id = 3;
vtctldata.ApplySchemaRequest request = 4;
}

message CancelSchemaMigrationRequest {
Expand Down
38 changes: 38 additions & 0 deletions web/vtadmin/src/api/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,3 +1054,41 @@ export const showVDiff = async ({ clusterID, request }: ShowVDiffParams) => {

return vtadmin.VDiffShowResponse.create(result);
};

export const fetchSchemaMigrations = async (request: vtadmin.IGetSchemaMigrationsRequest) => {
const { result } = await vtfetch(`/api/migrations/`, {
body: JSON.stringify(request),
method: 'post',
});

const err = vtadmin.GetSchemaMigrationsResponse.verify(result);
if (err) throw Error(err);

return vtadmin.GetSchemaMigrationsResponse.create(result);
};

export interface ApplySchemaParams {
clusterID: string;
keyspace: string;
callerID: string;
sql: string;
request: vtctldata.IApplySchemaRequest;
}

export const applySchema = async ({ clusterID, keyspace, callerID, sql, request }: ApplySchemaParams) => {
const body = {
sql,
caller_id: callerID,
request,
};

const { result } = await vtfetch(`/api/migration/${clusterID}/${keyspace}`, {
body: JSON.stringify(body),
method: 'post',
});

const err = vtctldata.ApplySchemaResponse.verify(result);
if (err) throw Error(err);

return vtctldata.ApplySchemaResponse.create(result);
};
12 changes: 12 additions & 0 deletions web/vtadmin/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import { CreateMoveTables } from './routes/createWorkflow/CreateMoveTables';
import { Transactions } from './routes/Transactions';
import { CreateReshard } from './routes/createWorkflow/CreateReshard';
import { CreateMaterialize } from './routes/createWorkflow/CreateMaterialize';
import { SchemaMigrations } from './routes/SchemaMigrations';
import { CreateSchemaMigration } from './routes/createSchemaMigration/CreateSchemaMigration';

export const App = () => {
return (
Expand Down Expand Up @@ -139,6 +141,16 @@ export const App = () => {
<Workflow />
</Route>

<Route exact path="/migrations">
<SchemaMigrations />
</Route>

{!isReadOnlyMode() && (
<Route exact path="/migrations/create">
<CreateSchemaMigration />
</Route>
)}

<Route path="/transactions">
<Transactions />
</Route>
Expand Down
3 changes: 3 additions & 0 deletions web/vtadmin/src/components/NavRail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export const NavRail = () => {
</ul>

<ul className={style.navList}>
<li>
<NavRailLink hotkey="M" text="Migrations" to="/migrations" />
</li>
<li>
<NavRailLink hotkey="T" text="Transactions" to="/transactions" />
</li>
Expand Down
Loading

0 comments on commit ca76dc7

Please sign in to comment.