Skip to content

Commit

Permalink
Update migration
Browse files Browse the repository at this point in the history
  • Loading branch information
nygrenh committed Dec 19, 2023
1 parent 16ce9ac commit eda49e9
Showing 1 changed file with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,62 @@
CREATE TABLE combined_certificates (
ALTER TABLE course_module_certificate_configurations
RENAME TO certificate_configurations;
ALTER TABLE course_module_certificates
RENAME TO generated_certificates;
-- A certificate can be for many courses and for many modules. Conversly, course may have many certificates
-- We will handle this many-to-many relationship with a join table
CREATE TABLE certificate_configuration_to_requirements (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
deleted_at TIMESTAMP WITH TIME ZONE
deleted_at TIMESTAMP WITH TIME ZONE,
certificate_configuration_id UUID NOT NULL REFERENCES certificate_configurations(id),
course_instance_id UUID REFERENCES course_instances(id),
course_module_id UUID REFERENCES course_modules(id)
);
CREATE TRIGGER set_timestamp BEFORE
UPDATE ON combined_certificates FOR EACH ROW EXECUTE PROCEDURE trigger_set_timestamp();
COMMENT ON TABLE combined_certificates IS 'A combined certificate can be created when a student has completed multiple course modules. The modules can be from different courses, in which case the combined certificate functions as a certificate from multiple courses. See combined_certificate_to_course_modules for which course modules are required to be eligible for a combined certificate. See certificate_configurations on how the certificate is supposed to be rendered.';
COMMENT ON COLUMN combined_certificates.id IS 'A unique, stable identifier for the record.';
COMMENT ON COLUMN combined_certificates.created_at IS 'Timestamp when the record was created.';
COMMENT ON COLUMN combined_certificates.updated_at IS 'Timestamp when the record was last updated. The field is updated automatically by the set_timestamp trigger.';
COMMENT ON COLUMN combined_certificates.deleted_at IS 'Timestamp when the record was deleted. If null, the record is not deleted.';
UPDATE ON certificate_configuration_to_requirements FOR EACH ROW EXECUTE PROCEDURE trigger_set_timestamp();
COMMENT ON TABLE certificate_configuration_to_requirements IS 'A join table that can be used to figure out what course modules and course instances are required to be completed to be eligible for a specific certificate. Each row defines one requirement. To find out all requirements one will need to query for all rows that match a certificate configuration id. Note that this is the correct table if you want a certificate to relate to a course or to courses because all courses have course modules and course instances.';
COMMENT ON COLUMN certificate_configuration_to_requirements.id IS 'A unique, stable identifier for the record.';
COMMENT ON COLUMN certificate_configuration_to_requirements.created_at IS 'Timestamp when the record was created.';
COMMENT ON COLUMN certificate_configuration_to_requirements.updated_at IS 'Timestamp when the record was last updated. The field is updated automatically by the set_timestamp trigger.';
COMMENT ON COLUMN certificate_configuration_to_requirements.deleted_at IS 'Timestamp when the record was deleted. If null, the record is not deleted.';
COMMENT ON COLUMN certificate_configuration_to_requirements.certificate_configuration_id IS 'Identifies the certificate this requirement is for.';
COMMENT ON COLUMN certificate_configuration_to_requirements.course_instance_id IS 'If defined, the referred course instance is a requirement for this certificate.';
COMMENT ON COLUMN certificate_configuration_to_requirements.course_module_id IS 'If defined, the referred course module is a requirement for this certificate.';
-- TODO: is this a good idea?
ALTER TABLE certificate_configuration_to_requirements
ADD CONSTRAINT one_requirement_per_row CHECK num_nonnulls(
course_instance_id,
course_module_id,
) = 1;
COMMENT ON CONSTRAINT one_requirement_per_row ON certificate_configuration_to_requirements IS 'Each row can define only one requirement. If you want multiple requirements for a certificate, add more rows.';
-- Generated certificates cannot no longer refer directly refer to the requirements as there may be many of them
-- We'll migrate the table to refer to the configuration instead
ALTER TABLE generated_certificates
ADD COLUMN certificate_configuration_id UUID REFERENCES certificate_configurations(id);
UPDATE generated_certificates gs
SET certificate_configuration_id = (
SELECT id
FROM certificate_configurations cc
WHERE cc.course_instance_id = gs.course_instance_id
AND cc.course_module_id = gs.course_module_id
);
ALTER TABLE generated_certificates
ALTER COLUMN certificate_configuration_id
SET NOT NULL;
ALTER TABLE generated_certificates DROP COLUMN course_instance_id;
ALTER TABLE generated_certificates DROP COLUMN course_module_id;
-- Move the requirements from the certificate configurations table to the new join table
INSERT INTO certificate_configuration_to_requirements (
certificate_configuration_id,
course_instance_id,
course_module_id
)
VALUES
SELECT id,
course_instance_id,
course_module_id
FROM certificate_configurations
WHERE deleted_at IS NULL;
ALTER TABLE certificate_configurations DROP COLUMN course_instance_id;
ALTER TABLE certificate_configurations DROP COLUMN course_module_id;

0 comments on commit eda49e9

Please sign in to comment.