Skip to content

Commit

Permalink
🫨 fix: apply schema changes, and fix associated tests and fe
Browse files Browse the repository at this point in the history
  • Loading branch information
in-mai-space authored Jan 9, 2025
1 parent d992430 commit 150334d
Show file tree
Hide file tree
Showing 21 changed files with 779 additions and 91 deletions.
5 changes: 0 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,6 @@ task backend:prod
task frontend:dev
```

Or run both of them at the same time
```bash
task start
```

-----

### Add new libraries or packages
Expand Down
21 changes: 11 additions & 10 deletions backend/src/database/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { migrate } from "drizzle-orm/postgres-js/migrator";
import { Configuration } from "../types/config";

export const automigrateDB = async (db: PostgresJsDatabase, config: Configuration) => {
const originalLog = console.log;
console.log = () => {};

try {
await migrate(db, config.automigrate);
} catch (error) {
console.error(error);
console.log("Failed to auto-migrate database");
} finally {
console.log = originalLog;
if (config.environment !== "production") {
const originalLog = console.log;
console.log = () => {};
try {
await migrate(db, config.automigrate);
} catch (error) {
console.error(error);
console.log("Failed to auto-migrate database");
} finally {
console.log = originalLog;
}
}
};
2 changes: 0 additions & 2 deletions backend/src/database/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
notificationsTable,
invitationsTable,
linksTable,
mediaTable,
likesTable,
commentsTable,
} from "../entities/schema";
Expand All @@ -22,7 +21,6 @@ export const resetDB = async (db: PostgresJsDatabase) => {
notificationsTable,
invitationsTable,
linksTable,
mediaTable,
likesTable,
commentsTable,
};
Expand Down
22 changes: 4 additions & 18 deletions backend/src/entities/schema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { timestamp, uuid, pgEnum, pgTable, varchar } from "drizzle-orm/pg-core";
import { timestamp, uuid, pgEnum, pgTable, varchar, boolean } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";

export const ageGroupEnum = pgEnum("ageGroup", ["CHILD", "TEEN", "ADULT", "SENIOR"]);
export const userModeEnum = pgEnum("mode", ["BASIC", "ADVANCED"]);
export const mediaTypeEnum = pgEnum("mediaType", ["IMAGE", "VIDEO", "AUDIO"]);
export const memberRoleEnum = pgEnum("role", ["MEMBER", "MANAGER"]);
export const referenceTypeEnum = pgEnum("referenceType", [
"POST",
Expand All @@ -18,9 +16,9 @@ export const usersTable = pgTable("users", {
id: uuid().primaryKey().defaultRandom(),
name: varchar({ length: 100 }).notNull(),
username: varchar({ length: 100 }).notNull().unique(),
ageGroup: ageGroupEnum().notNull(),
mode: userModeEnum().notNull().default("BASIC"),
profilePhoto: varchar(),
notificationsEnabled: boolean().notNull().default(true),
deviceTokens: varchar({ length: 152 }).array().default([]),
});

Expand All @@ -43,19 +41,7 @@ export const postsTable = pgTable("posts", {
.references(() => usersTable.id, { onDelete: "cascade" }),
createdAt: timestamp().notNull().defaultNow(),
caption: varchar({ length: 500 }),
thumbnail: varchar(),
});

export const mediaTable = pgTable("media", {
id: uuid().primaryKey().defaultRandom(),
mediaType: mediaTypeEnum().notNull(),
media: varchar().notNull(),
postId: uuid()
.notNull()
.references(() => postsTable.id, { onDelete: "cascade" }),
commentId: uuid()
.notNull()
.references(() => commentsTable.id, { onDelete: "cascade" }),
media: varchar().array().default([]),
});

export const membersTable = pgTable("members", {
Expand Down Expand Up @@ -88,6 +74,7 @@ export const commentsTable = pgTable("comments", {
.notNull()
.references(() => postsTable.id, { onDelete: "cascade" }),
content: varchar({ length: 500 }),
voiceMemo: varchar(),
});

export const notificationsTable = pgTable("notifications", {
Expand Down Expand Up @@ -154,7 +141,6 @@ export const postRelations = relations(postsTable, ({ one, many }) => ({
fields: [postsTable.userId],
references: [usersTable.id],
}),
media: many(mediaTable),
comments: many(commentsTable),
likes: many(likesTable),
}));
4 changes: 0 additions & 4 deletions backend/src/entities/users/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Mode } from "../../constants/database";
import { AgeGroup } from "../../constants/database";
import { InternalServerError, NotFoundError } from "../../utilities/errors/app-error";
import { handleServiceError } from "../../utilities/errors/service-error";
import { CreateUserPayload, UpdateUserPayload, User } from "./validator";
Expand All @@ -23,9 +21,7 @@ export class UserServiceImpl implements UserService {

async createUser(payload: CreateUserPayload): Promise<User> {
const createUserImpl = async () => {
const mode = payload.ageGroup === AgeGroup.SENIOR ? Mode.BASIC : Mode.ADVANCED;
const user = await this.userTransaction.insertUser({
mode,
...payload,
});
if (!user) {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/entities/users/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export class UserTransactionImpl implements UserTransaction {
.set({
name: payload.name,
username: payload.username,
ageGroup: payload.ageGroup,
mode: payload.mode,
profilePhoto: payload.profilePhoto,
deviceTokens: payload.deviceTokens,
notificationsEnabled: payload.notificationsEnabled,
})
.where(eq(usersTable.id, id))
.returning();
Expand Down
22 changes: 22 additions & 0 deletions backend/src/migrations/20250109030057_curious_proteus.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
DROP TABLE IF EXISTS "media" CASCADE; -- statement-breakpoint

DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_name = 'posts' AND column_name = 'thumbnail'
) THEN
ALTER TABLE "posts" RENAME COLUMN "thumbnail" TO "media";
END IF;
END $$; -- statement-breakpoint

ALTER TABLE "comments" ADD COLUMN IF NOT EXISTS "voiceMemo" varchar; -- statement-breakpoint

ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "notificationsEnabled" boolean DEFAULT true NOT NULL; -- statement-breakpoint

ALTER TABLE "users" DROP COLUMN IF EXISTS "ageGroup"; -- statement-breakpoint

DROP TYPE IF EXISTS "public"."ageGroup"; -- statement-breakpoint

DROP TYPE IF EXISTS "public"."mediaType"; -- statement-breakpoint
Loading

0 comments on commit 150334d

Please sign in to comment.