noot
All checks were successful
commit-tag / commit-tag-image (map[context:./migrations file:./migrations/Dockerfile name:migrations]) (push) Successful in 18s
commit-tag / commit-tag-image (map[context:./ts file:./ts/Dockerfile name:ts]) (push) Successful in 52s

This commit is contained in:
a 2025-03-02 18:34:53 -06:00
parent b0a5939eec
commit 7d0cb9d9ba
No known key found for this signature in database
GPG Key ID: 2F22877AA4DFDADB
4 changed files with 3 additions and 130 deletions

View File

@ -31,7 +31,7 @@ jobs:
registry: https://tuxpa.in
username: actions_token
password: ${{ secrets.ACTIONS_TOKEN }}
- name: build and push typescript
- name: build and push docker images
uses: docker/build-push-action@v6
with:
context: ${{ matrix.args.context }}

View File

@ -1,6 +1,7 @@
FROM ghcr.io/jackc/tern:v2.3.2
USER 1000:1000
WORKDIR /migrations
COPY . .
ENTRYPOINT ["/tern"]

View File

@ -1,7 +1,6 @@
import { Command } from 'clipanion';
import { c } from '#/di';
import { runMigrations } from '#/services/pg/migrations';
// di
import "#/services/temporal"
@ -90,7 +89,6 @@ const addSchedules = async (c: Client) => {
try {
const desc = await handle.describe();
console.log(desc)
}catch(e: any){
if(e instanceof ScheduleNotFoundError) {
await c.schedule.create(o)
@ -107,8 +105,6 @@ export class WorkerCommand extends Command {
async execute() {
const { db } = await c.getAsync(PG);
await runMigrations(db);
const client = await c.getAsync(Client);
// schedules
await addSchedules(client);

View File

@ -1,124 +0,0 @@
import { Sql, TransactionSql } from "postgres";
type MigrationFunc = (sql:TransactionSql)=>Promise<void>
interface Migration {
name: string
up: MigrationFunc
// TODO: implement down
down?: MigrationFunc
}
const migration = (name:string, up:MigrationFunc, down?:MigrationFunc)=>{
return {name, up, down}
}
const migrations: Array<Migration> = [
migration("create-api-responses", async (sql)=>{
await sql`
create table if not exists wynn_api_responses (
path text not null,
content jsonb not null,
content_hash text not null,
received_time timestamptz not null
)`
await sql`
create index wynn_api_responses_received_time on wynn_api_responses (received_time, path)
`
}),
migration("guild-info", async (sql)=>{
await sql`
create table if not exists wynn_guild_members (
guild_id UUID not null,
member_id UUID not null,
rank text not null,
joined_at timestamptz not null,
contributed bigint not null,
primary key (guild_id, member_id),
unique(member_id)
)`
await sql`
create table if not exists wynn_guild_info (
uid UUID not null,
name text not null,
prefix text not null,
level bigint,
xp_percent bigint,
territories bigint,
wars bigint,
created timestamptz,
primary key (uid)
)`
await sql`
create table if not exists wynn_guild_season_results (
guild_id UUID not null,
season text not null,
rating bigint not null,
territories bigint not null,
primary key (guild_id, season)
)
`
}),
migration("create-user-info", async (sql)=>{
await sql`
create table if not exists minecraft_user (
uid UUID not null,
name text not null,
server text,
primary key (uid)
)
`
}),
migration("create-guild-settings", async (sql)=>{
await sql`
create table if not exists discord_guild_setting(
discord_guild text not null,
name text not null,
value jsonb not null,
primary key (discord_guild, name)
)`
}),
migration("add-guild-xp", async (sql)=>{
await sql`alter table wynn_guild_info add xp bigint not null default 0;`
}),
]
export const runMigrations = async (pg: Sql) => {
await pg.begin(async (sql)=>{
await sql`
create table if not exists migration_version (version int)
`
await sql`
insert into migration_version
select 0
where 0=(select count(*) from migration_version)
`
const [{version}] = await sql`
select version from migration_version limit 1
`
const targetVersion = migrations.length
if(version == targetVersion) {
return
}
if (version > targetVersion) {
console.log(`version ${version} is greater than the latest migration ${targetVersion}, nothing to do`)
return
}
console.log(`running migrations from version ${version}`)
for (let i = version+1; i <= targetVersion; i++) {
const m = migrations[i-1]
console.log(`running migration ${i}_${m.name}`)
await m.up(sql)
await sql`
update migration_version set version=${i}
`
}
console.log(`done running db migrations`)
});
}