-
Notifications
You must be signed in to change notification settings - Fork 0
Add resources table #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add resources table #184
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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;'); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| 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')); | ||
| `); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.jswe used inline enum definition. is this constraint automatically created in this case? 🤔There was a problem hiding this comment.
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