Hook up syncing with postgres
This commit is contained in:
@@ -6,12 +6,26 @@ import { log } from "../util";
|
||||
// SYNC RUNS
|
||||
// =====================
|
||||
{
|
||||
log.info({ name: "sync_runs" }, "Creating table");
|
||||
await db.schema.dropTable("sync_runs").ifExists().execute()
|
||||
await db.schema.createTable("sync_runs")
|
||||
log.info({ name: "product_syncs" }, "Creating table");
|
||||
await db.schema.dropTable("product_syncs").ifExists().execute()
|
||||
await db.schema.createTable("product_syncs")
|
||||
.$call(addDefaultColumns)
|
||||
.addColumn("completed_at", "timestamptz")
|
||||
.addColumn("printful_product_id_filter", sql`integer[]`)
|
||||
.addColumn("syncing_printful_product_ids", sql`integer[]`, (cb) => cb.notNull())
|
||||
.addColumn("started_at", "timestamptz", (cb) => cb.unique())
|
||||
.addColumn("ended_at", "timestamptz")
|
||||
.addColumn("has_failed", "boolean", (cb) => cb.notNull().defaultTo(false))
|
||||
.execute()
|
||||
|
||||
await db.schema.dropIndex("idx_one_active_product_sync").ifExists().execute();
|
||||
await db.schema.createIndex("idx_one_active_product_sync")
|
||||
.on("product_syncs")
|
||||
.unique()
|
||||
.column("ended_at")
|
||||
.where(sql.ref("started_at"), "is not", null)
|
||||
.where("ended_at", "is", null)
|
||||
.nullsNotDistinct()
|
||||
.execute();
|
||||
}
|
||||
|
||||
function addDefaultColumns(ctb: CreateTableBuilder<any, any>) {
|
||||
@@ -23,6 +37,5 @@ function addDefaultColumns(ctb: CreateTableBuilder<any, any>) {
|
||||
.addColumn("created_at", "timestamptz", (cb) => cb
|
||||
.notNull()
|
||||
.defaultTo(sql`now()`)
|
||||
.unique()
|
||||
)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user