MCPcopy Create free account
hub / github.com/bytebase/bytebase / getTargetDBMetadata

Method getTargetDBMetadata

backend/api/v1/database_service.go:843–952  ·  view source on GitHub ↗
(ctx context.Context, request *v1pb.DiffSchemaRequest)

Source from the content-addressed store, hash-verified

841}
842
843func (s *DatabaseService) getTargetDBMetadata(ctx context.Context, request *v1pb.DiffSchemaRequest) (*model.DatabaseMetadata, error) {
844 changeHistoryID := request.GetChangelog()
845
846 // If the change history id is set, use the schema of the change history as the target.
847 if changeHistoryID != "" {
848 instanceID, databaseName, changelogID, err := common.GetInstanceDatabaseChangelogID(changeHistoryID)
849 if err != nil {
850 return nil, err
851 }
852
853 changelog, err := s.store.GetChangelog(ctx, &store.FindChangelogMessage{
854 InstanceID: instanceID,
855 ResourceID: &changelogID,
856 })
857 if err != nil {
858 return nil, err
859 }
860 if changelog == nil {
861 return nil, errors.Errorf("changelog %q not found", changelogID)
862 }
863
864 // Use SyncHistory to get historical metadata
865 if changelog.SyncHistory != nil {
866 syncHistory, err := s.store.GetSyncHistory(ctx, *changelog.SyncHistory)
867 if err != nil {
868 return nil, err
869 }
870 if syncHistory == nil {
871 return nil, errors.Errorf("sync history %s not found", *changelog.SyncHistory)
872 }
873
874 // Get instance to determine engine and case sensitivity
875 instance, err := s.store.GetInstance(ctx, &store.FindInstanceMessage{
876 Workspace: common.GetWorkspaceIDFromContext(ctx),
877 ResourceID: &instanceID,
878 })
879 if err != nil {
880 return nil, err
881 }
882 if instance == nil {
883 return nil, errors.Errorf("instance %s not found", instanceID)
884 }
885
886 return model.NewDatabaseMetadata(
887 syncHistory.Metadata,
888 []byte(syncHistory.Schema),
889 &storepb.DatabaseConfig{},
890 instance.Metadata.GetEngine(),
891 store.IsObjectCaseSensitive(instance),
892 ), nil
893 }
894
895 // Fallback to current database schema if no sync history
896 dbMetadata, err := s.store.GetDBSchema(ctx, &store.FindDBSchemaMessage{
897 Workspace: common.GetWorkspaceIDFromContext(ctx),
898 InstanceID: instanceID,
899 DatabaseName: databaseName,
900 })

Callers 1

DiffSchemaMethod · 0.95

Calls 14

getParserEngineMethod · 0.95
NewDatabaseMetadataFunction · 0.92
IsObjectCaseSensitiveFunction · 0.92
GetDatabaseMetadataFunction · 0.92
GetInstanceDatabaseIDFunction · 0.92
ErrorfMethod · 0.80
GetSyncHistoryMethod · 0.80
GetDBSchemaMethod · 0.80
GetChangelogMethod · 0.65
GetInstanceMethod · 0.65

Tested by

no test coverage detected