Skip to content

Commit

Permalink
s5-1/feat: move all models to postgre
Browse files Browse the repository at this point in the history
  • Loading branch information
artembell committed Jun 14, 2024
1 parent 76009e0 commit df398a8
Show file tree
Hide file tree
Showing 17 changed files with 402 additions and 124 deletions.
29 changes: 29 additions & 0 deletions backend/prisma/migrations/20240421134316_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- CreateTable
CREATE TABLE "Folder" (
"dr_id" TEXT NOT NULL,
"dr_name" TEXT,
"dr_owner" TEXT NOT NULL,
"dr_path" TEXT[],
"dr_removed" BOOLEAN NOT NULL DEFAULT false,
"dr_parent_dir_id" TEXT NOT NULL,

CONSTRAINT "Folder_pkey" PRIMARY KEY ("dr_id")
);

-- CreateTable
CREATE TABLE "File" (
"fl_id" TEXT NOT NULL,
"fl_name" TEXT NOT NULL,
"fl_extension" TEXT NOT NULL,
"fl_owner" TEXT NOT NULL,
"fl_removed" BOOLEAN NOT NULL DEFAULT false,
"folderId" TEXT NOT NULL,

CONSTRAINT "File_pkey" PRIMARY KEY ("fl_id")
);

-- AddForeignKey
ALTER TABLE "Folder" ADD CONSTRAINT "Folder_dr_parent_dir_id_fkey" FOREIGN KEY ("dr_parent_dir_id") REFERENCES "Folder"("dr_id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "File" ADD CONSTRAINT "File_folderId_fkey" FOREIGN KEY ("folderId") REFERENCES "Folder"("dr_id") ON DELETE RESTRICT ON UPDATE CASCADE;
38 changes: 38 additions & 0 deletions backend/prisma/migrations/20240421135001_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Warnings:
- The primary key for the `File` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `fl_id` column on the `File` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The primary key for the `Folder` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `dr_id` column on the `Folder` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- Changed the type of `folderId` on the `File` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `dr_parent_dir_id` on the `Folder` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
*/
-- DropForeignKey
ALTER TABLE "File" DROP CONSTRAINT "File_folderId_fkey";

-- DropForeignKey
ALTER TABLE "Folder" DROP CONSTRAINT "Folder_dr_parent_dir_id_fkey";

-- AlterTable
ALTER TABLE "File" DROP CONSTRAINT "File_pkey",
DROP COLUMN "fl_id",
ADD COLUMN "fl_id" SERIAL NOT NULL,
DROP COLUMN "folderId",
ADD COLUMN "folderId" INTEGER NOT NULL,
ADD CONSTRAINT "File_pkey" PRIMARY KEY ("fl_id");

-- AlterTable
ALTER TABLE "Folder" DROP CONSTRAINT "Folder_pkey",
DROP COLUMN "dr_id",
ADD COLUMN "dr_id" SERIAL NOT NULL,
DROP COLUMN "dr_parent_dir_id",
ADD COLUMN "dr_parent_dir_id" INTEGER NOT NULL,
ADD CONSTRAINT "Folder_pkey" PRIMARY KEY ("dr_id");

-- AddForeignKey
ALTER TABLE "Folder" ADD CONSTRAINT "Folder_dr_parent_dir_id_fkey" FOREIGN KEY ("dr_parent_dir_id") REFERENCES "Folder"("dr_id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "File" ADD CONSTRAINT "File_folderId_fkey" FOREIGN KEY ("folderId") REFERENCES "Folder"("dr_id") ON DELETE RESTRICT ON UPDATE CASCADE;
41 changes: 41 additions & 0 deletions backend/prisma/migrations/20240421142255_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Warnings:
- The primary key for the `Folder` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `dr_id` on the `Folder` table. All the data in the column will be lost.
- You are about to drop the column `dr_name` on the `Folder` table. All the data in the column will be lost.
- You are about to drop the column `dr_owner` on the `Folder` table. All the data in the column will be lost.
- You are about to drop the column `dr_parent_dir_id` on the `Folder` table. All the data in the column will be lost.
- You are about to drop the column `dr_path` on the `Folder` table. All the data in the column will be lost.
- You are about to drop the column `dr_removed` on the `Folder` table. All the data in the column will be lost.
- Added the required column `fd_owner` to the `Folder` table without a default value. This is not possible if the table is not empty.
- Added the required column `fd_parent_folder_id` to the `Folder` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "File" DROP CONSTRAINT "File_folderId_fkey";

-- DropForeignKey
ALTER TABLE "Folder" DROP CONSTRAINT "Folder_dr_parent_dir_id_fkey";

-- AlterTable
ALTER TABLE "Folder" DROP CONSTRAINT "Folder_pkey",
DROP COLUMN "dr_id",
DROP COLUMN "dr_name",
DROP COLUMN "dr_owner",
DROP COLUMN "dr_parent_dir_id",
DROP COLUMN "dr_path",
DROP COLUMN "dr_removed",
ADD COLUMN "fd_id" SERIAL NOT NULL,
ADD COLUMN "fd_name" TEXT,
ADD COLUMN "fd_owner" TEXT NOT NULL,
ADD COLUMN "fd_parent_folder_id" INTEGER NOT NULL,
ADD COLUMN "fd_path" INTEGER[],
ADD COLUMN "fd_removed" BOOLEAN NOT NULL DEFAULT false,
ADD CONSTRAINT "Folder_pkey" PRIMARY KEY ("fd_id");

-- AddForeignKey
ALTER TABLE "Folder" ADD CONSTRAINT "Folder_fd_parent_folder_id_fkey" FOREIGN KEY ("fd_parent_folder_id") REFERENCES "Folder"("fd_id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "File" ADD CONSTRAINT "File_folderId_fkey" FOREIGN KEY ("folderId") REFERENCES "Folder"("fd_id") ON DELETE RESTRICT ON UPDATE CASCADE;
34 changes: 34 additions & 0 deletions backend/prisma/migrations/20240421145436_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Warnings:
- The primary key for the `File` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The primary key for the `Folder` table will be changed. If it partially fails, the table could be left without primary key constraint.
*/
-- DropForeignKey
ALTER TABLE "File" DROP CONSTRAINT "File_folderId_fkey";

-- DropForeignKey
ALTER TABLE "Folder" DROP CONSTRAINT "Folder_fd_parent_folder_id_fkey";

-- AlterTable
ALTER TABLE "File" DROP CONSTRAINT "File_pkey",
ALTER COLUMN "fl_id" DROP DEFAULT,
ALTER COLUMN "fl_id" SET DATA TYPE TEXT,
ALTER COLUMN "folderId" SET DATA TYPE TEXT,
ADD CONSTRAINT "File_pkey" PRIMARY KEY ("fl_id");
DROP SEQUENCE "File_fl_id_seq";

-- AlterTable
ALTER TABLE "Folder" DROP CONSTRAINT "Folder_pkey",
ALTER COLUMN "fd_id" DROP DEFAULT,
ALTER COLUMN "fd_id" SET DATA TYPE TEXT,
ALTER COLUMN "fd_parent_folder_id" SET DATA TYPE TEXT,
ADD CONSTRAINT "Folder_pkey" PRIMARY KEY ("fd_id");
DROP SEQUENCE "Folder_fd_id_seq";

-- AddForeignKey
ALTER TABLE "Folder" ADD CONSTRAINT "Folder_fd_parent_folder_id_fkey" FOREIGN KEY ("fd_parent_folder_id") REFERENCES "Folder"("fd_id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "File" ADD CONSTRAINT "File_folderId_fkey" FOREIGN KEY ("folderId") REFERENCES "Folder"("fd_id") ON DELETE RESTRICT ON UPDATE CASCADE;
2 changes: 2 additions & 0 deletions backend/prisma/migrations/20240421145507_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Folder" ALTER COLUMN "fd_path" SET DATA TYPE TEXT[];
8 changes: 8 additions & 0 deletions backend/prisma/migrations/20240421150856_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- DropForeignKey
ALTER TABLE "Folder" DROP CONSTRAINT "Folder_fd_parent_folder_id_fkey";

-- AlterTable
ALTER TABLE "Folder" ALTER COLUMN "fd_parent_folder_id" DROP NOT NULL;

-- AddForeignKey
ALTER TABLE "Folder" ADD CONSTRAINT "Folder_fd_parent_folder_id_fkey" FOREIGN KEY ("fd_parent_folder_id") REFERENCES "Folder"("fd_id") ON DELETE SET NULL ON UPDATE CASCADE;
5 changes: 5 additions & 0 deletions backend/prisma/migrations/20240421151034_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- DropForeignKey
ALTER TABLE "Folder" DROP CONSTRAINT "Folder_fd_parent_folder_id_fkey";

-- AddForeignKey
ALTER TABLE "Folder" ADD CONSTRAINT "Folder_fd_parent_folder_id_fkey" FOREIGN KEY ("fd_parent_folder_id") REFERENCES "Folder"("fd_id") ON DELETE CASCADE ON UPDATE CASCADE;
52 changes: 52 additions & 0 deletions backend/prisma/schema.postgresql.dev.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,55 @@ model User {
username String @unique
password String?
}

model Folder {
id String @id @default(uuid()) @map("fd_id")
name String? @map("fd_name")
owner String @map("fd_owner")
path String[] @map("fd_path")
removed Boolean @default(false) @map("fd_removed")
parentFolder Folder? @relation("FolderParents", fields: [parentFolderId], references: [id], onDelete: Cascade, onUpdate: Cascade)
childrenFolders Folder[] @relation("FolderParents")
parentFolderId String? @map("fd_parent_folder_id")
childrenFiles File[]
}

model File {
id String @id @default(uuid()) @map("fl_id")
name String @map("fl_name")
extension String @map("fl_extension")
owner String @map("fl_owner")
removed Boolean @default(false) @map("fl_removed")
folder Folder @relation(fields: [folderId], references: [id])
folderId String
}

// model FileAccessList {
// id String @id
// file File
// fileId
// user User @relation(fields: [userId], references: [id])
// userId String
// rightListId

// @@map("file_access_list")
// }

// model FileAccessRightsListToUsers {
// id String @id
// listId
// userId
// }

// model FileAccessRightsList {
// id String @id
// rightId
// }

// model FileAccessRights {
// id String @id
// value String
// description String
// }
3 changes: 2 additions & 1 deletion backend/src/modules/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { DbUserUniqueConstraintError } from '@src/modules/errors/ErrorUniqueCons
import { UserRegisterError } from '@src/modules/errors/ErrorUserRegister';
import { CannotFullfillRequestError } from '@src/modules/errors/logic/CannotFullfillRequest';
import { GenericServerError } from '@src/modules/errors/logic/GenericServerError';
import { FileStructureRepository, IFileStructureRepository } from '@src/modules/file-structure/file-structure.service';
import { FileStructureRepository } from '@src/modules/file-structure/file-structure.service';
import { IFileStructureRepository } from '@src/modules/file-structure/file-structure.type';
import { FileSystemService, IFileSystemService } from '@src/modules/file-system/file-system.service';

export interface IAuthService {
Expand Down
22 changes: 10 additions & 12 deletions backend/src/modules/file-structure/file-structure.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { Test, TestingModule } from '@nestjs/testing';
import { User } from '../user/models/User';
import {
CreateUserRootFolderStructure,
FileStructureRepository,
IFileStructureRepository,
TFileRepository,
} from './file-structure.service';
import { FileStructureRepository } from './file-structure.service';
import { DbFileRecordDoesNotExist } from '../errors/db/DbFileRecordDoesNotExistError';
import { ObjectId } from 'bson';
import { TestConfigService } from '../config/test.config.service';
import { IFileStructureRepository, CreateUserRootFolderStructure, TFileRepository } from './file-structure.type';

describe('FileStructureRepository: ', () => {
let service: IFileStructureRepository;
Expand Down Expand Up @@ -39,7 +35,8 @@ describe('FileStructureRepository: ', () => {
const result = await service.createUserRootFolder(user.id);

expect(result).toMatchObject<Omit<CreateUserRootFolderStructure, 'id'>>({
parentId: '',
// parentId: ,
parentFolderId: null,
name: user.id,
});
});
Expand All @@ -58,7 +55,7 @@ describe('FileStructureRepository: ', () => {
const nestedFolder1 = await service.createFolder(user.id, nestedFolder1Name, parentFolder.id);
expect(nestedFolder1).toMatchObject<Omit<CreateUserRootFolderStructure, 'id'>>({
name: nestedFolder1Name,
parentId: parentFolder.id,
parentFolderId: parentFolder.id,
});

const children = await service.getChildrenFolders(parentFolder.id);
Expand All @@ -68,7 +65,8 @@ describe('FileStructureRepository: ', () => {
const nestedFolder2 = await service.createFolder(user.id, nestedFolder2Name, parentFolder.id);
expect(nestedFolder2).toMatchObject<Omit<CreateUserRootFolderStructure, 'id'>>({
name: nestedFolder2Name,
parentId: parentFolder.id,
parentFolderId: parentFolder.id,

});

const children2 = await service.getChildrenFolders(parentFolder.id);
Expand All @@ -78,7 +76,7 @@ describe('FileStructureRepository: ', () => {
const nestedFolder11 = await service.createFolder(user.id, nestedFolder11Name, nestedFolder1.id);
expect(nestedFolder11).toMatchObject<Omit<CreateUserRootFolderStructure, 'id'>>({
name: nestedFolder11Name,
parentId: nestedFolder1.id,
parentFolderId: nestedFolder1.id,
});

const children3 = await service.getChildrenFolders(nestedFolder1.id);
Expand Down Expand Up @@ -147,13 +145,13 @@ describe('FileStructureRepository: ', () => {

/** Create root user folder */
try {
await service.removeFile('unknown', false);
await service.removeFile('unkown', false);
} catch (e: unknown) {
expect(e).toBeInstanceOf(DbFileRecordDoesNotExist);
}

try {
await service.removeFile(new ObjectId().toString(), false);
await service.removeFile('', false);
} catch (e: unknown) {
expect(e).toBeInstanceOf(DbFileRecordDoesNotExist);
}
Expand Down
Loading

0 comments on commit df398a8

Please sign in to comment.