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

UBER-1185: Fix TT migration issues #4320

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 28 additions & 11 deletions models/task/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,8 @@ export interface FixTaskResult {
export async function fixTaskTypes (
client: MigrationClient,
descriptor: Ref<ProjectTypeDescriptor>,
dataFactory: (t: ProjectType) => Promise<FixTaskData[]>
dataFactory: (t: ProjectType) => Promise<FixTaskData[]>,
migrateTasks?: (projects: Project[], taskType: TaskType) => Promise<void>
): Promise<FixTaskResult> {
const categoryObj = client.model.findObject(descriptor)
if (categoryObj === undefined) {
Expand All @@ -655,10 +656,6 @@ export async function fixTaskTypes (
const resultProjects: Project[] = []

for (const t of projectTypes) {
if (t.tasks?.length > 0) {
// Already migrated.
continue
}
t.tasks = [...(t.tasks ?? [])]
if (t.targetClass === undefined) {
const targetProjectClassId: Ref<Class<Doc>> = generateId()
Expand Down Expand Up @@ -704,12 +701,22 @@ export async function fixTaskTypes (
)
}

const dataTypes = await dataFactory(t)
const newTaskTypes = await dataFactory(t)

const projects = await client.find<Project>(DOMAIN_SPACE, { type: t._id })
resultProjects.push(...projects)

for (const data of dataTypes) {
for (const data of newTaskTypes) {
// Check and skip if already had task type for same class
const tt = await client.find<TaskType>(DOMAIN_TASK, {
_class: task.class.TaskType,
ofClass: data.ofClass,
parent: t._id
})
if (tt.length > 0) {
continue
}

const taskTypeId: Ref<TaskType> = data._id ?? generateId()
const descr = client.model.getObject(data.descriptor)

Expand All @@ -718,16 +725,16 @@ export async function fixTaskTypes (
_class: data.statusClass
})

const dStatuses = [...t.statuses.map((it) => it._id)]
const dStatuses: Ref<Status>[] = []
const statusAttr = findStatusAttr(client.hierarchy, data.ofClass)
// Ensure we have at leas't one item in every category.
for (const c of data.statusCategories) {
const category = typeof c === 'string' ? c : c.category
const cat = await client.model.findOne(core.class.StatusCategory, { _id: category })

const st = statuses.find((it) => it.category === category)
const st = statuses.filter((it) => it.category === category)
const newStatuses: Ref<Status>[] = []
if (st === undefined) {
if (st.length === 0 || typeof c === 'object') {
if (typeof c === 'string') {
// We need to add new status into missing category
const statusId: Ref<Status> = generateId()
Expand Down Expand Up @@ -773,6 +780,8 @@ export async function fixTaskTypes (
})
newStatuses.push(statusId)
dStatuses.push(statusId)
} else {
dStatuses.push(st._id)
}

await client.update(
Expand All @@ -785,6 +794,12 @@ export async function fixTaskTypes (
t.statuses.push(...newStatuses.map((it) => ({ _id: it, taskType: taskTypeId })))
}
}
} else {
for (const sss of st.map((it) => it._id)) {
if (!dStatuses.includes(sss)) {
dStatuses.push(sss)
}
}
}
}
const taskType: TaskType = {
Expand Down Expand Up @@ -854,11 +869,13 @@ export async function fixTaskTypes (
)
t.tasks.push(taskTypeId)
// Update kind and target classId
const projectsToUpdate = projects.filter((it) => it.type === t._id)
await client.update(
DOMAIN_TASK,
{ space: { $in: projects.map((it) => it._id) }, _class: data.ofClass },
{ space: { $in: projectsToUpdate.map((it) => it._id) }, _class: data.ofClass },
{ $set: { kind: taskTypeId } }
)
await migrateTasks?.(projectsToUpdate, taskType)
}

// We need to fix project statuses field, for proper icon calculation.
Expand Down
2 changes: 1 addition & 1 deletion models/tracker/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ async function restoreToDoCategory (client: MigrationClient): Promise<void> {
const taskTypes = await client.find<TaskType>(DOMAIN_TASK, {
_class: task.class.TaskType,
descriptor: tracker.descriptors.Issue,
_id: { $in: p.tasks }
_id: { $in: p.tasks ?? [] }
})
for (const taskType of taskTypes) {
if (taskType.statusCategories.includes(task.statusCategory.ToDo)) continue
Expand Down