Skip to content

Commit f9cc1ff

Browse files
committed
chore: add isLegacy column to actions table
Signed-off-by: Tomás Migone <[email protected]>
1 parent f508a92 commit f9cc1ff

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Logger } from '@graphprotocol/common-ts'
2+
import { DataTypes, QueryInterface } from 'sequelize'
3+
4+
interface MigrationContext {
5+
queryInterface: QueryInterface
6+
logger: Logger
7+
}
8+
9+
interface Context {
10+
context: MigrationContext
11+
}
12+
13+
export async function up({ context }: Context): Promise<void> {
14+
const { queryInterface, logger } = context
15+
16+
logger.debug(`Checking if 'Actions' table exists`)
17+
const tables = await queryInterface.showAllTables()
18+
if (!tables.includes('Actions')) {
19+
logger.info(`Actions table does not exist, migration not necessary`)
20+
return
21+
}
22+
23+
logger.debug(`Checking if 'Actions' table needs to be migrated`)
24+
const table = await queryInterface.describeTable('Actions')
25+
const isLegacy = table.isLegacy
26+
if (isLegacy) {
27+
logger.info(`'isLegacy' column already exists, migration not necessary`)
28+
return
29+
}
30+
31+
logger.info(`Add 'isLegacy' column to 'Actions' table`)
32+
await queryInterface.addColumn('Actions', 'isLegacy', {
33+
type: DataTypes.BOOLEAN,
34+
allowNull: false,
35+
defaultValue: true,
36+
})
37+
}
38+
39+
export async function down({ context }: Context): Promise<void> {
40+
const { queryInterface, logger } = context
41+
42+
return await queryInterface.sequelize.transaction({}, async transaction => {
43+
const tables = await queryInterface.showAllTables()
44+
45+
if (tables.includes('Actions')) {
46+
logger.info(`Remove 'isLegacy' column`)
47+
await context.queryInterface.removeColumn('Actions', 'isLegacy', {
48+
transaction,
49+
})
50+
}
51+
})
52+
}

packages/indexer-cli/src/actions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export async function buildActionInput(
3636
protocolNetwork: string,
3737
): Promise<ActionInput> {
3838
await validateActionInput(type, actionParams)
39+
40+
// TODO: we could check isHorizon status here to set the proper value for isLegacy, but it requires multiNetworks
41+
// The IndexerManagementServer will set the correct value anyways
42+
const isLegacy = false
43+
3944
switch (type) {
4045
case ActionType.ALLOCATE:
4146
return {
@@ -47,6 +52,7 @@ export async function buildActionInput(
4752
status,
4853
priority,
4954
protocolNetwork,
55+
isLegacy,
5056
}
5157
case ActionType.UNALLOCATE: {
5258
let poi = actionParams.param2
@@ -64,6 +70,7 @@ export async function buildActionInput(
6470
status,
6571
priority,
6672
protocolNetwork,
73+
isLegacy,
6774
}
6875
}
6976
case ActionType.REALLOCATE: {
@@ -83,6 +90,7 @@ export async function buildActionInput(
8390
status,
8491
priority,
8592
protocolNetwork,
93+
isLegacy,
8694
}
8795
}
8896
}

packages/indexer-common/src/actions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export interface ActionInput {
4646
status: ActionStatus
4747
priority: number | undefined
4848
protocolNetwork: string
49+
isLegacy: boolean
4950
}
5051

5152
export const isValidActionInput = (
@@ -161,6 +162,7 @@ export interface ActionFilter {
161162
reason?: string
162163
updatedAt?: WhereOperators
163164
protocolNetwork?: string
165+
isLegacy?: boolean
164166
}
165167

166168
export const actionFilterToWhereOptions = (filter: ActionFilter): WhereOptions => {
@@ -192,6 +194,7 @@ export interface ActionResult {
192194
failureReason: string | null
193195
transaction: string | null
194196
protocolNetwork: string
197+
isLegacy: boolean
195198
}
196199

197200
export enum ActionType {

packages/indexer-common/src/indexer-management/__tests__/helpers.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ describe('Actions', () => {
246246
priority: 0,
247247
// When writing directly to the database, `protocolNetwork` must be in the CAIP2-ID format.
248248
protocolNetwork: 'eip155:421614',
249+
isLegacy: true,
249250
}
250251

251252
await models.Action.upsert(action)
@@ -283,6 +284,14 @@ describe('Actions', () => {
283284
updatedAt: { [Op.lte]: literal("NOW() - INTERVAL '1d'") },
284285
}),
285286
).resolves.toHaveLength(0)
287+
288+
await expect(
289+
ActionManager.fetchActions(models, null, {
290+
status: ActionStatus.FAILED,
291+
type: ActionType.ALLOCATE,
292+
isLegacy: true,
293+
}),
294+
).resolves.toHaveLength(1)
286295
})
287296
})
288297
describe('Types', () => {

packages/indexer-common/src/indexer-management/actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export class ActionManager {
112112
oldestAffectedAllocationCreatedAtEpoch:
113113
affectedAllocations[0]?.createdAtEpoch ??
114114
'no action in the batch affects existing allocations',
115+
oldestAffectedAllocationIsLegacy: affectedAllocations[0]?.isLegacy,
115116
affectedAllocationExpiring,
116117
},
117118
)

packages/indexer-common/src/indexer-management/models/action.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export class Action extends Model<
3737

3838
declare protocolNetwork: string
3939

40+
declare isLegacy: boolean
41+
4042
// eslint-disable-next-line @typescript-eslint/ban-types
4143
public toGraphQL(): object {
4244
return { ...this.toJSON(), __typename: 'Action' }
@@ -152,6 +154,11 @@ export const defineActionModels = (sequelize: Sequelize): ActionModels => {
152154
is: caip2IdRegex,
153155
},
154156
},
157+
isLegacy: {
158+
type: DataTypes.BOOLEAN,
159+
allowNull: false,
160+
defaultValue: true,
161+
},
155162
},
156163
{
157164
modelName: 'Action',

packages/indexer-common/src/indexer-management/resolvers/actions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import { literal, Op, Transaction } from 'sequelize'
2121
import { ActionManager } from '../actions'
2222
import groupBy from 'lodash.groupby'
23+
import { extractNetwork } from './utils'
2324

2425
// Perform insert, update, or no-op depending on existing queue data
2526
// INSERT - No item in the queue yet targeting this deploymentID
@@ -170,6 +171,14 @@ export default {
170171
}
171172
})
172173

174+
// Set proper value for isLegacy - any new actions in horizon are not legacy
175+
await Promise.all(
176+
actions.map(async (action) => {
177+
const network = extractNetwork(action.protocolNetwork, multiNetworks)
178+
action.isLegacy = !(await network.isHorizon.value())
179+
}),
180+
)
181+
173182
// Let Network Monitors validate actions based on their protocol networks
174183
await multiNetworks.mapNetworkMapped(
175184
groupBy(actions, (action) => action.protocolNetwork),

0 commit comments

Comments
 (0)