Compare commits

...

2 Commits

Author SHA1 Message Date
35354b1cd5 4 fields snake case 2026-04-05 15:41:06 +03:00
11ba82ebf0 4 studio usage 2026-04-01 10:58:05 +03:00
2 changed files with 99 additions and 93 deletions

View File

@ -148,7 +148,7 @@ function GenPrice(max = 1000) {
const ClassTypes = ['Theory', 'Practice']
const RegistrationStatuses = ['NotPaid', 'Paid']
const EquipmentUsageTypes = ['PayOnce', 'PayMonthly']
const UsageTypes = ['PayOnce', 'PayMonthly']
const COUNT = 10
@ -186,37 +186,37 @@ let script = 'INSERT INTO "Person" (full_name, passwd_hash) ' + Values(() => {
let person_id = 1
// Master
script += 'INSERT INTO "Master" ("Person_id", passport, readme) ' + Values(() => {
script += 'INSERT INTO "Master" (person_id, passport, readme) ' + Values(() => {
return `(${person_id++}, ${GenPassport()}, 'aboba')`
})
// Client
script += 'INSERT INTO "Client" ("Person_id", phone, email, billing_account) ' + Values(() => {
script += 'INSERT INTO "Client" (person_id, phone, email, billing_account) ' + Values(() => {
return `(${person_id++}, ${GenPhone()}, ${GenEmail()}, ${GenBillingAccount()})`
})
// Studio
script += 'INSERT INTO "Studio" (address, capacity, begin_date, duration) ' + Values(() => {
return `(${GenAddress()}, ${GenInt(10, 80)}, ${GenTimestamp()}, ${GenIntervalHours(12)})`
script += 'INSERT INTO "Studio" (address, capacity, begin_date, duration, usage_price, usage_type) ' + Values(() => {
return `(${GenAddress()}, ${GenInt(10, 80)}, ${GenTimestamp()}, ${GenIntervalHours(12)}, ${GenPrice(300)}, '${UsageTypes[GenMaxUInt(UsageTypes.length)]}')`
})
// Course
script += 'INSERT INTO "Course" ("Master_id", name, price, deadline_date) ' + Values(() => {
script += 'INSERT INTO "Course" (master_id, name, price, deadline_date) ' + Values(() => {
return `(${GenInt(1, COUNT + 1)}, 'курс ${GenMaxUInt(3)}', ${GenPrice()}, ${GenTimestamp()})`
})
// Class
script += 'INSERT INTO "Class" (begin_date, "Course_id", "Studio_id", name, type, duration) ' + Values(() => {
script += 'INSERT INTO "Class" (begin_date, course_id, studio_id, name, type, duration) ' + Values(() => {
return `(${GenTimestamp()}, ${GenInt(1, COUNT + 1)}, ${GenInt(1, COUNT + 1)}, 'занятие ${GenMaxUInt(100)}', '${ClassTypes[GenMaxUInt(ClassTypes.length)]}', ${GenIntervalHours()})`
})
// Class_Master
script += Many(`INSERT INTO "Class_Master" ("Class_begin_date", "Class_Course_id", "Master_id")`, () => {
return `SELECT begin_date, "Course_id", ${GenInt(1, COUNT + 1)} FROM "Class" LIMIT 1 OFFSET ${GenInt(0, COUNT)}`
script += Many(`INSERT INTO "Class_Master" (class_begin_date, class_course_id, master_id)`, () => {
return `SELECT begin_date, course_id, ${GenInt(1, COUNT + 1)} FROM "Class" LIMIT 1 OFFSET ${GenInt(0, COUNT)}`
})
// Registration
script += 'INSERT INTO "Registration" ("Client_id", "Course_id", date, status) ' + Values(() => {
script += 'INSERT INTO "Registration" (client_id, course_id, date, status) ' + Values(() => {
return `(${GenInt(COUNT + 1, COUNT * 2 + 1)}, ${GenInt(1, COUNT + 1)}, ${GenTimestamp()}, 'Paid')`
}, 0.5)
@ -226,18 +226,18 @@ script += 'INSERT INTO "FoodProductEnum" (name, avg_price) ' + Values(() => {
})
// FoodProduct
script += Many(`INSERT INTO "FoodProduct" ("FoodProductEnum_id", "Class_begin_date", "Class_Course_id", buy_price, buy_date, delivery_price, delivery_date, expires_date)`, () => {
return `SELECT ${GenInt(1, COUNT + 1)}, begin_date, "Course_id", ${GenPrice()}, ${GenTimestamp()}, ${GenPrice()}, ${GenTimestamp()}, ${GenTimestamp()} FROM "Class" LIMIT 1 OFFSET ${GenInt(0, COUNT)}`
script += Many(`INSERT INTO "FoodProduct" (food_product_enum_id, class_begin_date, class_course_id, buy_price, buy_date, delivery_price, delivery_date, expires_date)`, () => {
return `SELECT ${GenInt(1, COUNT + 1)}, begin_date, course_id, ${GenPrice()}, ${GenTimestamp()}, ${GenPrice()}, ${GenTimestamp()}, ${GenTimestamp()} FROM "Class" LIMIT 1 OFFSET ${GenInt(0, COUNT)}`
})
// Equipment
script += 'INSERT INTO "Equipment" ("Studio_id", name, usage_price, usage_type, delivery_price, delivery_date) ' + Values(() => {
return `(${GenInt(1, COUNT + 1)}, 'оборудование ${GenMaxUInt(100)}', ${GenPrice(300)}, '${EquipmentUsageTypes[GenMaxUInt(EquipmentUsageTypes.length)]}', ${GenPrice(200)}, ${GenTimestamp()})`
script += 'INSERT INTO "Equipment" (studio_id, name, usage_price, usage_type, delivery_price, delivery_date) ' + Values(() => {
return `(${GenInt(1, COUNT + 1)}, 'оборудование ${GenMaxUInt(100)}', ${GenPrice(300)}, '${UsageTypes[GenMaxUInt(UsageTypes.length)]}', ${GenPrice(200)}, ${GenTimestamp()})`
})
// Class_Equipment
script += Many(`INSERT INTO "Class_Equipment" ("Class_begin_date", "Class_Course_id", "Equipment_id")`, () => {
return `SELECT begin_date, "Course_id", ${GenInt(1, COUNT + 1)} FROM "Class" LIMIT 1 OFFSET ${GenInt(0, COUNT)}`
script += Many(`INSERT INTO "Class_Equipment" (class_begin_date, class_course_id, equipment_id)`, () => {
return `SELECT begin_date, course_id, ${GenInt(1, COUNT + 1)} FROM "Class" LIMIT 1 OFFSET ${GenInt(0, COUNT)}`
})
// DrugEnum
@ -246,17 +246,17 @@ script += 'INSERT INTO "DrugEnum" (name, avg_price) ' + Values(() => {
})
// Drug
script += 'INSERT INTO "Drug" ("DrugEnum_id", "Course_id", buy_price, buy_date, delivery_price, delivery_date, expires_date) ' + Values(() => {
script += 'INSERT INTO "Drug" (drug_enum_id, course_id, buy_price, buy_date, delivery_price, delivery_date, expires_date) ' + Values(() => {
return `(${GenInt(1, COUNT + 1)}, ${GenInt(1, COUNT + 1)}, ${GenPrice()}, ${GenTimestamp()}, ${GenPrice()}, ${GenTimestamp()}, ${GenTimestamp()})`
})
// DrugIntolerance
script += 'INSERT INTO "DrugIntolerance" ("Person_id", "DrugEnum_id") ' + Values(() => {
script += 'INSERT INTO "DrugIntolerance" (person_id, drug_enum_id) ' + Values(() => {
return `(${GenInt(1, COUNT * 2 + 1)}, ${GenInt(1, COUNT + 1)})`
})
// ActivityLog
script += 'INSERT INTO "ActivityLog" ("Person_id", action) ' + Values(() => {
script += 'INSERT INTO "ActivityLog" (person_id, action) ' + Values(() => {
return `(${GenInt(1, COUNT * 2 + 1)}, 'login')`
})

View File

@ -1,57 +1,68 @@
DROP TABLE IF EXISTS "Person", "Client", "Master", "Studio", "Course", "Class", "Class_Master", "Registration", "FoodProductEnum", "FoodProduct", "Equipment", "Class_Equipment", "DrugEnum", "Drug", "DrugIntolerance", "ActivityLog";
DROP TYPE IF EXISTS "ClassType", "RegistrationStatus", "EquipmentUsageType";
DROP TYPE IF EXISTS "ClassType", "RegistrationStatus", "UsageType";
CREATE TYPE "UsageType" AS ENUM (
'PayOnce',
'PayMonthly'
);
CREATE TABLE "Person" (
id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
person_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
full_name text NOT NULL,
passwd_hash bytea NOT NULL
);
CREATE TABLE "Client" (
"Person_id" int UNIQUE NOT NULL,
person_id int PRIMARY KEY,
phone text NOT NULL,
email text NOT NULL,
billing_account text NOT NULL,
FOREIGN KEY ("Person_id") REFERENCES "Person" (id)
FOREIGN KEY (person_id) REFERENCES "Person" (person_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE "Master" (
"Person_id" int UNIQUE NOT NULL,
person_id int PRIMARY KEY,
passport text UNIQUE NOT NULL,
readme text NOT NULL,
FOREIGN KEY ("Person_id") REFERENCES "Person" (id)
FOREIGN KEY (person_id) REFERENCES "Person" (person_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE "Studio" (
id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
studio_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
address text UNIQUE NOT NULL,
capacity int NOT NULL CHECK (capacity > 0),
begin_date timestamptz NOT NULL,
duration interval NOT NULL
duration interval NOT NULL,
usage_price real NOT NULL,
usage_type "UsageType" NOT NULL
);
CREATE TABLE "Course" (
id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"Master_id" int,
course_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
master_id int,
name text NOT NULL,
price real NOT NULL CHECK (price > 0),
deadline_date timestamptz NOT NULL,
FOREIGN KEY ("Master_id") REFERENCES "Master" ("Person_id")
FOREIGN KEY (master_id) REFERENCES "Master" (person_id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
@ -65,36 +76,35 @@ CREATE TYPE "ClassType" AS ENUM (
CREATE TABLE "Class" (
begin_date timestamptz,
"Course_id" int,
"Studio_id" int,
course_id int,
PRIMARY KEY (begin_date, course_id),
studio_id int,
name text NOT NULL,
type "ClassType" NOT NULL,
duration interval NOT NULL,
PRIMARY KEY (begin_date, "Course_id"),
FOREIGN KEY ("Course_id") REFERENCES "Course" (id)
FOREIGN KEY (course_id) REFERENCES "Course" (course_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY ("Studio_id") REFERENCES "Studio" (id)
FOREIGN KEY (studio_id) REFERENCES "Studio" (studio_id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE "Class_Master" (
"Class_begin_date" timestamptz,
"Class_Course_id" int,
"Master_id" int,
class_begin_date timestamptz,
class_course_id int,
master_id int,
PRIMARY KEY (class_begin_date, class_course_id, master_id),
PRIMARY KEY ("Class_begin_date", "Class_Course_id", "Master_id"),
FOREIGN KEY ("Master_id") REFERENCES "Master" ("Person_id")
FOREIGN KEY (master_id) REFERENCES "Master" (person_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY ("Class_begin_date", "Class_Course_id") REFERENCES "Class" (begin_date, "Course_id")
FOREIGN KEY (class_begin_date, class_course_id) REFERENCES "Class" (begin_date, course_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
@ -107,144 +117,140 @@ CREATE TYPE "RegistrationStatus" AS ENUM (
CREATE TABLE "Registration" (
"Client_id" int,
"Course_id" int,
client_id int,
course_id int,
PRIMARY KEY (client_id, course_id),
date timestamptz NOT NULL,
status "RegistrationStatus" NOT NULL,
PRIMARY KEY ("Client_id", "Course_id"),
FOREIGN KEY ("Client_id") REFERENCES "Client" ("Person_id")
FOREIGN KEY (client_id) REFERENCES "Client" (person_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY ("Course_id") REFERENCES "Course" (id)
FOREIGN KEY (course_id) REFERENCES "Course" (course_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE "FoodProductEnum" (
id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
food_product_enum_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
avg_price real NOT NULL
);
CREATE TABLE "FoodProduct" (
id int GENERATED ALWAYS AS IDENTITY,
"FoodProductEnum_id" int,
"Class_begin_date" timestamptz,
"Class_Course_id" int,
food_product_id int GENERATED ALWAYS AS IDENTITY,
food_product_enum_id int,
PRIMARY KEY (food_product_id, food_product_enum_id),
class_begin_date timestamptz,
class_course_id int,
buy_price real NOT NULL,
buy_date timestamptz NOT NULL,
delivery_price real NOT NULL,
delivery_date timestamptz NOT NULL,
expires_date timestamptz NOT NULL,
PRIMARY KEY (id, "FoodProductEnum_id"),
FOREIGN KEY ("FoodProductEnum_id") REFERENCES "FoodProductEnum" (id)
FOREIGN KEY (food_product_enum_id) REFERENCES "FoodProductEnum" (food_product_enum_id)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
FOREIGN KEY ("Class_begin_date", "Class_Course_id") REFERENCES "Class" (begin_date, "Course_id")
FOREIGN KEY (class_begin_date, class_course_id) REFERENCES "Class" (begin_date, course_id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TYPE "EquipmentUsageType" AS ENUM (
'PayOnce',
'PayMonthly'
);
CREATE TABLE "Equipment" (
id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"Studio_id" int,
equipment_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
studio_id int,
name text NOT NULL,
usage_price real NOT NULL,
usage_type "EquipmentUsageType" NOT NULL,
usage_type "UsageType" NOT NULL,
delivery_price real NOT NULL,
delivery_date timestamptz NOT NULL,
FOREIGN KEY ("Studio_id") REFERENCES "Studio" (id)
FOREIGN KEY (studio_id) REFERENCES "Studio" (studio_id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE "Class_Equipment" (
"Class_begin_date" timestamptz,
"Class_Course_id" int,
"Equipment_id" int,
class_begin_date timestamptz,
class_course_id int,
equipment_id int,
PRIMARY KEY (class_begin_date, class_course_id, equipment_id),
PRIMARY KEY ("Class_begin_date", "Class_Course_id", "Equipment_id"),
FOREIGN KEY ("Class_begin_date", "Class_Course_id") REFERENCES "Class" (begin_date, "Course_id")
FOREIGN KEY (class_begin_date, class_course_id) REFERENCES "Class" (begin_date, course_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY ("Equipment_id") REFERENCES "Equipment" (id)
FOREIGN KEY (equipment_id) REFERENCES "Equipment" (equipment_id)
ON DELETE RESTRICT
ON UPDATE CASCADE
);
CREATE TABLE "DrugEnum" (
id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
drug_enum_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
avg_price real NOT NULL
);
CREATE TABLE "Drug" (
id int GENERATED ALWAYS AS IDENTITY,
"DrugEnum_id" int,
"Course_id" int,
drug_id int GENERATED ALWAYS AS IDENTITY,
drug_enum_id int,
PRIMARY KEY (drug_id, drug_enum_id),
course_id int,
buy_price real NOT NULL,
buy_date timestamptz NOT NULL,
delivery_price real NOT NULL,
delivery_date timestamptz NOT NULL,
expires_date timestamptz NOT NULL,
PRIMARY KEY (id, "DrugEnum_id"),
FOREIGN KEY ("DrugEnum_id") REFERENCES "DrugEnum" (id)
FOREIGN KEY (drug_enum_id) REFERENCES "DrugEnum" (drug_enum_id)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
FOREIGN KEY ("Course_id") REFERENCES "Course" (id)
FOREIGN KEY (course_id) REFERENCES "Course" (course_id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE "DrugIntolerance" (
"Person_id" int,
"DrugEnum_id" int,
person_id int,
drug_enum_id int,
PRIMARY KEY ("Person_id", "DrugEnum_id"),
PRIMARY KEY (person_id, drug_enum_id),
FOREIGN KEY ("Person_id") REFERENCES "Person" (id)
FOREIGN KEY (person_id) REFERENCES "Person" (person_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY ("DrugEnum_id") REFERENCES "DrugEnum" (id)
FOREIGN KEY (drug_enum_id) REFERENCES "DrugEnum" (drug_enum_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE "ActivityLog" (
id int GENERATED ALWAYS AS IDENTITY,
"Person_id" int,
activity_log_id int GENERATED ALWAYS AS IDENTITY,
person_id int,
PRIMARY KEY (activity_log_id, person_id),
action text NOT NULL,
PRIMARY KEY (id, "Person_id"),
FOREIGN KEY ("Person_id") REFERENCES "Person" (id)
FOREIGN KEY (person_id) REFERENCES "Person" (person_id)
ON DELETE NO ACTION
ON UPDATE CASCADE
);