Add new inventory rows connection model, add connections counter caches to rows [SCI-9363]

This commit is contained in:
Oleksii Kriuchykhin 2023-09-25 11:24:50 +02:00
parent 30b848fef3
commit fe5848ad7c
4 changed files with 99 additions and 3 deletions

View file

@ -13,8 +13,8 @@ class RepositoryRow < ApplicationRecord
belongs_to :repository, class_name: 'RepositoryBase'
delegate :team, to: :repository
belongs_to :parent, class_name: 'RepositoryRow', optional: true
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User'
belongs_to :last_modified_by, foreign_key: :last_modified_by_id, class_name: 'User'
belongs_to :created_by, class_name: 'User'
belongs_to :last_modified_by, class_name: 'User'
belongs_to :archived_by,
class_name: 'User',
inverse_of: :archived_repository_rows,
@ -78,6 +78,24 @@ class RepositoryRow < ApplicationRecord
has_many :my_module_repository_rows,
inverse_of: :repository_row, dependent: :destroy
has_many :my_modules, through: :my_module_repository_rows
has_many :child_connections,
class_name: 'RepositoryRowConnection',
foreign_key: :parent_id,
dependent: :destroy,
inverse_of: :parent
has_many :child_repository_rows,
through: :child_connections,
class_name: 'RepositoryRow',
source: :child
has_many :parent_connections,
class_name: 'RepositoryRowConnection',
foreign_key: :child_id,
dependent: :destroy,
inverse_of: :child
has_many :parent_repository_rows,
through: :parent_connections,
class_name: 'RepositoryRow',
source: :parent
auto_strip_attributes :name, nullify: false
validates :name,

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
class RepositoryRowConnection < ApplicationRecord
belongs_to :parent,
class_name: 'RepositoryRow',
inverse_of: :child_connections,
counter_cache: :child_connections_count
belongs_to :child,
class_name: 'RepositoryRow',
inverse_of: :parent_connections,
counter_cache: :parent_connections_count
belongs_to :created_by, class_name: 'User'
belongs_to :last_modified_by, class_name: 'User'
validates :parent_id, uniqueness: { scope: :child_id }
end

View file

@ -0,0 +1,21 @@
# frozen_string_literal: true
class AddRepositoryRowConnections < ActiveRecord::Migration[7.0]
def change
create_table :repository_row_connections do |t|
t.references :parent, index: true, foreign_key: { to_table: :repository_rows }
t.references :child, index: true, foreign_key: { to_table: :repository_rows }
t.references :created_by, foreign_key: { to_table: :users }
t.references :last_modified_by, foreign_key: { to_table: :users }
t.timestamps
end
add_index :repository_row_connections, %i(parent_id child_id), unique: true
change_table :repository_rows, bulk: true do |t|
t.integer :parent_connections_count
t.integer :child_connections_count
end
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_07_24_094959) do
ActiveRecord::Schema[7.0].define(version: 2023_09_22_085412) do
# These are extensions that must be enabled in order to support this database
enable_extension "btree_gist"
enable_extension "pg_trgm"
@ -776,6 +776,20 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_24_094959) do
t.index ["last_modified_by_id"], name: "index_repository_number_values_on_last_modified_by_id"
end
create_table "repository_row_connections", force: :cascade do |t|
t.bigint "parent_id"
t.bigint "child_id"
t.bigint "created_by_id"
t.bigint "last_modified_by_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["child_id"], name: "index_repository_row_connections_on_child_id"
t.index ["created_by_id"], name: "index_repository_row_connections_on_created_by_id"
t.index ["last_modified_by_id"], name: "index_repository_row_connections_on_last_modified_by_id"
t.index ["parent_id", "child_id"], name: "index_repository_row_connections_on_parent_id_and_child_id", unique: true
t.index ["parent_id"], name: "index_repository_row_connections_on_parent_id"
end
create_table "repository_rows", force: :cascade do |t|
t.integer "repository_id"
t.bigint "created_by_id", null: false
@ -790,6 +804,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_24_094959) do
t.bigint "archived_by_id"
t.bigint "restored_by_id"
t.string "external_id"
t.integer "parent_connections_count"
t.integer "child_connections_count"
t.index "(('IT'::text || id)) gin_trgm_ops", name: "index_repository_rows_on_repository_row_code", using: :gin
t.index "((id)::text) gin_trgm_ops", name: "index_repository_rows_on_id_text", using: :gin
t.index "date_trunc('minute'::text, archived_on)", name: "index_repository_rows_on_archived_on_as_date_time_minutes"
@ -944,6 +960,23 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_24_094959) do
t.index ["type"], name: "index_settings_on_type", unique: true
end
create_table "shareable_links", force: :cascade do |t|
t.string "uuid"
t.string "description"
t.string "shareable_type"
t.bigint "shareable_id"
t.bigint "team_id"
t.bigint "created_by_id"
t.bigint "last_modified_by_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["created_by_id"], name: "index_shareable_links_on_created_by_id"
t.index ["last_modified_by_id"], name: "index_shareable_links_on_last_modified_by_id"
t.index ["shareable_type", "shareable_id"], name: "index_shareable_links_on_shareable"
t.index ["team_id"], name: "index_shareable_links_on_team_id"
t.index ["uuid"], name: "index_shareable_links_on_uuid"
end
create_table "step_assets", force: :cascade do |t|
t.bigint "step_id", null: false
t.bigint "asset_id", null: false
@ -1048,6 +1081,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_24_094959) do
t.bigint "last_modified_by_id"
t.string "description"
t.bigint "space_taken", default: 1048576, null: false
t.boolean "shareable_links_enabled", default: false, null: false
t.index ["created_by_id"], name: "index_teams_on_created_by_id"
t.index ["last_modified_by_id"], name: "index_teams_on_last_modified_by_id"
t.index ["name"], name: "index_teams_on_name"
@ -1378,6 +1412,10 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_24_094959) do
add_foreign_key "repository_list_values", "users", column: "last_modified_by_id"
add_foreign_key "repository_number_values", "users", column: "created_by_id"
add_foreign_key "repository_number_values", "users", column: "last_modified_by_id"
add_foreign_key "repository_row_connections", "repository_rows", column: "child_id"
add_foreign_key "repository_row_connections", "repository_rows", column: "parent_id"
add_foreign_key "repository_row_connections", "users", column: "created_by_id"
add_foreign_key "repository_row_connections", "users", column: "last_modified_by_id"
add_foreign_key "repository_rows", "users", column: "archived_by_id"
add_foreign_key "repository_rows", "users", column: "created_by_id"
add_foreign_key "repository_rows", "users", column: "last_modified_by_id"
@ -1407,6 +1445,9 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_24_094959) do
add_foreign_key "results", "users", column: "archived_by_id"
add_foreign_key "results", "users", column: "last_modified_by_id"
add_foreign_key "results", "users", column: "restored_by_id"
add_foreign_key "shareable_links", "teams"
add_foreign_key "shareable_links", "users", column: "created_by_id"
add_foreign_key "shareable_links", "users", column: "last_modified_by_id"
add_foreign_key "step_assets", "assets"
add_foreign_key "step_assets", "steps"
add_foreign_key "step_orderable_elements", "steps"