Skip to content

Commit

Permalink
- fix data type for PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
shepilov committed Feb 19, 2024
1 parent 4ac8501 commit c0995ca
Show file tree
Hide file tree
Showing 18 changed files with 1,184 additions and 966 deletions.
1,140 changes: 628 additions & 512 deletions tdrive/backend/node/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tdrive/backend/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"@types/eslint": "^7.2.3",
"@types/fluent-ffmpeg": "^2.1.20",
"@types/html-to-text": "^8.1.1",
"@types/jest": "^29.1.0",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.14.165",
"@types/node": "^18.11",
"@types/node-cron": "^3.0.0",
Expand All @@ -88,13 +88,13 @@
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-unused-imports": "^2.0.0",
"form-auto-content": "^2.2.0",
"jest": "^29.1.0",
"jest": "^29.7.0",
"jest-mock-extended": "^3.0.4",
"nodemon": "^2.0.22",
"pino-pretty": "^10.0.0",
"prettier": "^2.1.2",
"rimraf": "^3.0.2",
"ts-jest": "^29.1.0",
"ts-jest": "^29.1.2",
"ts-node": "^9.0.0",
"ts-node-dev": "^1.1.8",
"tsc-watch": "^4.2.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,19 @@ export class PostgresConnector extends AbstractConnector<PostgresConnectionOptio

