bvplan/backend/migrations/2022-12-03-124501_init/up.sql

193 lines
5.4 KiB
MySQL
Raw Normal View History

2023-01-17 05:54:45 +00:00
CREATE TABLE schoolyears (
id SERIAL PRIMARY KEY,
untis_id INTEGER NOT NULL UNIQUE,
name VARCHAR NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
2023-02-22 17:02:10 +00:00
CREATE UNIQUE INDEX schoolyears_unique ON schoolyears(active)
WHERE
active;
2023-01-17 05:54:45 +00:00
CREATE TABLE tenants (
id SERIAL PRIMARY KEY,
untis_id INTEGER NOT NULL UNIQUE,
2023-02-22 17:02:10 +00:00
schoolyear_id INTEGER NOT NULL REFERENCES schoolyears(id),
2023-01-17 05:54:45 +00:00
name VARCHAR NOT NULL,
active BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
2023-02-22 17:02:10 +00:00
CREATE UNIQUE INDEX tenants_active ON tenants(active)
2022-12-12 16:54:07 +00:00
WHERE
2023-01-17 05:54:45 +00:00
active;
CREATE TABLE teachers (
id SERIAL PRIMARY KEY,
untis_id INTEGER NOT NULL UNIQUE,
2023-02-22 17:02:10 +00:00
schoolyear_id INTEGER NOT NULL REFERENCES schoolyears(id),
2023-01-17 05:54:45 +00:00
name VARCHAR NOT NULL,
forename VARCHAR,
display_name VARCHAR NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE classes (
id SERIAL PRIMARY KEY,
untis_id INTEGER NOT NULL UNIQUE,
2023-02-22 17:02:10 +00:00
schoolyear_id INTEGER NOT NULL REFERENCES schoolyears(id),
2023-01-17 05:54:45 +00:00
name VARCHAR NOT NULL,
long_name VARCHAR NOT NULL,
active BOOLEAN NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE subjects (
id SERIAL PRIMARY KEY,
untis_id INTEGER NOT NULL UNIQUE,
2023-02-22 17:02:10 +00:00
schoolyear_id INTEGER NOT NULL REFERENCES schoolyears(id),
2023-01-17 05:54:45 +00:00
name VARCHAR NOT NULL,
long_name VARCHAR NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE rooms (
id SERIAL PRIMARY KEY,
untis_id INTEGER NOT NULL UNIQUE,
2023-02-22 17:02:10 +00:00
schoolyear_id INTEGER NOT NULL REFERENCES schoolyears(id),
2023-01-17 05:54:45 +00:00
name VARCHAR NOT NULL,
long_name VARCHAR NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE departments (
id SERIAL PRIMARY KEY,
untis_id INTEGER NOT NULL UNIQUE,
2023-02-22 17:02:10 +00:00
schoolyear_id INTEGER NOT NULL REFERENCES schoolyears(id),
2023-01-17 05:54:45 +00:00
name VARCHAR NOT NULL,
long_name VARCHAR NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE holidays (
id SERIAL PRIMARY KEY,
untis_id INTEGER NOT NULL UNIQUE,
2023-02-22 17:02:10 +00:00
schoolyear_id INTEGER NOT NULL REFERENCES schoolyears(id),
2023-01-17 05:54:45 +00:00
name VARCHAR NOT NULL,
long_name VARCHAR NOT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE timegrids (
id SERIAL PRIMARY KEY,
2023-02-22 17:02:10 +00:00
schoolyear_id INTEGER NOT NULL REFERENCES schoolyears(id),
2023-01-17 05:54:45 +00:00
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE timegrid_days (
id SERIAL PRIMARY KEY,
2023-02-22 17:02:10 +00:00
timegrid_id INTEGER NOT NULL REFERENCES timegrids(id),
2023-01-17 05:54:45 +00:00
day_index SMALLINT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE timegrid_time_unit (
id SERIAL PRIMARY KEY,
2023-02-22 17:02:10 +00:00
timegrid_day_id INTEGER NOT NULL REFERENCES timegrid_days(id),
2023-01-17 05:54:45 +00:00
start_time TIME NOT NULL,
end_time TIME NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TYPE week_type AS ENUM ('a', 'b');
CREATE TABLE substitution_queries (
id SERIAL PRIMARY KEY,
2023-02-22 17:02:10 +00:00
schoolyear_id INTEGER NOT NULL REFERENCES schoolyears(id),
2023-01-17 05:54:45 +00:00
date DATE NOT NULL,
week_type week_type NOT NULL,
queried_at TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
2022-12-20 18:00:12 +00:00
);
2022-12-12 16:54:07 +00:00
CREATE TYPE substitution_type AS ENUM (
2023-01-17 05:54:45 +00:00
'cancel',
'subst',
'add',
'shift',
'rmchg',
'rmlk',
'bs',
'oh',
'sb',
'other',
'free',
'exam',
'ac',
'holi',
'stxt'
2022-12-12 16:54:07 +00:00
);
CREATE TABLE substitutions(
2023-01-17 05:54:45 +00:00
id SERIAL PRIMARY KEY,
2023-02-22 17:02:10 +00:00
substitution_query_id INTEGER NOT NULL REFERENCES substitution_queries(id),
2023-01-17 05:54:45 +00:00
subst_type substitution_type NOT NULL,
lesson_id INTEGER NOT NULL,
start_time TIME NOT NULL,
end_time TIME NOT NULL,
text VARCHAR,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE substitution_classes (
id SERIAL PRIMARY KEY,
2023-02-22 17:02:10 +00:00
substitution_id INTEGER NOT NULL REFERENCES substitutions(id),
2023-01-17 05:54:45 +00:00
position SMALLINT NOT NULL,
2023-02-22 17:02:10 +00:00
class_id INTEGER NOT NULL REFERENCES classes(id),
2023-01-17 05:54:45 +00:00
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE substitution_teachers (
id SERIAL PRIMARY KEY,
2023-02-22 17:02:10 +00:00
substitution_id INTEGER NOT NULL REFERENCES substitutions(id),
2023-01-17 05:54:45 +00:00
position SMALLINT NOT NULL,
2023-02-22 17:02:10 +00:00
teacher_id INTEGER REFERENCES teachers(id),
2023-01-17 05:54:45 +00:00
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE substitution_subjects (
id SERIAL PRIMARY KEY,
2023-02-22 17:02:10 +00:00
substitution_id INTEGER NOT NULL REFERENCES substitutions(id),
2023-01-17 05:54:45 +00:00
position SMALLINT NOT NULL,
2023-02-22 17:02:10 +00:00
subject_id INTEGER NOT NULL REFERENCES subjects(id),
2023-01-17 05:54:45 +00:00
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE substitution_rooms (
id SERIAL PRIMARY KEY,
2023-02-22 17:02:10 +00:00
substitution_id INTEGER NOT NULL REFERENCES substitutions(id),
2023-01-17 05:54:45 +00:00
position SMALLINT NOT NULL,
2023-02-22 17:02:10 +00:00
room_id INTEGER REFERENCES rooms(id),
original_room_id INTEGER REFERENCES rooms(id),
2023-01-17 05:54:45 +00:00
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);