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-174: Introduce createOn every there #3222

Merged
merged 3 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 2 additions & 4 deletions dev/generator/src/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ const object: AttachedData<Issue> = {
reportedTime: 0,
estimation: 0,
reports: 0,
childInfo: [],
createOn: Date.now()
childInfo: []
}

export interface IssueOptions {
Expand Down Expand Up @@ -98,8 +97,7 @@ async function genIssue (client: TxOperations, statuses: Ref<IssueStatus>[]): Pr
estimation: object.estimation,
reports: 0,
relations: [],
childInfo: [],
createOn: Date.now()
childInfo: []
}
await client.addCollection(
tracker.class.Issue,
Expand Down
18 changes: 8 additions & 10 deletions dev/generator/src/recruit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import contact, { Channel, Employee, EmployeeAccount, Person } from '@hcengineer
import core, {
AttachedData,
Data,
generateId,
MeasureContext,
MeasureMetricsContext,
metricsToString,
MixinUpdate,
Ref,
TxOperations,
WorkspaceId
WorkspaceId,
generateId,
metricsToString
} from '@hcengineering/core'
import { MinioService } from '@hcengineering/minio'
import recruit from '@hcengineering/model-recruit'
import { Applicant, Candidate, Vacancy } from '@hcengineering/recruit'
import { genRanks, State } from '@hcengineering/task'
import { State, genRanks } from '@hcengineering/task'
import faker from 'faker'
import jpeg, { BufferRet } from 'jpeg-js'
import { addAttachments, AttachmentOptions } from './attachments'
import { addComments, CommentOptions } from './comments'
import { AttachmentOptions, addAttachments } from './attachments'
import { CommentOptions, addComments } from './comments'
import { connect } from './connect'
import { createUpdateSpaceKanban } from './kanban'
import { findOrUpdate, findOrUpdateAttached } from './utils'
Expand Down Expand Up @@ -162,8 +162,7 @@ async function genApplicant (
doneState: null,
rank: rank as string,
startDate: null,
dueDate: null,
createOn: Date.now()
dueDate: null
}

// Update or create candidate
Expand Down Expand Up @@ -234,8 +233,7 @@ async function genCandidate (
const candidate: Data<Person> = {
name: fName + ',' + lName,
city: faker.address.city(),
avatar: imgId,
createOn: Date.now()
avatar: imgId
}

const candidateMixin: MixinUpdate<Person, Candidate> = {
Expand Down
4 changes: 0 additions & 4 deletions models/chunter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ export class TChunterMessage extends TAttachedDoc implements ChunterMessage {
@ReadOnly()
createBy!: Ref<Account>

@Prop(TypeTimestamp(), chunter.string.Create)
@ReadOnly()
createOn!: Timestamp

@Prop(TypeTimestamp(), chunter.string.Edit)
editedOn?: Timestamp

Expand Down
18 changes: 1 addition & 17 deletions models/chunter/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//

import { Comment, Message, ThreadMessage } from '@hcengineering/chunter'
import core, { Doc, DOMAIN_TX, Ref, TxCreateDoc, TxOperations } from '@hcengineering/core'
import core, { DOMAIN_TX, Doc, Ref, TxCreateDoc, TxOperations } from '@hcengineering/core'
import { MigrateOperation, MigrationClient, MigrationUpgradeClient } from '@hcengineering/model'
import { DOMAIN_CHUNTER, DOMAIN_COMMENT } from './index'
import chunter from './plugin'
Expand Down Expand Up @@ -81,21 +81,6 @@ async function createBacklink (tx: TxOperations): Promise<void> {
}
}

export async function setCreate (client: TxOperations): Promise<void> {
const messages = (await client.findAll(chunter.class.Message, {}))
.filter((m) => m.createBy === undefined)
.map((m) => m._id)
if (messages.length === 0) return
const txes = await client.findAll(core.class.TxCreateDoc, { objectId: { $in: messages } })
const promises = txes.map(async (tx) => {
await client.updateDoc<Message>(chunter.class.Message, tx.objectSpace, tx.objectId as Ref<Message>, {
createBy: tx.modifiedBy,
createOn: tx.modifiedOn
})
})
await Promise.all(promises)
}

export async function migrateMessages (client: MigrationClient): Promise<void> {
const messages = await client.find(DOMAIN_CHUNTER, {
_class: chunter.class.Message,
Expand Down Expand Up @@ -188,6 +173,5 @@ export const chunterOperation: MigrateOperation = {
await createGeneral(tx)
await createRandom(tx)
await createBacklink(tx)
await setCreate(tx)
}
}
5 changes: 0 additions & 5 deletions models/contact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
Index,
Model,
Prop,
ReadOnly,
TypeDate,
TypeRef,
TypeString,
Expand Down Expand Up @@ -103,10 +102,6 @@ export class TContact extends TDoc implements Contact {
@Prop(TypeString(), contact.string.Location)
@Index(IndexKind.FullText)
city!: string

@Prop(TypeTimestamp(), contact.string.CreatedDate)
@ReadOnly()
createOn!: Timestamp
}

@Model(contact.class.Channel, core.class.AttachedDoc, DOMAIN_CHANNEL)
Expand Down
67 changes: 3 additions & 64 deletions models/contact/src/migration.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//

import { Contact } from '@hcengineering/contact'
import { DOMAIN_TX, TxCreateDoc, TxOperations } from '@hcengineering/core'
import { TxOperations } from '@hcengineering/core'
import { MigrateOperation, MigrationClient, MigrationUpgradeClient } from '@hcengineering/model'
import core from '@hcengineering/model-core'
import contact, { DOMAIN_CONTACT } from './index'
import contact from './index'

async function createSpace (tx: TxOperations): Promise<void> {
const current = await tx.findOne(core.class.Space, {
Expand Down Expand Up @@ -43,64 +42,6 @@ async function createSpace (tx: TxOperations): Promise<void> {
}
}

let totalCreateOn = 0
async function setCreate (client: MigrationClient): Promise<void> {
while (true) {
const docs = await client.find<Contact>(
DOMAIN_CONTACT,
{
_class: {
$in: [contact.class.Contact, contact.class.Organization, contact.class.Person, contact.class.Employee]
},
createOn: { $exists: false }
},
{ limit: 500 }
)
if (docs.length === 0) {
break
}
totalCreateOn += docs.length
console.log('processing createOn migration', totalCreateOn)
const creates = await client.find<TxCreateDoc<Contact>>(DOMAIN_TX, {
objectId: { $in: docs.map((it) => it._id) },
_class: core.class.TxCreateDoc
})
for (const doc of docs) {
const tx = creates.find((it) => it.objectId === doc._id)
if (tx !== undefined) {
await client.update(
DOMAIN_CONTACT,
{
_id: doc._id
},
{
createOn: tx.modifiedOn
}
)
await client.update(
DOMAIN_TX,
{
_id: tx._id
},
{
'attributes.createOn': tx.modifiedOn
}
)
} else {
await client.update(
DOMAIN_CONTACT,
{
_id: doc._id
},
{
createOn: doc.modifiedOn
}
)
}
}
}
}

async function createEmployeeEmail (client: TxOperations): Promise<void> {
const employees = await client.findAll(contact.class.Employee, {})
const channels = await client.findAll(contact.class.Channel, {
Expand Down Expand Up @@ -133,9 +74,7 @@ async function createEmployeeEmail (client: TxOperations): Promise<void> {
}

export const contactOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {
await setCreate(client)
},
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
const tx = new TxOperations(client, core.account.System)
await createSpace(tx)
Expand Down
5 changes: 5 additions & 0 deletions models/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
Mixin as MMixin,
Model,
Prop,
ReadOnly,
TypeBoolean,
TypeIntlString,
TypeRecord,
Expand Down Expand Up @@ -101,6 +102,10 @@ export class TDoc extends TObj implements Doc {
@Prop(TypeRef(core.class.Account), core.string.CreatedBy)
@Index(IndexKind.Indexed)
createdBy!: Ref<Account>

@Prop(TypeTimestamp(), core.string.CreatedDate)
@ReadOnly()
createOn!: Timestamp
}

@Model(core.class.AttachedDoc, core.class.Doc)
Expand Down
71 changes: 67 additions & 4 deletions models/core/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
// limitations under the License.
//

import { MigrateOperation, MigrationClient, MigrationUpgradeClient } from '@hcengineering/model'
import core, {
Doc,
AttachedDoc,
DOMAIN_BLOB,
DOMAIN_DOC_INDEX_STATE,
DOMAIN_MODEL,
DOMAIN_TX,
TxCreateDoc,
Doc,
TxCollectionCUD,
AttachedDoc
TxCreateDoc
} from '@hcengineering/core'
import { MigrateOperation, MigrationClient, MigrationUpgradeClient } from '@hcengineering/model'

async function fillCreatedBy (client: MigrationClient): Promise<void> {
const h = client.hierarchy
Expand Down Expand Up @@ -87,9 +87,72 @@ async function fillCreatedBy (client: MigrationClient): Promise<void> {
}
}
}
async function fillCreatedOn (client: MigrationClient): Promise<void> {
const h = client.hierarchy
const domains = h.domains()
for (const domain of domains) {
if (
domain === DOMAIN_TX ||
domain === DOMAIN_MODEL ||
domain === DOMAIN_BLOB ||
domain === DOMAIN_DOC_INDEX_STATE
) {
continue
}
while (true) {
try {
const objects = await client.find<Doc>(
domain,
{ createdOn: { $exists: false } },
{ projection: { _id: 1, modifiedOn: 1 }, limit: 10000 }
)
if (objects.length === 0) {
break
}
const txes = await client.find<TxCreateDoc<Doc>>(
DOMAIN_TX,
{
_class: core.class.TxCreateDoc,
objectId: { $in: Array.from(objects.map((it) => it._id)) }
},
{ projection: { _id: 1, modifiedOn: 1, createOn: 1, objectId: 1 } }
)

const txes2 = (
await client.find<TxCollectionCUD<Doc, AttachedDoc>>(
DOMAIN_TX,
{
_class: core.class.TxCollectionCUD,
'tx._class': core.class.TxCreateDoc,
'tx.objectId': { $in: Array.from(objects.map((it) => it._id)) }
},
{ projection: { _id: 1, modifiedOn: 1, createOn: 1, tx: 1 } }
)
).map((it) => it.tx as unknown as TxCreateDoc<Doc>)

const txMap = new Map(txes.concat(txes2).map((p) => [p.objectId, p]))

console.log('migrateCreateOn', domain, objects.length)
await client.bulk(
domain,
objects.map((it) => {
const createTx = txMap.get(it._id)
return {
filter: { _id: it._id },
update: {
createdOn: createTx?.createOn ?? it.modifiedOn
}
}
})
)
} catch (err) {}
}
}
}
export const coreOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {
await fillCreatedBy(client)
await fillCreatedOn(client)
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {}
}
10 changes: 4 additions & 6 deletions models/demo/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// limitations under the License.
//

import contact, { EmployeeAccount } from '@hcengineering/contact'
import core, { AccountRole, DOMAIN_TX, TxCreateDoc, TxOperations } from '@hcengineering/core'
import { MigrateOperation, MigrationClient, MigrationUpgradeClient } from '@hcengineering/model'
import contact, { EmployeeAccount } from '@hcengineering/contact'
import recruit from '@hcengineering/model-recruit'
import { DOMAIN_CONTACT } from '@hcengineering/model-contact'
import recruit from '@hcengineering/model-recruit'

async function createCandidate (
tx: TxOperations,
Expand All @@ -33,8 +33,7 @@ async function createCandidate (
if (current !== undefined) return
const u1 = await tx.createDoc(contact.class.Person, recruit.space.CandidatesPublic, {
name,
city,
createOn: Date.now()
city
})
await tx.addCollection(contact.class.Channel, recruit.space.CandidatesPublic, u1, contact.class.Person, 'channels', {
provider: contact.channelProvider.Email,
Expand Down Expand Up @@ -74,8 +73,7 @@ export const demoOperation: MigrateOperation = {
const employee = await ops.createDoc(contact.class.Employee, contact.space.Employee, {
name: 'Chen,Rosamund',
city: 'Mountain View',
active: true,
createOn: Date.now()
active: true
})

await ops.createDoc<EmployeeAccount>(contact.class.EmployeeAccount, core.space.Model, {
Expand Down
1 change: 1 addition & 0 deletions models/lead/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export function createModel (builder: Builder): void {
orderBy: [
['state', SortingOrder.Ascending],
['modifiedOn', SortingOrder.Descending],
['createOn', SortingOrder.Descending],
['dueDate', SortingOrder.Ascending],
['rank', SortingOrder.Ascending]
],
Expand Down
Loading