DiffSchema diff the database schema.
(ctx context.Context, req *connect.Request[v1pb.DiffSchemaRequest])
| 682 | |
| 683 | // DiffSchema diff the database schema. |
| 684 | func (s *DatabaseService) DiffSchema(ctx context.Context, req *connect.Request[v1pb.DiffSchemaRequest]) (*connect.Response[v1pb.DiffSchemaResponse], error) { |
| 685 | engine, err := s.getParserEngine(ctx, req.Msg) |
| 686 | if err != nil { |
| 687 | return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get parser engine")) |
| 688 | } |
| 689 | |
| 690 | // PG/CockroachDB raw schema text still needs the omni SDL diff path. Metadata |
| 691 | // targets such as changelogs use schema.DiffMigration's metadata path. |
| 692 | if shouldDiffSchemaViaSDL(engine, req.Msg) { |
| 693 | migrationSQL, err := s.diffSchemaViaSDL(ctx, req.Msg, engine) |
| 694 | if err != nil { |
| 695 | return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to compute schema diff")) |
| 696 | } |
| 697 | return connect.NewResponse(&v1pb.DiffSchemaResponse{Diff: migrationSQL}), nil |
| 698 | } |
| 699 | |
| 700 | // Metadata-backed diff path. PostgreSQL/CockroachDB changelog targets use |
| 701 | // their registered metadata migration here, while other engines keep the |
| 702 | // existing metadata parser/generator behavior. |
| 703 | sourceDBSchema, err := s.getSourceDBMetadata(ctx, req.Msg) |
| 704 | if err != nil { |
| 705 | return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get source schema")) |
| 706 | } |
| 707 | targetDBSchema, err := s.getTargetDBMetadata(ctx, req.Msg) |
| 708 | if err != nil { |
| 709 | return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get target schema")) |
| 710 | } |
| 711 | migrationSQL, err := schema.DiffMigration(engine, sourceDBSchema, targetDBSchema) |
| 712 | if err != nil { |
| 713 | return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to compute schema diff")) |
| 714 | } |
| 715 | return connect.NewResponse(&v1pb.DiffSchemaResponse{Diff: migrationSQL}), nil |
| 716 | } |
| 717 | |
| 718 | func shouldDiffSchemaViaSDL(engine storepb.Engine, req *v1pb.DiffSchemaRequest) bool { |
| 719 | if engine != storepb.Engine_POSTGRES && engine != storepb.Engine_COCKROACHDB { |
nothing calls this directly
no test coverage detected