diff --git a/drizzle/0005_bizarre_nightshade.sql b/drizzle/0005_bizarre_nightshade.sql new file mode 100644 index 0000000..1bce2b9 --- /dev/null +++ b/drizzle/0005_bizarre_nightshade.sql @@ -0,0 +1,5 @@ +DROP INDEX IF EXISTS "idx_name";--> statement-breakpoint +ALTER TABLE "profile_address" ADD COLUMN "is_primary" boolean DEFAULT false;--> statement-breakpoint +ALTER TABLE "profile_language" ADD COLUMN "level" "languagelevel" NOT NULL;--> statement-breakpoint +ALTER TABLE "address" DROP COLUMN IF EXISTS "is_primary";--> statement-breakpoint +ALTER TABLE "language" DROP COLUMN IF EXISTS "level"; \ No newline at end of file diff --git a/drizzle/meta/0005_snapshot.json b/drizzle/meta/0005_snapshot.json new file mode 100644 index 0000000..93be932 --- /dev/null +++ b/drizzle/meta/0005_snapshot.json @@ -0,0 +1,807 @@ +{ + "id": "291c017a-7442-4bd6-903c-64d3d5126f22", + "prevId": "09f88566-9464-4917-976e-75b07bcd1b43", + "version": "6", + "dialect": "postgresql", + "tables": { + "public.address": { + "name": "address", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "street": { + "name": "street", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "street_number": { + "name": "street_number", + "type": "varchar(10)", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "addresstype", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "city_id": { + "name": "city_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "address_city_id_city_id_fk": { + "name": "address_city_id_city_id_fk", + "tableFrom": "address", + "tableTo": "city", + "columnsFrom": ["city_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.city": { + "name": "city", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "name": { + "name": "name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "postal_code": { + "name": "postal_code", + "type": "varchar(10)", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "country_id": { + "name": "country_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "city_country_id_country_id_fk": { + "name": "city_country_id_country_id_fk", + "tableFrom": "city", + "tableTo": "country", + "columnsFrom": ["country_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.country": { + "name": "country", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "name": { + "name": "name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.language": { + "name": "language", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "name": { + "name": "name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.profile_skill": { + "name": "profile_skill", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "name": { + "name": "name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "profile_id": { + "name": "profile_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "profile_skill_profile_id_profile_id_fk": { + "name": "profile_skill_profile_id_profile_id_fk", + "tableFrom": "profile_skill", + "tableTo": "profile", + "columnsFrom": ["profile_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.profile": { + "name": "profile", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "first_name": { + "name": "first_name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "last_name": { + "name": "last_name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "oib": { + "name": "oib", + "type": "varchar(11)", + "primaryKey": false, + "notNull": true + }, + "sex": { + "name": "sex", + "type": "sexenum", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "birth_date": { + "name": "birth_date", + "type": "date", + "primaryKey": false, + "notNull": false + }, + "birth_place": { + "name": "birth_place", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "parent_name": { + "name": "parent_name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "nationality": { + "name": "nationality", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "profile_user_id_user_id_fk": { + "name": "profile_user_id_user_id_fk", + "tableFrom": "profile", + "tableTo": "user", + "columnsFrom": ["user_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "profile_oib_unique": { + "name": "profile_oib_unique", + "nullsNotDistinct": false, + "columns": ["oib"] + } + } + }, + "public.profile_address": { + "name": "profile_address", + "schema": "", + "columns": { + "profile_id": { + "name": "profile_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "address_id": { + "name": "address_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "is_primary": { + "name": "is_primary", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "profile_address_profile_id_profile_id_fk": { + "name": "profile_address_profile_id_profile_id_fk", + "tableFrom": "profile_address", + "tableTo": "profile", + "columnsFrom": ["profile_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "profile_address_address_id_address_id_fk": { + "name": "profile_address_address_id_address_id_fk", + "tableFrom": "profile_address", + "tableTo": "address", + "columnsFrom": ["address_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "profile_address_profile_id_address_id_pk": { + "name": "profile_address_profile_id_address_id_pk", + "columns": ["profile_id", "address_id"] + } + }, + "uniqueConstraints": {} + }, + "public.profile_language": { + "name": "profile_language", + "schema": "", + "columns": { + "profile_id": { + "name": "profile_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "language_id": { + "name": "language_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "level": { + "name": "level", + "type": "languagelevel", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "profile_language_profile_id_profile_id_fk": { + "name": "profile_language_profile_id_profile_id_fk", + "tableFrom": "profile_language", + "tableTo": "profile", + "columnsFrom": ["profile_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "profile_language_language_id_language_id_fk": { + "name": "profile_language_language_id_language_id_fk", + "tableFrom": "profile_language", + "tableTo": "language", + "columnsFrom": ["language_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "profile_language_profile_id_language_id_pk": { + "name": "profile_language_profile_id_language_id_pk", + "columns": ["profile_id", "language_id"] + } + }, + "uniqueConstraints": {} + }, + "public.profile_licence": { + "name": "profile_licence", + "schema": "", + "columns": { + "profile_id": { + "name": "profile_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "licence_id": { + "name": "licence_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "profile_licence_profile_id_profile_id_fk": { + "name": "profile_licence_profile_id_profile_id_fk", + "tableFrom": "profile_licence", + "tableTo": "profile", + "columnsFrom": ["profile_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "profile_licence_licence_id_license_id_fk": { + "name": "profile_licence_licence_id_license_id_fk", + "tableFrom": "profile_licence", + "tableTo": "license", + "columnsFrom": ["licence_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "profile_licence_profile_id_licence_id_pk": { + "name": "profile_licence_profile_id_licence_id_pk", + "columns": ["profile_id", "licence_id"] + } + }, + "uniqueConstraints": {} + }, + "public.profile_size": { + "name": "profile_size", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "shoe_size": { + "name": "shoe_size", + "type": "real", + "primaryKey": false, + "notNull": false + }, + "clothing_size": { + "name": "clothing_size", + "type": "clothingsize", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "height": { + "name": "height", + "type": "real", + "primaryKey": false, + "notNull": false + }, + "weight": { + "name": "weight", + "type": "real", + "primaryKey": false, + "notNull": false + }, + "profile_id": { + "name": "profile_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "profile_size_profile_id_profile_id_fk": { + "name": "profile_size_profile_id_profile_id_fk", + "tableFrom": "profile_size", + "tableTo": "profile", + "columnsFrom": ["profile_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.user": { + "name": "user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "email": { + "name": "email", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "password": { + "name": "password", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "active": { + "name": "active", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "idx_created_at_uuid": { + "name": "idx_created_at_uuid", + "columns": ["created_at", "id"], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.work_status": { + "name": "work_status", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "status": { + "name": "status", + "type": "workstatus", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "profession": { + "name": "profession", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "institution": { + "name": "institution", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "education_level": { + "name": "education_level", + "type": "educationlevel", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "profile_id": { + "name": "profile_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "work_status_profile_id_profile_id_fk": { + "name": "work_status_profile_id_profile_id_fk", + "tableFrom": "work_status", + "tableTo": "profile", + "columnsFrom": ["profile_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.license": { + "name": "license", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "type": { + "name": "type", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": { + "public.addresstype": { + "name": "addresstype", + "schema": "public", + "values": ["permanent_residence", "temporary_residence", "work", "other"] + }, + "public.clothingsize": { + "name": "clothingsize", + "schema": "public", + "values": ["XS", "S", "M", "L", "XL", "XXL"] + }, + "public.educationlevel": { + "name": "educationlevel", + "schema": "public", + "values": [ + "primary", + "secondary", + "college", + "bachelor", + "master", + "doctorate", + "post_doctorate" + ] + }, + "public.languagelevel": { + "name": "languagelevel", + "schema": "public", + "values": ["A1", "A2", "B1", "B2", "C1", "C2"] + }, + "public.sexenum": { + "name": "sexenum", + "schema": "public", + "values": ["M", "F", "O"] + }, + "public.workstatus": { + "name": "workstatus", + "schema": "public", + "values": [ + "EMPLOYED", + "UNEMPLOYED", + "SELF_EMPLOYED", + "STUDENT", + "RETIRED" + ] + } + }, + "schemas": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index 4d47314..ce59599 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -36,6 +36,13 @@ "when": 1726816797150, "tag": "0004_strong_hellion", "breakpoints": true + }, + { + "idx": 5, + "version": "6", + "when": 1726929449044, + "tag": "0005_bizarre_nightshade", + "breakpoints": true } ] } diff --git a/src/server/db/schema/user.ts b/src/server/db/schema/user.ts index 1684201..72bc6d8 100644 --- a/src/server/db/schema/user.ts +++ b/src/server/db/schema/user.ts @@ -181,7 +181,6 @@ export const addresses = pgTable("address", { street: varchar("street", { length: 255 }).notNull(), streetNumber: varchar("street_number", { length: 10 }).notNull(), type: addressEnum("type").notNull(), - isPrimary: boolean("is_primary").default(false), // For now upon creation we will manually set it to primary createdAt: timestamp("created_at").notNull().defaultNow(), updatedAt: timestamp("updated_at"), cityId: uuid("city_id").references(() => cities.id, { onDelete: "cascade" }), @@ -205,6 +204,7 @@ export const profilesAddresses = pgTable( addressId: uuid("address_id") .notNull() .references(() => addresses.id, { onDelete: "cascade" }), + isPrimary: boolean("is_primary").default(false), // For now upon creation we will manually set it to primary createdAt: timestamp("created_at").notNull().defaultNow(), }, (table) => ({ @@ -280,21 +280,12 @@ export const workStatusesRelations = relations(workStatuses, ({ one }) => ({ }), })); -export const languages = pgTable( - "language", - { - id: uuid("id").defaultRandom().primaryKey(), - name: varchar("name", { length: 255 }).notNull(), - level: languageLevelEnum("level").notNull(), - createdAt: timestamp("created_at").notNull().defaultNow(), - updatedAt: timestamp("updated_at"), - }, - (table) => { - return { - idx_name: uniqueIndex("idx_name").on(table.name, table.level), - }; - }, -); +export const languages = pgTable("language", { + id: uuid("id").defaultRandom().primaryKey(), + name: varchar("name", { length: 255 }).notNull(), + createdAt: timestamp("created_at").notNull().defaultNow(), + updatedAt: timestamp("updated_at"), +}); export const languagesRelations = relations(languages, ({ many }) => ({ profilesLanguages: many(profilesLanguages), @@ -309,6 +300,7 @@ export const profilesLanguages = pgTable( languageId: uuid("language_id") .notNull() .references(() => languages.id, { onDelete: "cascade" }), + level: languageLevelEnum("level").notNull(), createdAt: timestamp("created_at").notNull().defaultNow(), }, (table) => ({ diff --git a/src/server/db/seed.ts b/src/server/db/seed.ts index ca35206..082e815 100644 --- a/src/server/db/seed.ts +++ b/src/server/db/seed.ts @@ -24,7 +24,7 @@ import { users, workStatuses, } from "../db/schema"; -import { PgTransaction } from "drizzle-orm/pg-core"; +import { type PgTransaction } from "drizzle-orm/pg-core"; const SALT_OR_ROUNDS = 10; const names = ["John", "Jane", "Alice", "Bob", "Charlie", "Diana"]; @@ -130,7 +130,6 @@ const generateAddresses = async (cityIds: string[]): Promise => { street: _address.street, streetNumber: _address.streetNumber, cityId: cityIds[i % cityIds.length], - isPrimary: true, type: AddressType.PERMANENT_RESIDENCE, })), ) @@ -189,11 +188,9 @@ const generateLanguages = async (): Promise => { columns: { id: true }, }); if (existingLanguages.length) return existingLanguages.map(({ id }) => id); - const items: { name: string; level: LanguageLevel }[] = []; + const items: { name: string }[] = []; for (const lang of _languages) { - for (const level of Object.values(LanguageLevel)) { - items.push({ name: lang, level }); - } + items.push({ name: lang }); } const res = await db.insert(languages).values(items).returning(); @@ -223,16 +220,19 @@ const generateProfileSkills = ( const getRandomLanguages = ( userId: string, languageIds: string[], -): { profileId: string; languageId: string }[] => { +): { profileId: string; languageId: string; level: LanguageLevel }[] => { const shuffled = languageIds.sort(() => 0.5 - Math.random()); - const selected = shuffled.slice( - 0, - Math.floor(Math.random() * shuffled.length), - ); + const randomSelected = Math.floor(Math.random() * shuffled.length); + const selected = shuffled.slice(0, randomSelected === 0 ? 1 : randomSelected); + const randomLevel = + Object.values(LanguageLevel)[ + Math.floor(Math.random() * Object.values(LanguageLevel).length) + ]!; return selected.map((languageId) => ({ profileId: userId, languageId, + level: randomLevel, })); }; @@ -307,6 +307,7 @@ const generateUsers = async ( profileId: userProfile.insertedId, addressId: addressIds[Math.floor(Math.random() * addressIds.length)]!, + isPrimary: true, }); await tx @@ -395,6 +396,7 @@ const main = async () => { await tx.insert(profilesAddresses).values({ profileId: adminProfile.id, addressId: addressIds[Math.floor(Math.random() * addressIds.length)]!, + isPrimary: true, }); await insertWorkStatus(