Skip to content

Commit

Permalink
update migrations descriptions, migrations list supports json (#5222)
Browse files Browse the repository at this point in the history
* update migrations descriptions, migrations list supports json

* use waitForOpen instead of a try/catch for DB
  • Loading branch information
mat-if authored Aug 13, 2024
1 parent f6b5886 commit 31cbe8f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
30 changes: 27 additions & 3 deletions ironfish-cli/src/commands/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { NodeUtils } from '@ironfish/sdk'
import { IronfishCommand } from '../../command'
import { JsonFlags } from '../../flags'
import * as ui from '../../ui'

export class StatusCommand extends IronfishCommand {
static description = `list data migrations`
static description = `list data migrations and their status`
static enableJsonFlag = true

async start(): Promise<void> {
static flags = {
...JsonFlags,
}

async start(): Promise<unknown> {
await this.parse(StatusCommand)

const node = await this.sdk.node()
await node.migrator.check()

// Verify the DB is in a state to be opened by the migrator
await NodeUtils.waitForOpen(node)
await node.closeDB()

const migrationsStatus = await node.migrator.status()

const displayData: Record<string, string> = {}
for (const { name, applied } of migrationsStatus.migrations) {
displayData[name] = applied ? 'APPLIED' : 'WAITING'
}

this.log(ui.card(displayData))

this.log(`\nYou have ${migrationsStatus.unapplied} unapplied migrations.`)

return migrationsStatus
}
}
2 changes: 1 addition & 1 deletion ironfish-cli/src/commands/migrations/revert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { IronfishCommand } from '../../command'

export class RevertCommand extends IronfishCommand {
static description = `Revert the last run migration`
static description = `revert the last run migration`

static hidden = true

Expand Down
2 changes: 1 addition & 1 deletion ironfish-cli/src/commands/migrations/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../command'

export class StartCommand extends IronfishCommand {
static description = `Run migrations`
static description = `run migrations`

static flags = {
dry: Flags.boolean({
Expand Down
36 changes: 17 additions & 19 deletions ironfish/src/migrations/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { Assert } from '../assert'
import { Logger } from '../logger'
import { IDatabaseTransaction } from '../storage/database/transaction'
import { StrEnumUtils } from '../utils'
import { ErrorUtils } from '../utils/error'
import { MIGRATIONS } from './data'
import { Database, Migration, MigrationContext } from './migration'

type MigrationStatus = { name: string; applied: boolean }

export class Migrator {
readonly context: MigrationContext
readonly logger: Logger
Expand Down Expand Up @@ -172,31 +173,28 @@ export class Migrator {
logger.info(`Successfully ${dryRun ? 'dry ran' : 'applied'} ${unapplied.length} migrations`)
}

async check(): Promise<void> {
async status(): Promise<{
migrations: MigrationStatus[]
unapplied: number
}> {
let unapplied = 0

this.logger.info('Checking migrations:')

const migrations: MigrationStatus[] = []
for (const migration of this.migrations) {
process.stdout.write(` Checking ${migration.name.slice(0, 35)}...`.padEnd(50, ' '))
const applied = await this.isApplied(migration)

try {
const applied = await this.isApplied(migration)
process.stdout.write(` ${applied ? 'APPLIED' : 'WAITING'}\n`)
migrations.push({
name: migration.name,
applied,
})

if (!applied) {
unapplied++
}
} catch (e) {
process.stdout.write(` ERROR\n`)
this.logger.error(ErrorUtils.renderError(e, true))
throw e
if (!applied) {
unapplied++
}
}

if (unapplied > 0) {
this.logger.info('')
this.logger.info(`You have ${unapplied} unapplied migrations.`)
return {
migrations,
unapplied,
}
}
}

0 comments on commit 31cbe8f

Please sign in to comment.