diff --git a/prisma/migrations/20241023102925_update_stats_type/migration.sql b/prisma/migrations/20241023102925_update_stats_type/migration.sql new file mode 100644 index 0000000000000000000000000000000000000000..d487783e936e99ad092a86abed05e8dd8fb621a6 --- /dev/null +++ b/prisma/migrations/20241023102925_update_stats_type/migration.sql @@ -0,0 +1,11 @@ +/* + Warnings: + + - The primary key for the `Stats` table will be changed. If it partially fails, the table could be left without primary key constraint. + - Added the required column `id` to the `Stats` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "Stats" DROP CONSTRAINT "Stats_pkey", +ADD COLUMN "id" TEXT NOT NULL, +ADD CONSTRAINT "Stats_pkey" PRIMARY KEY ("id"); diff --git a/prisma/models/stats.prisma b/prisma/models/stats.prisma index a35a81424576eef2f7dc3f8b542764d46bc16f6b..0394d83452f4e305225646e5663b61d0522a3e14 100644 --- a/prisma/models/stats.prisma +++ b/prisma/models/stats.prisma @@ -8,6 +8,9 @@ model Stats { /// L'écart entre la moyenne et la valeur minimum. ecartMoyenneMin Int @default(0) + /// Id de ces statistiques. + id String @id + /// La législature de ces statistiques. legislature String @@ -36,7 +39,7 @@ model Stats { q100 Int @default(0) /// L'activité mesurée - type StatsType @id + type StatsType } enum StatsType { diff --git a/prisma/swagger/json-schema.json b/prisma/swagger/json-schema.json index c0cff2b70cd6e37422209340c9b3dee877a79b23..9ca56326337afc9f9e70b80b64bb5bb53bf047b2 100644 --- a/prisma/swagger/json-schema.json +++ b/prisma/swagger/json-schema.json @@ -4892,6 +4892,10 @@ "default": 0, "description": "L'écart entre la moyenne et la valeur minimum." }, + "id": { + "type": "string", + "description": "Id de ces statistiques." + }, "legislature": { "type": "string", "description": "La législature de ces statistiques." diff --git a/src/scripts/update_stats.ts b/src/scripts/update_stats.ts index b7b127adeb6adddfc256e61bc663d6d55639f4c2..b7117da043384d23602e0ab7ee33ac57f27291ed 100644 --- a/src/scripts/update_stats.ts +++ b/src/scripts/update_stats.ts @@ -93,15 +93,18 @@ export async function updateAmendementStats( ...quantiles, } + const type = StatsType.amendements + const id = `${legislature}-${type}`.toUpperCase() + await prisma.stats.upsert({ where: { - type: StatsType.amendements, - legislature: legislature.toString(), + id, }, update: payload, create: { - type: StatsType.amendements, legislature: legislature.toString(), + id, + type, ...payload, } }) @@ -187,15 +190,18 @@ export async function updateInterventionStats( ...quantiles, } + const type = StatsType.interventions + const id = `${legislature}-${type}`.toUpperCase() + await prisma.stats.upsert({ where: { - type: StatsType.interventions, - legislature: legislature.toString(), + id, }, update: payload, create: { - type: StatsType.interventions, + id, legislature: legislature.toString(), + type, ...payload, } }) @@ -282,15 +288,18 @@ export async function updateQuestionStats( ...quantiles, } + const type = StatsType.questions + const id = `${legislature}-${type}`.toUpperCase() + await prisma.stats.upsert({ where: { - type: StatsType.questions, - legislature: legislature.toString(), + id, }, update: payload, create: { - type: StatsType.questions, + id, legislature: legislature.toString(), + type, ...payload, } }) @@ -325,15 +334,17 @@ async function upsertZeroStats( q100: 0, } + const id = `${legislature}-${type}`.toUpperCase() + await prisma.stats.upsert({ where: { - type: type, - legislature: legislature.toString(), + id, }, update: zeroPayload, create: { - type: type, + id, legislature: legislature.toString(), + type, ...zeroPayload, } })