onetable-cli
Advanced tools
Comparing version 0.7.0 to 0.7.2
@@ -30,4 +30,4 @@ #!/usr/bin/env node | ||
import AWS from 'aws-sdk'; | ||
import Migrate from 'onetable-migrate'; | ||
import { Table } from 'dynamodb-onetable'; | ||
import { Migrate } from 'onetable-migrate'; | ||
import Blend from 'js-blend'; | ||
@@ -76,3 +76,3 @@ import Dates from 'js-dates'; | ||
`; | ||
const RESET_VERSION = 'latest'; | ||
const LATEST_VERSION = 'latest'; | ||
const DebugVerbosity = 10; | ||
@@ -158,3 +158,3 @@ class CLI { | ||
else if (cmd == 'reset') { | ||
await this.move(RESET_VERSION); | ||
await this.move(LATEST_VERSION); | ||
} | ||
@@ -249,3 +249,3 @@ else if (cmd == 'status') { | ||
pastMigrations = []; | ||
versions = [RESET_VERSION]; | ||
versions = [LATEST_VERSION]; | ||
} | ||
@@ -310,2 +310,3 @@ else if (target == 'up') { | ||
let noun = versions.length > 1 ? 'changes' : 'change'; | ||
let fromto = action == 'downgrade' ? 'from' : 'to'; | ||
let target = versions[versions.length - 1]; | ||
@@ -316,6 +317,6 @@ if (this.config.profile == 'prod') { | ||
if (this.dry) { | ||
print(`${this.dry} ${action} ${versions.length} ${noun} to version ${target}.`); | ||
print(`${this.dry} ${action} ${versions.length} ${noun} ${fromto} version ${target}.`); | ||
} | ||
else { | ||
print(`Confirm ${versions.length} "${action}" ${noun} to 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}".`); | ||
} | ||
@@ -322,0 +323,0 @@ print(`\nMigrations to ${direction < 0 ? 'revert' : 'apply'}:`); |
{ | ||
"name": "onetable-cli", | ||
"version": "0.7.0", | ||
"version": "0.7.2", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "DynamoDB OneTable Migration CLI", |
@@ -9,3 +9,3 @@ # OneTable Migrate CLI | ||
Theh DynamoDB OneTable Migration CLI is a command line tool for orchestrating DynamoDB migrations when using [DynamoDB OneTable](https://www.npmjs.com/package/dynamodb-onetable) and [OneTable Migrate](https://www.npmjs.com/package/onetable-migrate). | ||
The DynamoDB OneTable Migration CLI is a command line tool for orchestrating DynamoDB migrations when using [DynamoDB OneTable](https://www.npmjs.com/package/dynamodb-onetable) and [OneTable Migrate](https://www.npmjs.com/package/onetable-migrate). | ||
@@ -126,3 +126,3 @@ The CLI is ideal for development teams to initialize and reset database contents and for production use to control and sequence step-wise database upgrades. | ||
Reset the database to the latest migration. This will erase the database and apply the `latest.js` migration. The purpose of the `latest` migration is to have one migration that can quickly create a new database with the latest schema without having to apply all historical migrations. | ||
Reset the database to the latest migration. This should the database and apply the `latest.js` migration. The purpose of the `latest` migration is to have one migration that can quickly create a new database with the latest schema without having to apply all historical migrations. | ||
@@ -235,2 +235,36 @@ ```sh | ||
### Latest Migration | ||
You can create a special `latest` migration that is used for the `migrate reset` command which is is a quick way to get a development database up to the current version. | ||
The latest migration should remove all data from the database and then initialize the database equivalent to applying all migrations. | ||
When creating your `latest.js` migration, be very careful when removing all items from the database. We typically protect this with a test against the deployment profile to ensure you never do this on a production database. | ||
Sample latest.js migration | ||
```javascript | ||
export default { | ||
description: 'Database reset to latest version', | ||
async up(db, migrate) { | ||
if (migrate.params.profile == 'dev') { | ||
await removeAllItems(db) | ||
} | ||
// Provision required database data | ||
}, | ||
async down(db, migrate) { | ||
if (migrate.params.profile == 'dev') { | ||
await removeAllItems(db) | ||
} | ||
}, | ||
} | ||
async function removeAllItems(db) { | ||
do { | ||
items = await db.scanItems({}, {limit: 100}) | ||
for (let item of items) { | ||
await db.deleteItem(item) | ||
} | ||
} while (items.length) | ||
} | ||
``` | ||
### References | ||
@@ -237,0 +271,0 @@ |
@@ -32,4 +32,4 @@ #!/usr/bin/env node | ||
import Migrate from 'onetable-migrate' | ||
import {Table} from 'dynamodb-onetable' | ||
import {Migrate} from 'onetable-migrate' | ||
@@ -82,3 +82,3 @@ import Blend from 'js-blend' | ||
const RESET_VERSION = 'latest' | ||
const LATEST_VERSION = 'latest' | ||
const DebugVerbosity = 10 | ||
@@ -167,3 +167,3 @@ | ||
} else if (cmd == 'reset') { | ||
await this.move(RESET_VERSION) | ||
await this.move(LATEST_VERSION) | ||
} else if (cmd == 'status') { | ||
@@ -254,3 +254,3 @@ await this.status() | ||
pastMigrations = [] | ||
versions = [RESET_VERSION] | ||
versions = [LATEST_VERSION] | ||
@@ -315,2 +315,3 @@ } else if (target == 'up') { | ||
let noun = versions.length > 1 ? 'changes' : 'change' | ||
let fromto = action == 'downgrade' ? 'from' : 'to' | ||
let target = versions[versions.length - 1] | ||
@@ -321,5 +322,5 @@ if (this.config.profile == 'prod') { | ||
if (this.dry) { | ||
print(`${this.dry} ${action} ${versions.length} ${noun} to version ${target}.`) | ||
print(`${this.dry} ${action} ${versions.length} ${noun} ${fromto} version ${target}.`) | ||
} else { | ||
print(`Confirm ${versions.length} "${action}" ${noun} to 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}".`) | ||
} | ||
@@ -326,0 +327,0 @@ print(`\nMigrations to ${direction < 0 ? 'revert' : 'apply'}:`) |
89395
1932
288