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

Nara off-chain migration script fixes #315

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a0eaeda
Add is short field to video entity (#301)
zeeshanakram3 Feb 22, 2024
effc0f0
Disable both in Appp and eail notifications for video posted events (…
zeeshanakram3 Feb 22, 2024
f6b3bbf
bump package version and update CHANGELOG (#302)
zeeshanakram3 Feb 22, 2024
743f56b
Simple public homefeed query and mutation (#304)
zeeshanakram3 Feb 26, 2024
887427c
Update `nara` from `master` (#300)
zeeshanakram3 Feb 27, 2024
a597dbd
Revert "Update `nara` from `master` (#300)" (#306)
zeeshanakram3 Feb 27, 2024
bc2ef48
Merge remote-tracking branch 'upstream/master' into nara
zeeshanakram3 Feb 27, 2024
cd98642
generate auth api docs and types
zeeshanakram3 Feb 27, 2024
237c900
Merge remote-tracking branch 'upstream/nara' into nara
zeeshanakram3 Feb 27, 2024
f1561b5
Merge remote-tracking branch 'upstream/nara' into nara
zeeshanakram3 Feb 29, 2024
3127350
add is short derived field to video entity (#310)
zeeshanakram3 Mar 5, 2024
3c222c3
Merge remote-tracking branch 'upstream/nara' into nara
zeeshanakram3 Mar 5, 2024
8d98cc4
Merge remote-tracking branch 'upstream/master' into nara
zeeshanakram3 Mar 5, 2024
8e5b98b
fix: video language detection fix (#309)
zeeshanakram3 Mar 8, 2024
08fd96c
fix: include max 1 video per channel in homepage videos (#313)
zeeshanakram3 Mar 12, 2024
5c41fa8
Merge remote-tracking branch 'upstream/nara' into nara
zeeshanakram3 Mar 12, 2024
2e06b34
Merge remote-tracking branch 'upstream/master' into nara
zeeshanakram3 Mar 12, 2024
8a2a070
Merge remote-tracking branch 'upstream/nara' into nara
zeeshanakram3 Mar 12, 2024
8ae65ba
remove NextEntityIdManager migration script
zeeshanakram3 Mar 13, 2024
5591913
[offchainState] add v4.0.0 (CRT release) migrations
zeeshanakram3 Mar 13, 2024
44c4ae5
[offchainState] remove ORDER BY clause from UPDATE statements
zeeshanakram3 Mar 13, 2024
13aea15
add migration for NextEntityId
zeeshanakram3 Mar 13, 2024
b9182cf
bump package version and update CHANGELOG
zeeshanakram3 Mar 13, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 4.0.0

This is Creator Tokens (CRT) release. It introduces the CRT mappings, custom resolvers and mutations.

# 3.7.0

## Schema changes
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "orion",
"version": "3.7.0",
"version": "4.0.0",
"engines": {
"node": ">=16"
},
Expand Down
16 changes: 7 additions & 9 deletions src/mappings/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { metaToObject } from '@joystream/metadata-protobuf/utils'
import { AnyMetadataClass, DecodedMetadataObject } from '@joystream/metadata-protobuf/types'
import { Logger } from '../logger'
import { SubstrateBlock } from '@subsquid/substrate-processor'
import { Event, MetaprotocolTransactionResultFailed, NftActivity, NftHistoryEntry } from '../model'
import { encodeAddress } from '@polkadot/util-crypto'
import { EntityManagerOverlay } from '../utils/overlay'
import { Bytes } from '@polkadot/types/primitive'
import { metaToObject } from '@joystream/metadata-protobuf/utils'
import { createType } from '@joystream/types'
import { Bytes } from '@polkadot/types/primitive'
import { u8aToHex } from '@polkadot/util'
import { encodeAddress } from '@polkadot/util-crypto'
import { SubstrateBlock } from '@subsquid/substrate-processor'
import { Logger } from '../logger'
import { Event, MetaprotocolTransactionResultFailed, NftActivity, NftHistoryEntry } from '../model'
import { CommentCountersManager } from '../utils/CommentsCountersManager'
import { VideoRelevanceManager } from '../utils/VideoRelevanceManager'
import { NextEntityIdManager } from '../utils/NextEntityIdManager'
import { EntityManagerOverlay } from '../utils/overlay'

export const commentCountersManager = new CommentCountersManager()
export const videoRelevanceManager = new VideoRelevanceManager()
export const migrateCounters = new NextEntityIdManager()
// eslint-disable-next-line no-void
void videoRelevanceManager.init({
fullUpdateLoopTime: 1000 * 60 * 60 * 12, // 12 hrs
Expand Down
3 changes: 1 addition & 2 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ import {
processUpcomingTokenSaleUpdatedEvent,
processUserParticipatedInSplitEvent,
} from './mappings/token'
import { commentCountersManager, migrateCounters, videoRelevanceManager } from './mappings/utils'
import { commentCountersManager, videoRelevanceManager } from './mappings/utils'
import { Event } from './types/support'
import { EventHandler, EventInstance, EventNames, eventConstructors } from './utils/events'
import { assertAssignable } from './utils/misc'
Expand Down Expand Up @@ -415,7 +415,6 @@ processor.run(new TypeormDatabase({ isolationLevel: 'READ COMMITTED' }), async (
await commentCountersManager.updateVideoCommentsCounters(em, true)
await commentCountersManager.updateParentRepliesCounters(em, true)
await videoRelevanceManager.updateVideoRelevanceValue(em, true)
await migrateCounters.migrateCounters(overlay)
ctx.log.info(`Offchain state successfully imported!`)
}
}
Expand Down
26 changes: 0 additions & 26 deletions src/utils/NextEntityIdManager.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/notification/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { EntityManagerOverlay } from '../overlay'
export const RUNTIME_NOTIFICATION_ID_TAG = 'RuntimeNotification'
export const OFFCHAIN_NOTIFICATION_ID_TAG = 'OffchainNotification'

function notificationPrefAllTrue(): NotificationPreference {
export function notificationPrefAllTrue(): NotificationPreference {
return new NotificationPreference({ inAppEnabled: true, emailEnabled: true })
}

Expand Down
68 changes: 65 additions & 3 deletions src/utils/offchainState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import fs from 'fs'
import path from 'path'
import { EntityManager } from 'typeorm'
import * as model from '../model'
import {
AccountNotificationPreferences,
fromJsonDeliveryStatus,
fromJsonNotificationType,
fromJsonReadOrUnread,
fromJsonRecipientType,
} from '../model'
import { uniqueId } from './crypto'
import { defaultNotificationPreferences } from './notification/helpers'
import { defaultNotificationPreferences, notificationPrefAllTrue } from './notification/helpers'

const DEFAULT_EXPORT_PATH = path.resolve(__dirname, '../../db/export/export.json')

Expand Down Expand Up @@ -112,11 +119,51 @@ function migrateExportDataToV320(data: ExportedData): ExportedData {
return data
}

export function setCrtNotificationPreferences(
notificationPreferencesObj: any
): AccountNotificationPreferences {
notificationPreferencesObj.crtIssued = notificationPrefAllTrue()
notificationPreferencesObj.crtMarketStarted = notificationPrefAllTrue()
notificationPreferencesObj.crtMarketMint = notificationPrefAllTrue()
notificationPreferencesObj.crtMarketBurn = notificationPrefAllTrue()
notificationPreferencesObj.crtSaleStarted = notificationPrefAllTrue()
notificationPreferencesObj.crtSaleMint = notificationPrefAllTrue()
notificationPreferencesObj.crtRevenueShareStarted = notificationPrefAllTrue()
notificationPreferencesObj.crtRevenueSharePlanned = notificationPrefAllTrue()
notificationPreferencesObj.crtRevenueShareEnded = notificationPrefAllTrue()
const notificationPreferences = new AccountNotificationPreferences(
undefined,
notificationPreferencesObj
)
return notificationPreferences
}

function migrateExportDataToV400(data: ExportedData): ExportedData {
data.Account?.values.forEach((account) => {
// account will find himself with all CRT notification pref. enabled by default
account.notificationPreferences = setCrtNotificationPreferences(
account.notificationPreferences as AccountNotificationPreferences
)
})

data.Notification?.values.forEach((notification) => {
notification.notificationType = fromJsonNotificationType(notification.notificationType)
notification.status = fromJsonReadOrUnread(notification.status)
notification.recipient = fromJsonRecipientType(notification.recipient)
})

data.EmailDeliveryAttempt?.values.forEach((emailDeliveryAttempt) => {
emailDeliveryAttempt.status = fromJsonDeliveryStatus(emailDeliveryAttempt.status)
})
return data
}

export class OffchainState {
private logger = createLogger('offchainState')
private _isImported = false

private migrations: Migrations = {
'4.0.0': migrateExportDataToV400,
'3.2.0': migrateExportDataToV320,
'3.0.0': migrateExportDataToV300,
}
Expand Down Expand Up @@ -252,7 +299,6 @@ export class OffchainState {
})
.join(', ')}
) AS "data"
ORDER BY "id"
WHERE "${meta.tableName}"."id" = "data"."id"`,
fieldNames.map((fieldName) => batch.map((v) => v[fieldName]))
)
Expand All @@ -269,7 +315,23 @@ export class OffchainState {
values.length
} entities left)...`
)
await em.getRepository(entityName).insert(batch)

// UPSERT operation specifically for NextEntityId
if (entityName === 'NextEntityId') {
for (const entity of batch) {
await em.query(
`
INSERT INTO "next_entity_id" ("entity_name", "next_id")
VALUES ($1, $2)
ON CONFLICT (entity_name)
DO UPDATE SET next_id = EXCLUDED.next_id;
`,
[entity.entityName, entity.nextId]
)
}
} else {
await em.getRepository(entityName).insert(batch)
}
}
}
this.logger.info(
Expand Down
5 changes: 0 additions & 5 deletions src/utils/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,6 @@ export class EntityManagerOverlay {
return this.em
}

// reason: during migration the overlay would write to the database the old nextId
public invalidateRepository(entityName: string) {
this.repositories.delete(entityName)
}

// Create an entity repository overlay or load already cached one
public getRepository<E extends AnyEntity>(entityClass: Constructor<E>): RepositoryOverlay<E> {
const loadedRepository = this.repositories.get(entityClass.name)
Expand Down
Loading