Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VTAdmin: Support for schema migrations view/create #17134

Merged
merged 8 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -488,6 +489,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 @@ -388,7 +388,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 @@ -1068,3 +1068,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 @@ -45,6 +45,8 @@ import { Transactions } from './routes/Transactions';
import { Transaction } from './routes/transaction/Transaction';
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 @@ -140,6 +142,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
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/
import React from 'react';
import Dialog from '../../dialog/Dialog';
import { Icon, Icons } from '../../Icon';
import Dialog from './Dialog';
import { Icon, Icons } from '../Icon';

export interface ErrorDialogProps {
errorTitle?: string;
Expand Down
Loading
Loading