forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup PLC server with postgres and migrations (bluesky-social#249)
* Setup PLC with postgres * Setup PLC with kysely migrations * Add note to PLC migrations file
- Loading branch information
Showing
12 changed files
with
231 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env ts-node | ||
|
||
import * as fs from 'fs/promises' | ||
import * as path from 'path' | ||
|
||
export async function main() { | ||
const now = new Date() | ||
const prefix = now.toISOString().replace(/[^a-z0-9]/gi, '') // Order of migrations matches alphabetical order of their names | ||
const name = process.argv[2] | ||
if (!name || !name.match(/^[a-z0-9-]+$/)) { | ||
process.exitCode = 1 | ||
return console.error( | ||
'Must pass a migration name consisting of lowercase digits, numbers, and dashes.', | ||
) | ||
} | ||
const filename = `${prefix}-${name}` | ||
const dir = path.join(__dirname, '..', 'src', 'server', 'migrations') | ||
|
||
await fs.writeFile(path.join(dir, `${filename}.ts`), template, { flag: 'wx' }) | ||
await fs.writeFile( | ||
path.join(dir, 'index.ts'), | ||
`export * as _${prefix} from './${filename}'\n`, | ||
{ flag: 'a' }, | ||
) | ||
} | ||
|
||
const template = `import { Kysely } from 'kysely' | ||
export async function up(db: Kysely<unknown>): Promise<void> { | ||
// Migration code | ||
} | ||
export async function down(db: Kysely<unknown>): Promise<void> { | ||
// Migration code | ||
} | ||
` | ||
|
||
main() |
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
17 changes: 17 additions & 0 deletions
17
packages/plc/src/server/migrations/20221020T204908820Z-operations-init.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,17 @@ | ||
import { Kysely } from 'kysely' | ||
|
||
export async function up(db: Kysely<unknown>): Promise<void> { | ||
await db.schema | ||
.createTable('operations') | ||
.addColumn('did', 'varchar', (col) => col.notNull()) | ||
.addColumn('operation', 'text', (col) => col.notNull()) | ||
.addColumn('cid', 'varchar', (col) => col.notNull()) | ||
.addColumn('nullified', 'int2', (col) => col.defaultTo(0)) | ||
.addColumn('createdAt', 'varchar', (col) => col.notNull()) | ||
.addPrimaryKeyConstraint('primary_key', ['did', 'cid']) | ||
.execute() | ||
} | ||
|
||
export async function down(db: Kysely<unknown>): Promise<void> { | ||
await db.schema.dropTable('operations').execute() | ||
} |
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,5 @@ | ||
// NOTE this file can be edited by hand, but it is also appended to by the migrations:create command. | ||
// It's important that every migration is exported from here with the proper name. We'd simplify | ||
// this with kysely's FileMigrationProvider, but it doesn't play nicely with the build process. | ||
|
||
export * as _20221020T204908820Z from './20221020T204908820Z-operations-init' |
Oops, something went wrong.