Skip to content

Commit

Permalink
fixes after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeBu committed Jul 26, 2024
1 parent 6623b8e commit 72a5b92
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 97 deletions.
6 changes: 1 addition & 5 deletions api/scripts/load-git-repo-in-pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,7 @@ const insertInstances = async ({ instanceRows, db }: { instanceRows: Db.Instance
console.info("Number of instances to insert : ", instanceRows.length);
await db.transaction().execute(async trx => {
await trx.deleteFrom("instances").execute();

await trx
.insertInto("instances")
.values(instanceRows.map(({ otherSoftwareWikidataIds, ...row }) => row))
.executeTakeFirst();
await trx.insertInto("instances").values(instanceRows).executeTakeFirst();
});
};

Expand Down
3 changes: 0 additions & 3 deletions api/scripts/migration/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const zInstanceRow = z.object({
"organization": z.string(),
"targetAudience": z.string(),
"publicUrl": z.string().optional(),
"otherSoftwareWikidataIds": z.array(z.string()),
"addedByAgentEmail": z.string(),
"referencedSinceTime": z.number(),
"updateTime": z.number()
Expand Down Expand Up @@ -43,7 +42,6 @@ fs.writeFileSync(
id,
mainSoftwareSillId,
organization,
otherSoftwareWikidataIds,
publicUrl,
targetAudience,
addedByAgentEmail,
Expand All @@ -58,7 +56,6 @@ fs.writeFileSync(
id,
mainSoftwareSillId,
organization,
otherSoftwareWikidataIds,
publicUrl,
targetAudience,
addedByAgentEmail,
Expand Down
78 changes: 16 additions & 62 deletions api/src/core/adapters/dbApi/kysely/createPgInstanceRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,30 @@ import { Kysely } from "kysely";
import type { Equals } from "tsafe";
import { assert } from "tsafe/assert";
import { InstanceRepository } from "../../../ports/DbApiV2";
import { ParentSoftwareExternalData } from "../../../ports/GetSoftwareExternalData";
import { Instance } from "../../../usecases/readWriteSillData";
import { Database } from "./kysely.database";
import { jsonBuildObject } from "./kysely.utils";

export const createPgInstanceRepository = (db: Kysely<Database>): InstanceRepository => ({
create: async ({ fromData, agentEmail }) => {
const { mainSoftwareSillId, organization, targetAudience, publicUrl, otherSoftwareWikidataIds, ...rest } =
fromData;
const { mainSoftwareSillId, organization, targetAudience, publicUrl, ...rest } = fromData;
assert<Equals<typeof rest, {}>>();

const now = Date.now();

await db.transaction().execute(async trx => {
const { instanceId } = await trx
.insertInto("instances")
.values({
addedByAgentEmail: agentEmail,
updateTime: now,
referencedSinceTime: now,
mainSoftwareSillId,
organization,
targetAudience,
publicUrl
})
.returning("id as instanceId")
.executeTakeFirstOrThrow();

if (otherSoftwareWikidataIds.length === 0) return;
await trx
.insertInto("instances__other_external_softwares")
.values(
otherSoftwareWikidataIds.map(externalId => ({
instanceId,
externalId
}))
)
.execute();
});
await db
.insertInto("instances")
.values({
addedByAgentEmail: agentEmail,
updateTime: now,
referencedSinceTime: now,
mainSoftwareSillId,
organization,
targetAudience,
publicUrl
})
.executeTakeFirstOrThrow();
},
update: async ({ fromData, instanceId }) => {
const { mainSoftwareSillId, organization, targetAudience, publicUrl, otherSoftwareWikidataIds, ...rest } =
fromData;
const { mainSoftwareSillId, organization, targetAudience, publicUrl, ...rest } = fromData;
assert<Equals<typeof rest, {}>>();

const now = Date.now();
Expand All @@ -63,41 +44,14 @@ export const createPgInstanceRepository = (db: Kysely<Database>): InstanceReposi
getAll: async () =>
db
.selectFrom("instances as i")
.leftJoin("instances__other_external_softwares as ioes", "ioes.instanceId", "i.id")
.leftJoin("software_external_datas as ext", "ext.externalId", "ioes.externalId")
.groupBy(["i.id"])
.select([
"i.id",
"i.mainSoftwareSillId",
"i.organization",
"i.targetAudience",
"i.publicUrl",
// ({ fn }) =>
// fn
// .jsonAgg("ext")
// .filterWhere("ext.externalId", "is not", null)
// .distinct()
// .$castTo<ParentSoftwareExternalData[]>()
// .as("otherWikidataSoftwares")
({ ref, fn }) =>
fn
.jsonAgg(
jsonBuildObject({
externalId: ref("ext.externalId"),
label: ref("ext.label"),
description: ref("ext.description")
}).$castTo<ParentSoftwareExternalData>()
)
.filterWhere("ext.externalId", "is not", null)
.as("otherWikidataSoftwares")
])
.select(["i.id", "i.mainSoftwareSillId", "i.organization", "i.targetAudience", "i.publicUrl"])
.execute()
.then(instances =>
instances.map(
(instance): Instance => ({
...instance,
publicUrl: instance.publicUrl ?? undefined,
otherWikidataSoftwares: instance.otherWikidataSoftwares
publicUrl: instance.publicUrl ?? undefined
})
)
)
Expand Down
6 changes: 0 additions & 6 deletions api/src/core/adapters/dbApi/kysely/kysely.database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export type Database = {
software_referents: SoftwareReferentsTable;
software_users: SoftwareUsersTable;
instances: InstancesTable;
instances__other_external_softwares: InstancesOtherExternalSoftwaresTable;
softwares: SoftwaresTable;
software_external_datas: SoftwareExternalDatasTable;
softwares__similar_software_external_datas: SimilarExternalSoftwareExternalDataTable;
Expand Down Expand Up @@ -50,11 +49,6 @@ type InstancesTable = {
updateTime: number;
};

type InstancesOtherExternalSoftwaresTable = {
instanceId: number;
externalId: ExternalId;
};

type ExternalId = string;
type ExternalDataOrigin = "wikidata" | "HAL";
type LocalizedString = Partial<Record<string, string>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,10 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn("referencedSinceTime", "bigint", col => col.notNull())
.addColumn("updateTime", "bigint", col => col.notNull())
.execute();

await db.schema
.createTable("instances__other_external_softwares")
.addColumn("instanceId", "integer", col => col.notNull().references("instances.id").onDelete("cascade"))
.addColumn("externalId", "text", col => col.notNull())
.execute();
}

export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable("softwares__similar_software_external_datas").execute();
await db.schema.dropTable("instances__other_external_softwares").execute();
await db.schema.dropTable("software_external_datas").execute();
await db.schema.dropTable("instances").execute();
await db.schema.dropTable("software_referents").execute();
Expand Down
14 changes: 2 additions & 12 deletions api/src/core/adapters/dbApi/kysely/pgDbApi.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Kysely } from "kysely";
import * as fs from "node:fs";
import { beforeEach, describe, expect, it, afterEach } from "vitest";
import { expectPromiseToFailWith, expectToEqual } from "../../../../tools/test.helpers";
import { compiledDataPrivateToPublic } from "../../../ports/CompileData";
import { Agent, DbApiV2 } from "../../../ports/DbApiV2";
import { SoftwareExternalData } from "../../../ports/GetSoftwareExternalData";
import { SoftwareFormData } from "../../../usecases/readWriteSillData";
Expand Down Expand Up @@ -182,8 +180,7 @@ describe("pgDbApi", () => {
mainSoftwareSillId: softwareId,
organization: "test-orga",
targetAudience: "test-audience",
publicUrl: "https://example.com",
otherSoftwareWikidataIds: [externalId]
publicUrl: "https://example.com"
}
});

Expand All @@ -195,14 +192,7 @@ describe("pgDbApi", () => {
mainSoftwareSillId: softwareId,
organization: "test-orga",
targetAudience: "test-audience",
publicUrl: "https://example.com",
otherWikidataSoftwares: [
{
externalId,
label: softwareExternalData.label,
description: softwareExternalData.description
}
]
publicUrl: "https://example.com"
});
});
});
Expand Down
3 changes: 1 addition & 2 deletions api/src/core/usecases/readWriteSillData/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type {
ExternalDataOrigin,
ParentSoftwareExternalData,
SimilarSoftwareExternalData,
SoftwareExternalData
SimilarSoftwareExternalData
} from "../../ports/GetSoftwareExternalData";

export type ServiceProvider = {
Expand Down

0 comments on commit 72a5b92

Please sign in to comment.