From 7db40b211763c9e5461e5ab20581b6062d44115c Mon Sep 17 00:00:00 2001 From: rhymes Date: Tue, 26 May 2020 15:34:53 +0200 Subject: [PATCH] [deploy] Add unique indexes - part 3 (#8050) --- app/models/github_issue.rb | 1 + ..._index_to_github_repo_url_github_id_code.rb | 8 ++++++++ ...que_index_to_data_update_script_filename.rb | 7 +++++++ ...0642_add_unique_index_to_collection_slug.rb | 7 +++++++ ...611_add_unique_index_to_github_issue_url.rb | 7 +++++++ db/schema.rb | 7 ++++++- spec/requests/articles/articles_spec.rb | 18 ++++++++++++++---- 7 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20200525115740_add_unique_index_to_github_repo_url_github_id_code.rb create mode 100644 db/migrate/20200525120420_add_unique_index_to_data_update_script_filename.rb create mode 100644 db/migrate/20200525120642_add_unique_index_to_collection_slug.rb create mode 100644 db/migrate/20200525125611_add_unique_index_to_github_issue_url.rb diff --git a/app/models/github_issue.rb b/app/models/github_issue.rb index 55eb81cb0..da6198db7 100644 --- a/app/models/github_issue.rb +++ b/app/models/github_issue.rb @@ -11,6 +11,7 @@ class GithubIssue < ApplicationRecord validates :category, inclusion: { in: CATEGORIES } validates :url, presence: true, length: { maximum: 400 }, format: API_URL_REGEXP + validates :url, uniqueness: true class << self def find_or_fetch(url) diff --git a/db/migrate/20200525115740_add_unique_index_to_github_repo_url_github_id_code.rb b/db/migrate/20200525115740_add_unique_index_to_github_repo_url_github_id_code.rb new file mode 100644 index 000000000..f32c3f0cd --- /dev/null +++ b/db/migrate/20200525115740_add_unique_index_to_github_repo_url_github_id_code.rb @@ -0,0 +1,8 @@ +class AddUniqueIndexToGithubRepoUrlGithubIdCode < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + add_index :github_repos, :url, unique: true, algorithm: :concurrently + add_index :github_repos, :github_id_code, unique: true, algorithm: :concurrently + end +end diff --git a/db/migrate/20200525120420_add_unique_index_to_data_update_script_filename.rb b/db/migrate/20200525120420_add_unique_index_to_data_update_script_filename.rb new file mode 100644 index 000000000..b208ab685 --- /dev/null +++ b/db/migrate/20200525120420_add_unique_index_to_data_update_script_filename.rb @@ -0,0 +1,7 @@ +class AddUniqueIndexToDataUpdateScriptFilename < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + add_index :data_update_scripts, :file_name, unique: true, algorithm: :concurrently + end +end diff --git a/db/migrate/20200525120642_add_unique_index_to_collection_slug.rb b/db/migrate/20200525120642_add_unique_index_to_collection_slug.rb new file mode 100644 index 000000000..96851c8b6 --- /dev/null +++ b/db/migrate/20200525120642_add_unique_index_to_collection_slug.rb @@ -0,0 +1,7 @@ +class AddUniqueIndexToCollectionSlug < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + add_index :collections, %w[slug user_id], unique: true, algorithm: :concurrently + end +end diff --git a/db/migrate/20200525125611_add_unique_index_to_github_issue_url.rb b/db/migrate/20200525125611_add_unique_index_to_github_issue_url.rb new file mode 100644 index 000000000..679b4d189 --- /dev/null +++ b/db/migrate/20200525125611_add_unique_index_to_github_issue_url.rb @@ -0,0 +1,7 @@ +class AddUniqueIndexToGithubIssueUrl < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + add_index :github_issues, :url, unique: true, algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index 7c2baf05b..c8e8dc9fb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_05_21_153435) do +ActiveRecord::Schema.define(version: 2020_05_25_125611) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -361,6 +361,7 @@ ActiveRecord::Schema.define(version: 2020_05_21_153435) do t.datetime "updated_at", null: false t.integer "user_id" t.index ["organization_id"], name: "index_collections_on_organization_id" + t.index ["slug", "user_id"], name: "index_collections_on_slug_and_user_id", unique: true t.index ["user_id"], name: "index_collections_on_user_id" end @@ -414,6 +415,7 @@ ActiveRecord::Schema.define(version: 2020_05_21_153435) do t.datetime "run_at" t.integer "status", default: 0, null: false t.datetime "updated_at", null: false + t.index ["file_name"], name: "index_data_update_scripts_on_file_name", unique: true end create_table "display_ad_events", force: :cascade do |t| @@ -526,6 +528,7 @@ ActiveRecord::Schema.define(version: 2020_05_21_153435) do t.string "processed_html" t.datetime "updated_at", null: false t.string "url" + t.index ["url"], name: "index_github_issues_on_url", unique: true end create_table "github_repos", force: :cascade do |t| @@ -545,6 +548,8 @@ ActiveRecord::Schema.define(version: 2020_05_21_153435) do t.string "url" t.integer "user_id" t.integer "watchers_count" + t.index ["github_id_code"], name: "index_github_repos_on_github_id_code", unique: true + t.index ["url"], name: "index_github_repos_on_url", unique: true end create_table "html_variant_successes", force: :cascade do |t| diff --git a/spec/requests/articles/articles_spec.rb b/spec/requests/articles/articles_spec.rb index 5fcd7a260..e948584c5 100644 --- a/spec/requests/articles/articles_spec.rb +++ b/spec/requests/articles/articles_spec.rb @@ -58,9 +58,14 @@ RSpec.describe "Articles", type: :request do end shared_context "when user/organization articles exist" do - let(:organization) { create(:organization) } - let!(:user_article) { create(:article, user_id: user.id) } - let!(:organization_article) { create(:article, organization_id: organization.id) } + let_it_be_readonly(:user) { create(:user) } + let_it_be_readonly(:organization) { create(:organization) } + let_it_be_readonly(:user_article) do + create(:article, user: user, title: "user_article_title") + end + let_it_be_readonly(:organization_article) do + create(:article, organization: organization, title: "organization_article_title") + end end context "when :username param is given and belongs to a user" do @@ -85,7 +90,12 @@ RSpec.describe "Articles", type: :request do context "when :username param is given but it belongs to nither user nor organization" do include_context "when user/organization articles exist" - it("renders empty body") { expect { get "/feed", params: { username: "unknown" } }.to raise_error(ActiveRecord::RecordNotFound) } + + it "renders empty body" do + expect do + get feed_path("unknown") + end.to raise_error(ActiveRecord::RecordNotFound) + end end context "when format is invalid" do