-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from o2Labs/ttl-fixes
Fix TTL including milliseconds
- Loading branch information
Showing
12 changed files
with
184 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { DataMapper } from '@aws/dynamodb-data-mapper'; | ||
import { DynamoDB } from 'aws-sdk'; | ||
import { getUnixTime } from 'date-fns'; | ||
import { Config } from '../app-config'; | ||
import { BookingsModel } from '../db/bookings'; | ||
import { OfficeBookingModel } from '../db/officeBookings'; | ||
import { UserBookingsModel } from '../db/userBookings'; | ||
|
||
// Check if TTL is beyond the year 3000 | ||
const timestampTooHigh = (ttl: number) => ttl > 32503680000; | ||
|
||
async function* getCorrectedTtls(mapper: DataMapper, model: { new (): any }) { | ||
for await (const booking of mapper.scan(model)) { | ||
if (timestampTooHigh(booking.ttl)) { | ||
const ttl = new Date(booking.ttl); | ||
booking.ttl = getUnixTime(ttl); | ||
yield booking; | ||
} | ||
} | ||
} | ||
|
||
const scanAndFix = async (mapper: DataMapper, model: { new (): any }) => { | ||
let updated = 0; | ||
for await (const _item of mapper.batchPut(getCorrectedTtls(mapper, model))) { | ||
updated++; | ||
if (updated % 100 === 0) { | ||
console.log('Updated ', updated); | ||
} | ||
} | ||
console.log('Updated ', updated); | ||
}; | ||
|
||
export const fixTtl = async (config: Config) => { | ||
const mapper = new DataMapper({ | ||
client: new DynamoDB(config.dynamoDB), | ||
tableNamePrefix: config.dynamoDBTablePrefix, | ||
}); | ||
await scanAndFix(mapper, BookingsModel); | ||
await scanAndFix(mapper, OfficeBookingModel); | ||
await scanAndFix(mapper, UserBookingsModel); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,70 @@ | ||
// const { app, resetDb, config } = configureServer('migrations', { | ||
// users: [{ Username: cognitoUsername, UserCreateDate: cognitoUserCreated }], | ||
// }); | ||
import { configureServer } from './test-utils'; | ||
import { DataMapper } from '@aws/dynamodb-data-mapper'; | ||
import { DynamoDB } from 'aws-sdk'; | ||
import { UserBookingsModel } from '../db/userBookings'; | ||
import { fixTtl } from '../migrations/4-fix-ttl'; | ||
import { BookingsModel } from '../db/bookings'; | ||
import { OfficeBookingModel } from '../db/officeBookings'; | ||
|
||
// beforeEach(resetDb); | ||
const { resetDb, config } = configureServer('migrations'); | ||
|
||
test('no-active-migration', () => {}); | ||
beforeEach(resetDb); | ||
|
||
describe('4 - fix TTL', async () => { | ||
it(`fixes bookings ttl`, async () => { | ||
const expires = new Date('2020-03-01'); | ||
const mapper = new DataMapper({ | ||
client: new DynamoDB(config.dynamoDB), | ||
tableNamePrefix: config.dynamoDBTablePrefix, | ||
}); | ||
|
||
const userBookingKey = { | ||
email: 'user.name@domain.test', | ||
weekCommencing: '2020-01-01', | ||
}; | ||
const officeBookingKey = { | ||
name: 'the-office', | ||
date: '2020-01-01', | ||
}; | ||
const bookingKey = { | ||
id: 'booking-id', | ||
user: 'user.name@domain.test', | ||
}; | ||
await mapper.put( | ||
Object.assign(new UserBookingsModel(), userBookingKey, { | ||
bookingCount: 1, | ||
ttl: expires.getTime(), | ||
}) | ||
); | ||
await mapper.put( | ||
Object.assign(new OfficeBookingModel(), officeBookingKey, { | ||
bookingCount: 1, | ||
parkingCount: 0, | ||
ttl: expires.getTime(), | ||
}) | ||
); | ||
await mapper.put( | ||
Object.assign(new BookingsModel(), bookingKey, { | ||
date: '2020-01-01', | ||
officeId: 'the-office', | ||
parking: false, | ||
ttl: expires.getTime(), | ||
}) | ||
); | ||
|
||
await fixTtl(config); | ||
|
||
const updatedUserBooking = await mapper.get( | ||
Object.assign(new UserBookingsModel(), userBookingKey) | ||
); | ||
expect(updatedUserBooking.ttl).toBe(Math.floor(expires.getTime() / 1000)); | ||
|
||
const updatedOfficeBooking = await mapper.get( | ||
Object.assign(new OfficeBookingModel(), officeBookingKey) | ||
); | ||
expect(updatedOfficeBooking.ttl).toBe(Math.floor(expires.getTime() / 1000)); | ||
|
||
const updatedBooking = await mapper.get(Object.assign(new BookingsModel(), bookingKey)); | ||
expect(updatedBooking.ttl).toBe(Math.floor(expires.getTime() / 1000)); | ||
}); | ||
}); |