diff --git a/docs/db/schema.dbml b/docs/db/schema.dbml index fe46939..0b9816b 100644 --- a/docs/db/schema.dbml +++ b/docs/db/schema.dbml @@ -118,7 +118,7 @@ Table tags { Table tags_assigned { tag_id integer [primary key] assigned_id integer [primary key] - assigned_to enum [note: 'mentor, project, study_buddy'] + assigned_to enum [note: 'mentor, project, study_buddy, resource'] } Table links { @@ -129,8 +129,31 @@ Table links { entity_id integer } +Table resources { + id integer [primary key] + title varchar(255) [not null] + type resource_type [not null] + audience audience [not null] + description text + attachment_filename varchar(255) + link varchar(255) + created_by integer [ref: > users.id] + created_at timestamp + updated_at timestamp +} +Enum resource_type { + post + video + picture + link + file +} - +Enum audience { + public + member + mentor +} //Ref: applications.last_modified_by > users.id // many-to-one Ref: users.id <> user_roles.user_id // many-to-many @@ -151,4 +174,5 @@ Ref: study_buddy.id <> users_learnings.learning_entity_id // links relationships: other resources to be added Ref: links.entity_id > projects.id -Ref: "study_buddy"."id" < "study_buddy"."created_by" \ No newline at end of file +Ref: "study_buddy"."id" < "study_buddy"."created_by" +Ref: resources.id <> tags_assigned.assigned_id \ No newline at end of file diff --git a/knex/migrations/20260307092657_create_resources_table.js b/knex/migrations/20260307092657_create_resources_table.js new file mode 100644 index 0000000..5e20272 --- /dev/null +++ b/knex/migrations/20260307092657_create_resources_table.js @@ -0,0 +1,84 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = async function(knex) { + await knex.raw(` + DO $$ + BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'resource_type') THEN + CREATE TYPE resource_type AS ENUM ('post', 'video', 'picture', 'link', 'file'); + END IF; + + IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'audience') THEN + CREATE TYPE audience AS ENUM ('public', 'member', 'mentor'); + END IF; + END $$; + `); + + // create resources + await knex.schema.createTable('resources', function(table) { + table.increments('id').primary(); + table.string('title', 255).notNullable(); + table.specificType('type', 'resource_type').notNullable(); + table.specificType('audience', 'audience').notNullable(); + table.text('description'); + table.string('attachment_filename', 255); + table.string('link', 255); + table.integer('created_by').references('id').inTable('users'); + table.timestamp('created_at').defaultTo(knex.fn.now()); + table.timestamp('updated_at').defaultTo(knex.fn.now()); + + //checks for certain fields + table.check('?? IS NOT NULL OR ?? IS NOT NULL OR ?? IS NOT NULL', + ['description', 'attachment_filename', 'link']); + }); + + //adding contraints to resources + await knex.raw(` + ALTER TABLE resources + ADD CONSTRAINT resources_type_description_check + CHECK ( + (type = 'post' AND description IS NOT NULL) OR + (type != 'post') + ); + + ALTER TABLE resources + ADD CONSTRAINT resources_type_video_check + CHECK ( + (type = 'video' AND link IS NOT NULL) OR + (type != 'video') + ); + + ALTER TABLE resources + ADD CONSTRAINT resources_type_picture_check + CHECK ( + (type = 'picture' AND (attachment_filename IS NOT NULL OR link IS NOT NULL)) OR + (type != 'picture') + ); + + ALTER TABLE resources + ADD CONSTRAINT resources_type_link_check + CHECK ( + (type = 'link' AND link IS NOT NULL) OR + (type != 'link') + ); + + ALTER TABLE resources + ADD CONSTRAINT resources_type_file_check + CHECK ( + (type = 'file' AND attachment_filename IS NOT NULL) OR + (type != 'file') + ); + `); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = async function(knex) { + await knex.schema.dropTableIfExists('resources'); + await knex.raw('DROP TYPE IF EXISTS resource_type;'); + await knex.raw('DROP TYPE IF EXISTS audience;'); +}; \ No newline at end of file diff --git a/knex/migrations/20260307092839_update_tags_assigned.js b/knex/migrations/20260307092839_update_tags_assigned.js new file mode 100644 index 0000000..83eba3e --- /dev/null +++ b/knex/migrations/20260307092839_update_tags_assigned.js @@ -0,0 +1,29 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = async function(knex) { + await knex.raw(` + ALTER TABLE tags_assigned + DROP CONSTRAINT IF EXISTS tags_assigned_assigned_to_check; + + ALTER TABLE tags_assigned + ADD CONSTRAINT tags_assigned_assigned_to_check + CHECK (assigned_to IN ('mentor', 'project', 'study_buddy', 'resource')); + `); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = async function(knex) { + await knex.raw(` + ALTER TABLE tags_assigned + DROP CONSTRAINT IF EXISTS tags_assigned_assigned_to_check; + + ALTER TABLE tags_assigned + ADD CONSTRAINT tags_assigned_assigned_to_check + CHECK (assigned_to IN ('mentor', 'project', 'study_buddy')); + `); +}; \ No newline at end of file