Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions docs/db/schema.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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"
Ref: "study_buddy"."id" < "study_buddy"."created_by"
Ref: resources.id <> tags_assigned.assigned_id
84 changes: 84 additions & 0 deletions knex/migrations/20260307092657_create_resources_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
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<void> }
*/
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;');
};
29 changes: 29 additions & 0 deletions knex/migrations/20260307092839_update_tags_assigned.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function(knex) {
await knex.raw(`
ALTER TABLE tags_assigned
DROP CONSTRAINT IF EXISTS tags_assigned_assigned_to_check;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this constraint? when creating the table in 20251019074637_add_tags_tables.js we used inline enum definition. is this constraint automatically created in this case? 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah because as far as i understand when it comes to inline enum you have to actually to drop the constraint every time to recreate it in order to update it


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<void> }
*/
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'));
`);
};
Loading