-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved indexedAt time to record instead of ipld-block (#501)
* moved indexedAt time to record instead of ipld-block * use underlying kysely obj in tests * pr feedback
- Loading branch information
Showing
12 changed files
with
133 additions
and
8 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
35 changes: 35 additions & 0 deletions
35
packages/pds/src/db/migrations/20230127T215753149Z-indexed-at-on-record.ts
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,35 @@ | ||
import { Kysely } from 'kysely' | ||
|
||
export async function up(db: Kysely<any>): Promise<void> { | ||
const now = new Date().toISOString() | ||
await db.schema | ||
.alterTable('record') | ||
.addColumn('indexedAt', 'varchar', (col) => col.notNull().defaultTo(now)) | ||
.execute() | ||
|
||
const ref = db.dynamic.ref | ||
|
||
const indexedAtForRecordQb = db | ||
.selectFrom('ipld_block') | ||
.whereRef('ipld_block.cid', '=', ref('record.cid')) | ||
.select('indexedAt') | ||
|
||
await db | ||
.updateTable('record') | ||
.set({ | ||
indexedAt: indexedAtForRecordQb, | ||
}) | ||
.whereExists(indexedAtForRecordQb) | ||
.execute() | ||
|
||
await db.schema.alterTable('ipld_block').dropColumn('indexedAt').execute() | ||
} | ||
|
||
export async function down(db: Kysely<unknown>): Promise<void> { | ||
const now = new Date().toISOString() | ||
await db.schema | ||
.alterTable('ipld_block') | ||
.addColumn('indexedAt', 'varchar', (col) => col.notNull().defaultTo(now)) | ||
.execute() | ||
await db.schema.alterTable('record').dropColumn('indexedAt').execute() | ||
} |
File renamed without changes.
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
77 changes: 77 additions & 0 deletions
77
packages/pds/tests/migrations/indexed-at-on-record.test.ts
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,77 @@ | ||
import { Database } from '../../src' | ||
import { randomStr } from '@atproto/crypto' | ||
import { dataToCborBlock, TID } from '@atproto/common' | ||
import { AtUri } from '@atproto/uri' | ||
import { Kysely } from 'kysely' | ||
|
||
describe('indexedAt on record migration', () => { | ||
let db: Database | ||
let rawDb: Kysely<any> | ||
|
||
beforeAll(async () => { | ||
if (process.env.DB_POSTGRES_URL) { | ||
db = Database.postgres({ | ||
url: process.env.DB_POSTGRES_URL, | ||
schema: 'migration_indexed_at_on_record', | ||
}) | ||
} else { | ||
db = Database.memory() | ||
} | ||
|
||
await db.migrateToOrThrow('_20221230T215012029Z') | ||
rawDb = db.db | ||
}) | ||
|
||
afterAll(async () => { | ||
await db.close() | ||
}) | ||
|
||
const randomDate = () => { | ||
const start = new Date(2022, 0, 1) | ||
const end = new Date() | ||
return new Date( | ||
start.getTime() + Math.random() * (end.getTime() - start.getTime()), | ||
).toISOString() | ||
} | ||
|
||
const times: { [cid: string]: string } = {} | ||
|
||
it('fills the db with some records & blocks', async () => { | ||
const blocks: any[] = [] | ||
const records: any[] = [] | ||
for (let i = 0; i < 100; i++) { | ||
const date = randomDate() | ||
const record = { test: randomStr(8, 'base32') } | ||
const block = await dataToCborBlock(record) | ||
blocks.push({ | ||
cid: block.cid.toString(), | ||
content: block.bytes, | ||
size: block.bytes.length, | ||
indexedAt: date, | ||
}) | ||
const uri = AtUri.make('did:example:alice', 'fake.posts', TID.nextStr()) | ||
records.push({ | ||
uri: uri.toString(), | ||
cid: block.cid.toString(), | ||
did: uri.hostname, | ||
collection: uri.collection, | ||
rkey: uri.rkey, | ||
}) | ||
times[block.cid.toString()] = date | ||
} | ||
|
||
await rawDb.insertInto('ipld_block').values(blocks).execute() | ||
await rawDb.insertInto('record').values(records).execute() | ||
}) | ||
|
||
it('migrates up', async () => { | ||
await db.migrateToOrThrow('_20230127T215753149Z') | ||
}) | ||
|
||
it('associated the date to the correct record', async () => { | ||
const res = await rawDb.selectFrom('record').selectAll().execute() | ||
res.forEach((row) => { | ||
expect(row.indexedAt).toEqual(times[row.cid]) | ||
}) | ||
}) | ||
}) |
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