private async alterTablePrimaryKey(entity: EntityDefinition) {
if (entity.options.primaryKey) {
const query = `ALTER TABLE "${entity.name}" ADD PRIMARY KEY (
${entity.options.primaryKey.join(", ")});`;
const query = `
do $$
begin
IF NOT EXISTS
(SELECT constraint_name
FROM information_schema.table_constraints
WHERE table_name = '${entity.name}'
and constraint_type = 'PRIMARY KEY')
THEN
ALTER TABLE "${entity.name}" ADD PRIMARY KEY (
${entity.options.primaryKey.join(", ")});
END IF;
end $$;`;
try {
await this.client.query(query);
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export default class EntityManager<EntityType extends Record<string, any>> {
case "uuid":
entity[columnsDefinition[pk].nodename] = uuidv4();
break;
case "string":
entity[columnsDefinition[pk].nodename] = uuidv4();
break;
case "timeuuid":
entity[columnsDefinition[pk].nodename] = uuidv1();
break;
Expand Down
6 changes: 3 additions & 3 deletions tdrive/backend/node/src/services/documents/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ export class DocumentsService {

id = id || this.ROOT;

let entity = null;
//Get requested entity
let entity = isVirtualFolder(id)
? null
: await this.repository.findOne({ company_id: context.company.id, id }, {}, context);
if (isUserRootFolder(id)) {
entity = await this.repository.findOne({ id }, {}, context);
} else if (!isVirtualFolder(id)) {
entity = await this.repository.findOne({ company_id: context.company.id, id }, {}, context);
}

if (!entity && !isVirtualFolder(id)) {
Expand Down
4 changes: 3 additions & 1 deletion tdrive/backend/node/src/services/documents/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ const TRASH: TrashType = "trash";
const SHARED_WITH_ME: SharedWithMeType = "shared_with_me";

export const isVirtualFolder = (id: string) => {
return id === ROOT || id === TRASH || id.startsWith("trash_") || id == SHARED_WITH_ME;
return (
id == null || id === ROOT || id === TRASH || id.startsWith("trash_") || id == SHARED_WITH_ME
);
};

export const isUserRootFolder = (id: string) => {
Expand Down
4 changes: 1 addition & 3 deletions tdrive/backend/node/src/services/files/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,5 @@ export class FileServiceImpl {
}

function getFilePath(entity: File): string {
return `${gr.platformServices.storage.getHomeDir()}/files/${entity.company_id}/${
entity.user_id || "anonymous"
}/${entity.id}`;
return `/files/${entity.company_id}/${entity.user_id || "anonymous"}/${entity.id}`;
}
2 changes: 1 addition & 1 deletion tdrive/backend/node/src/services/global-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { StatisticsServiceImpl } from "./statistics/service";
import { TagsService } from "./tags/services/tags";
import { CompanyServiceImpl } from "./user/services/companies";
import { UserExternalLinksServiceImpl } from "./user/services/external_links";
import { UserServiceImpl } from "./user/services/users/service";
import { UserServiceImpl } from "./user/services/users/user-service";
import { WorkspaceServiceImpl } from "./workspaces/services/workspace";

import { PreviewEngine } from "./previews/services/files/engine";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,14 @@ export class UserServiceImpl {
{
id: "user_" + user.id,
is_directory: true,
parent_id: null,
},
{
user: { id: user.id },
company: { id: config.get<string>("drive.defaultCompany") },
} as CompanyExecutionContext,
);
userRoot.parent_id = null;
await this.driveFileRepository.save(userRoot);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ describe('The Postgres Connector module', () => {

//then
expect(dbQuerySpy).toHaveBeenCalledTimes(6 + NUMBER_OF_HEALTHCHECK_CALLS);
expect(normalizeWhitespace(dbQuerySpy.mock.calls[1][0])).toBe(normalizeWhitespace(`CREATE TABLE IF NOT EXISTS "test_table" ( company_id UUID, id UUID, parent_id UUID, is_in_trash BOOLEAN, tags TEXT, added BIGINT );`))
expect(normalizeWhitespace(dbQuerySpy.mock.calls[1][0])).toBe(normalizeWhitespace(`CREATE TABLE IF NOT EXISTS "test_table" ( company_id UUID, id TEXT, parent_id UUID, is_in_trash BOOLEAN, tags TEXT, added BIGINT );`))
expect(normalizeWhitespace(dbQuerySpy.mock.calls[2][0])).toBe(normalizeWhitespace("SELECT table_name, column_name, data_type FROM information_schema.columns WHERE table_name = $1"));
expect(dbQuerySpy.mock.calls[2][1]).toStrictEqual(["test_table"])
expect(normalizeWhitespace(dbQuerySpy.mock.calls[3][0])).toBe(`ALTER TABLE "test_table" ADD COLUMN id UUID, ADD COLUMN is_in_trash BOOLEAN, ADD COLUMN tags TEXT, ADD COLUMN added BIGINT`)
expect(normalizeWhitespace(dbQuerySpy.mock.calls[4][0])).toBe(`ALTER TABLE "test_table" ADD PRIMARY KEY ( company_id, id);`)
expect(normalizeWhitespace(dbQuerySpy.mock.calls[3][0])).toBe(`ALTER TABLE "test_table" ADD COLUMN id TEXT, ADD COLUMN is_in_trash BOOLEAN, ADD COLUMN tags TEXT, ADD COLUMN added BIGINT`)
expect(normalizeWhitespace(dbQuerySpy.mock.calls[4][0])).toBe(`do $$ begin IF NOT EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'test_table' and constraint_type = 'PRIMARY KEY') THEN ALTER TABLE "test_table" ADD PRIMARY KEY ( company_id, id); END IF; end $$;`)
expect(normalizeWhitespace(dbQuerySpy.mock.calls[5][0])).toBe(`CREATE INDEX IF NOT EXISTS index_test_table_company_id_parent_id ON "test_table" ((company_id), parent_id)`)
expect(normalizeWhitespace(dbQuerySpy.mock.calls[6][0])).toBe(`CREATE INDEX IF NOT EXISTS index_test_table_company_id_is_in_trash ON "test_table" ((company_id), is_in_trash)`)
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class TestDbEntity {
company_id: string;

@Type(() => String)
@Column("id", "uuid", { generator: "uuid" })
@Column("id", "string", { generator: "uuid" })
// @ts-ignore
id: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DriveFileDTOBuilder } from "../../../../../../../src/services/documents
import { ListResult } from "../../../../../../../src/core/platform/framework/api/crud-service";
import { DriveFile } from "../../../../../../../src/services/documents/entities/drive-file";
import { CompanyExecutionContext } from "../../../../../../../src/services/applications/web/types";
import { UserServiceImpl } from "../../../../../../../src/services/user/services/users/service";
import { UserServiceImpl } from "../../../../../../../src/services/user/services/users/user-service";

describe("Drive File DTO Builder Test", () => {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { jest } from "@jest/globals";
import { UserServiceImpl } from "../../../../../../../src/services/user/services/users/user-service";
import Repository from "../../../../../../../src/core/platform/services/database/services/orm/repository/repository";
import { DriveFile } from "../../../../../../../src/services/documents/entities/drive-file";
import { randomUUID } from "crypto";
import User, { UserPrimaryKey } from "../../../../../../../src/services/user/entities/user";
describe("The UsersService", () => {

const subj: UserServiceImpl = new UserServiceImpl();

beforeEach(async () => {
const mockRepository = {
save: jest.fn(() => Promise.resolve(null)),
};

const mockConfig = {
get: jest.fn(() => Promise.resolve("00000")),
}

subj.driveFileRepository = mockRepository as unknown as Repository<DriveFile>;
});

afterEach(() => {
jest.clearAllMocks();
});

test("Test creating default folder for the user", async () => {
//given
const user = { id: randomUUID()};
let actualDoc:DriveFile = null;
let saveSpy = jest.spyOn(subj.driveFileRepository, "save");
saveSpy.mockImplementation(
jest.fn(async (doc: DriveFile) => {
actualDoc = doc;
})
);

//when
await subj.createUserRootFolder(user as User)

//then
expect(actualDoc).toBeDefined();
expect(actualDoc.id).toBeDefined();
expect(actualDoc.id.startsWith("user_")).toBeTruthy()
expect(actualDoc.parent_id).toBeNull();

})

});
Loading

0 comments on commit c0995ca

Please sign in to comment.