Skip to content

Commit

Permalink
FIX: dry run
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael O'Brien committed Nov 15, 2021
1 parent db01eba commit 31e013b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
41 changes: 25 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,60 +89,68 @@ export default {
version: '0.0.1',
description: 'Purpose of this migration',
schema: Schema,
async up(db, migrate) {
// await db.create('Model', {})
async up(db, migrate, params) {
if (!params.dry) {
await db.create('Model', {})
} else {
console.log('Dry run: create "Model"')
}
},
async down(db, migrate) {
// await db.remove('Model', {})
async down(db, migrate, params) {
if (!params.dry) {
await db.remove('Model', {})
} else {
console.log('Dry run: remove "Model"')
}
}
}
```

### Examples

Apply the next migration
Apply the next migration.

```sh
onetable up
```

Reverse the last migration
Reverse the last migration.

```sh
onetable down
```

Repeat the last migration
Repeat the last migration.

```sh
onetable repeat
```

Migrate to a specific version (up or down)
Migrate to a specific version (up or down).

```sh
onetable 0.1.3
```

Apply all outstanding migrations
Apply all outstanding migrations.

```sh
onetable all
```

Show the last applied migration
Show the last applied migration.

```sh
onetable status
```

Show applied migrations
Show applied migrations.

```sh
onetable list
```

Show outstanding migrations not yet applied
Show outstanding migrations not yet applied.

```sh
onetable outstanding
Expand All @@ -154,13 +162,14 @@ Reset the database to the latest migration. This should reset the database and a
onetable reset
```

Generate a specific version migration
Generate a specific version migration.

```sh
onetable --bump 2.4.3 generate
```

Do a dry run for a migration and not execute
Do a dry run for a migration and not execute. This will set params.dry to true when invoking the up/down.
It is up to the up/down routines to implement the dry run functionality if that support is desired.

```sh
onetable --dry up
Expand Down Expand Up @@ -268,13 +277,13 @@ export default {
version: '0.0.1',
description: 'Database reset to latest version',
schema: Schema,
async up(db, migrate) {
async up(db, migrate, params) {
if (migrate.params.profile == 'dev') {
await removeAllItems(db)
}
// Provision required database data
},
async down(db, migrate) {
async down(db, migrate, params) {
if (migrate.params.profile == 'dev') {
await removeAllItems(db)
}
Expand Down
13 changes: 7 additions & 6 deletions package-lock.json

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

34 changes: 15 additions & 19 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import Readline from 'readline'
import Semver from 'semver'
import AWS from 'aws-sdk'

import {Table} from 'dynamodb-onetable'
import {Migrate} from 'onetable-migrate'

// DEV
// import {Table} from '../../onetable/dist/mjs/index.js'
// import {Migrate} from '../../onetable-migrate/dist/mjs/index.js'
// import {Migrate} from '../../migrate/dist/mjs/index.js'

import Blend from 'js-blend'
import Dates from 'js-dates'
Expand All @@ -45,11 +43,15 @@ const MigrationTemplate = `
export default {
version: 'VERSION',
description: 'Purpose of this migration',
async up(db, migrate) {
// await db.create('Model', {})
async up(db, migrate, params) {
if (!params.dry) {
// await db.create('Model', {})
}
},
async down(db, migrate) {
// await db.remove('Model', {})
async down(db, migrate, params) {
if (!params.dry) {
// await db.remove('Model', {})
}
}
}`

Expand Down Expand Up @@ -84,7 +86,7 @@ Options:
--crypto cipher:password # Crypto to use for encrypted attributes
--debug # Show debug trace
--dir directory # Change to directory to execute
--dry # Dry-run, don't execute
--dry # Dry-run, pass params.dry to migrations.
--endpoint http://host:port # Database endpoint
--force # Force action without confirmation
--profile prod|qa|dev|... # Select configuration profile
Expand Down Expand Up @@ -319,7 +321,7 @@ class CLI {
await this.confirm(versions, direction)
for (let version of versions) {
let verb = ['Downgrade from', 'Reset to', 'Upgrade to', 'Repeat'][direction + 1]
let migration = await this.migrate.apply(direction, version)
let migration = await this.migrate.apply(direction, version, {dry: this.dry})
print(`${verb} "${migration.version} - ${migration.description}"`)
}
current = await this.migrate.getCurrentVersion()
Expand All @@ -342,18 +344,12 @@ class CLI {
if (this.config.profile == 'prod') {
await this.rusure('WARNING: DANGEROUS: You are working on a production database! ')
}
if (this.dry) {
print(`${this.dry} ${action} ${versions.length} ${noun} ${fromto} version ${target}.`)
} else {
print(`Confirm ${versions.length} "${action}" ${noun} ${fromto} version "${target}" for database "${this.config.onetable.name}" using profile "${this.config.profile}".`)
}
print(`Confirm ${versions.length} "${action}" ${noun} ${fromto} version "${target}" for database "${this.config.onetable.name}" using profile "${this.config.profile}".`)
print(`\nMigrations to ${direction < 0 ? 'revert' : 'apply'}:`)
for (let version of versions) {
print(`${version}`)
}
if (!this.dry) {
await this.rusure()
}
await this.rusure()
print()
}

Expand Down Expand Up @@ -497,8 +493,8 @@ class Proxy {
this.lambda = new AWS.Lambda(aws)
}

async apply(direction, version) {
return await this.invoke('apply', {direction, version})
async apply(direction, version, params = {}) {
return await this.invoke('apply', {direction, version, params})
}

async findPastMigrations() {
Expand Down

0 comments on commit 31e013b

Please sign in to comment.