mirror of
https://github.com/catdevnull/preciazo.git
synced 2024-11-22 06:16:18 +00:00
script para actualizar index
This commit is contained in:
parent
cdeb3c8d7d
commit
6cfe008b2b
2 changed files with 28 additions and 4 deletions
|
@ -196,10 +196,7 @@ export const sucursalesRelations = relations(sucursales, ({ one }) => ({
|
|||
}));
|
||||
|
||||
// para actualizar la tabla:
|
||||
// insert into productos_descripcion_index
|
||||
// select distinct id_producto, productos_descripcion, productos_marca from precios
|
||||
// on conflict do nothing;
|
||||
// probablemente se pueda poner un where en el select para solo seleccionar precios recientes (tipo id_dataset > X)
|
||||
// bun run scripts/refresh-descripciones-index.ts
|
||||
export const productos_descripcion_index = pgTable(
|
||||
"productos_descripcion_index",
|
||||
{
|
||||
|
|
27
sepa/scripts/refresh-descripciones-index.ts
Normal file
27
sepa/scripts/refresh-descripciones-index.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
import * as schema from "../db/schema";
|
||||
import { formatISO, subDays } from "date-fns";
|
||||
import { sql } from "drizzle-orm";
|
||||
|
||||
const db = drizzle(postgres(process.env.DATABASE_URL!), {
|
||||
schema,
|
||||
logger: true,
|
||||
});
|
||||
|
||||
const aWeekAgo = subDays(new Date(), 5);
|
||||
|
||||
db.execute(sql`
|
||||
insert into productos_descripcion_index
|
||||
select distinct id_producto, productos_descripcion, productos_marca from precios
|
||||
where id_dataset in (
|
||||
SELECT d1.id FROM datasets d1
|
||||
JOIN (
|
||||
SELECT id_comercio, MAX(date) as max_date
|
||||
FROM datasets
|
||||
WHERE date > ${formatISO(aWeekAgo, { representation: "date" })}
|
||||
GROUP BY id_comercio
|
||||
) d2 ON d1.id_comercio = d2.id_comercio AND d1.date = d2.max_date
|
||||
ORDER BY d1.id_comercio)
|
||||
on conflict do nothing;
|
||||
`);
|
Loading…
Reference in a new issue