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

JIT-29004: Enable and disable TTL support #1

Merged
merged 5 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ npd migrate:rollback

## How to test?
```sh
npm test
yarn test
```

## QueryBuilder Callbacks
Expand Down
56 changes: 43 additions & 13 deletions lib/migrate/migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ Migrator.prototype.deleteTable = function(tableName) {
}.bind(this));
};

Migrator.prototype.enableTTL = function(tableName, attributeName) {
return this.npd().rawClient().updateTimeToLive({
TableName: tableName,
TimeToLiveSpecification: {
AttributeName: attributeName,
Enabled: true,
},
});
};

Migrator.prototype.disableTTL = function(tableName, attributeName) {
return this.npd().rawClient().updateTimeToLive({
TableName: tableName,
TimeToLiveSpecification: {
AttributeName: attributeName,
Enabled: false,
},
});
};

Migrator.prototype.waitUntilTableActivate = function(tableName, timeoutms){
var self = this;
timeoutms = timeoutms || 10000;
Expand Down Expand Up @@ -107,20 +127,28 @@ function MigrateRunner(config){
this.migrator = create(npd);
}

MigrateRunner.prototype._createMigrateTableIfNotExits = function(){
MigrateRunner.prototype._createMigrateTableIfNotExists = function() {
var self = this;
var tableName = this.config.migrations.tableName;
return this.npd().showTables()
.then(function(tables){
var isFound = _.find(tables.TableNames, function(t){ return t === tableName });
if(isFound) { return; }

return self.migrator().createTable(tableName, function(t){
t.number('version').hashKey();
t.provisionedThroughput.apply(t, self.config.migrations.ProvisionedThroughput);
})
.then(function(){
return self.npd().rawClient().waitFor('tableExists', {TableName: tableName});
return new Promise(function(resolve, reject) {
self.npd().table(tableName).describe().then(function(_) {
resolve();
}).catch(function (err) {
if (!err) {
reject();
} else if (err.code === 'ResourceInUseException') {
resolve();
} else if (err.code === 'ResourceNotFoundException') {
self.migrator().createTable(tableName, function(t) {
t.number('version').hashKey();
t.provisionedThroughput.apply(t, self.config.migrations.ProvisionedThroughput);
})
.then(function() {
self.npd().rawClient().waitFor('tableExists', {TableName: tableName}).then(resolve).catch(reject);
}).catch(reject);
} else {
reject();
}
});
});
};
Expand All @@ -129,7 +157,7 @@ MigrateRunner.prototype.run = function(){
var self = this;
var tableName = this.config.migrations.tableName;

return this._createMigrateTableIfNotExits().then(function(){
return this._createMigrateTableIfNotExists().then(function(){
return self.npd().table(tableName).all().then(function(data){
var dirs = fs.readdirSync(self.config.cwd);

Expand Down Expand Up @@ -165,6 +193,8 @@ MigrateRunner.prototype.run = function(){

return utils.PromiseWaterfall(tasks);
});
}).catch(function (err) {
console.error(err);
});
};

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@
"dynamodb-doc": "^1.0.0",
"glob": "^5.0.3",
"interpret": "^0.5.2",
"json-loader": "0.5.2",
"liftoff": "^2.0.3",
"lodash": "^3.5.0",
"minimist": "^1.1.1",
"readline": "0.0.7",
"v8flags": "^2.0.3"
"uglify-js": "2.4.24",
"v8flags": "^2.0.3",
"webpack": "1.10.1"
},
"devDependencies": {
"aws-sdk": "^2.1.18",
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash -e

npm install [email protected] [email protected] [email protected]
yarn add [email protected] [email protected] [email protected]

webpack=node_modules/.bin/webpack
uglifyjs=node_modules/.bin/uglifyjs
Expand Down
Loading