Skip to content

Commit

Permalink
fix backwards compatibility of migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Connoropolous committed Sep 11, 2023
1 parent 0afa5db commit 757ef75
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
7 changes: 4 additions & 3 deletions web/src/migrating/import/cloneFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../../types/shared'
import { cellIdFromString } from '../../utils'
import { Tag, Outcome, Connection, ProjectMeta } from '../../types'
import { BackwardsCompatibleProjectMeta, LayeringAlgorithm } from 'zod-models'
import { BackwardsCompatibleConnection, BackwardsCompatibleProjectMeta, LayeringAlgorithm } from 'zod-models'

export type ActionHashMap = { [oldActionHash: ActionHashB64]: ActionHashB64 }

Expand Down Expand Up @@ -94,7 +94,7 @@ issue. Then try again.`)
}

export const cloneConnection = (outcomeActionHashMap: ActionHashMap) => (
old: WithActionHash<Connection>
old: WithActionHash<BackwardsCompatibleConnection>
): WithActionHash<Connection> => {
const newParentOutcomeActionHash = outcomeActionHashMap[old.parentActionHash]
const newChildOutcomeActionHash = outcomeActionHashMap[old.childActionHash]
Expand All @@ -118,7 +118,8 @@ export const cloneConnection = (outcomeActionHashMap: ActionHashMap) => (
...old, // technically not needed, but left in case more properties are added in future
parentActionHash: newParentOutcomeActionHash,
childActionHash: newChildOutcomeActionHash,
siblingOrder: old.siblingOrder || 0, // siblingOrder is a new field, so it may not exist
// siblingOrder is a new field in Acorn v9, so it may not exist from before
siblingOrder: typeof old['siblingOrder'] !== "undefined" ? old['siblingOrder'] : 0,
// randomizer used to be a float, but is now an int
randomizer: Number(old.randomizer.toFixed()),
isImported: true,
Expand Down
7 changes: 5 additions & 2 deletions web/src/migrating/import/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { installProject } from '../../projects/installProject'
import { createProfilesZomeApi } from './zomeApiCreators'
import { importProject } from './importProject'
import ProfilesZomeApi from '../../api/profilesApi'
import { AllProjectsDataExport, AllProjectsDataExportSchema } from 'zod-models'
import {
BackwardsCompatibleAllProjectsExport,
BackwardsCompatibleAllProjectsExportSchema,
} from 'zod-models'
import { internalJoinProject } from '../../projects/joinProject'

const stringToJSONSchema = z.string().transform((str, ctx): any => {
Expand All @@ -29,7 +32,7 @@ export async function internalImportProjectsData(
migrationData: string,
onStep: (completed: number, toComplete: number) => void
) {
const migrationDataParsed: AllProjectsDataExport = AllProjectsDataExportSchema.parse(
const migrationDataParsed: BackwardsCompatibleAllProjectsExport = BackwardsCompatibleAllProjectsExportSchema.parse(
stringToJSONSchema.parse(migrationData)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import {z} from 'zod'
import { z } from 'zod'
import ProfileSchema from '../profile/profileSchema'
import ProjectExportDataV1Schema from '../projectExportData/projectExportDataSchema'
import ProjectExportDataSchema, {
BackwardsCompatibleProjectExportSchema,
} from '../projectExportData/projectExportDataSchema'

// this is the one that requires backwards compatibility
// it's the one that gets used for importing
export const BackwardsCompatibleAllProjectsExportSchema = z.object({
myProfile: ProfileSchema,
projects: z.array(ProjectExportDataV1Schema),
projects: z.array(BackwardsCompatibleProjectExportSchema),
integrityVersion: z.number().optional(),
})
export type BackwardsCompatibleAllProjectsExport = z.infer<
typeof BackwardsCompatibleAllProjectsExportSchema
>

export type BackwardsCompatibleAllProjectsExport = z.infer<typeof BackwardsCompatibleAllProjectsExportSchema>

// this is designed for whatever the current/latest version is.
export const AllProjectsDataExportSchema = z.object({
myProfile: ProfileSchema,
projects: z.array(ProjectExportDataV1Schema),
projects: z.array(ProjectExportDataSchema),
integrityVersion: z.number(),
})

export type AllProjectsDataExport = z.infer<typeof AllProjectsDataExportSchema>

export default AllProjectsDataExportSchema
export default AllProjectsDataExportSchema

0 comments on commit 757ef75

Please sign in to comment.