Skip to content

Commit

Permalink
replace documentation functionality with schema one
Browse files Browse the repository at this point in the history
  • Loading branch information
akabishau committed Nov 1, 2024
1 parent 3e71535 commit 385654b
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 61 deletions.
10 changes: 0 additions & 10 deletions app/models/ctgov_api/documentation.rb

This file was deleted.

6 changes: 1 addition & 5 deletions app/models/ctgov_api/mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ module CtgovApi
class Mapping < ApplicationRecord
self.table_name = "support.ctgov_mappings"

belongs_to :ctgov_metadata,
class_name: "CtgovApi::Metadata",
foreign_key: "ctgov_metadata_id",
optional: true

validates :table_name, :field_name, :api_path, presence: true
end
end
# TODO: move to support namespace for consistency
5 changes: 1 addition & 4 deletions app/models/ctgov_api/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ class Metadata < ApplicationRecord

# TODO: Add active flag - to handle changes coming from the API

has_many :mappings,
class_name: "CtgovApi::Mapping",
foreign_key: "ctgov_api_metadata_id"

validates :name, :data_type, :path, :version, presence: true
end
end
# TODO: move to support namespace for consistency
2 changes: 1 addition & 1 deletion app/models/study_relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def self.blacklist
ctgov_metadata
ctgov_mappings
schema_snapshots
ctgov_documentation
ctgov_schema
)
end

Expand Down
7 changes: 7 additions & 0 deletions app/models/support/ctgov_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Support
class CtgovSchema < ApplicationRecord
self.table_name = "support.ctgov_schema"

validates :table_name, :column_name, presence: true
end
end
2 changes: 1 addition & 1 deletion app/services/data_mapping_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def process_json(mappings, parent_root = nil)
table_name: mapping["table"],
field_name: column["name"],
api_path: api_path,
ctgov_metadata_id: fetch_metadata_id(api_path),
# ctgov_metadata_id: fetch_metadata_id(api_path),
active: true,
created_at: Time.now,
updated_at: Time.now
Expand Down
3 changes: 2 additions & 1 deletion app/services/schema_snapshot_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ def schema_changed?

private

# TODO: filter out Views (table_type = "BASE TABLE")
def current_schema_data
@current_schema_data ||= ActiveRecord::Base.connection.select_all(<<-SQL).to_a
SELECT table_name, column_name
SELECT table_name, column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_schema = '#{@schema_name}'
ORDER BY table_name, ordinal_position;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class DocumentationSyncService
class SchemaSyncService
def initialize(schema_name)
@schema_name = schema_name
end
Expand All @@ -9,34 +9,22 @@ def sync_schema_with_documentation
schema_snapshot.each do |item|
table_name = item['table_name']
column_name = item['column_name']

doc_record = find_or_initialize_documentation(table_name, column_name)
# byebug
mapping = CtgovApi::Mapping.find_by(table_name: table_name, field_name: column_name)
doc_record.ctgov_mapping_id = mapping&.id

if mapping
doc_record.ctgov_mapping_id = mapping.id

metadata = CtgovApi::Metadata.find_by(path: mapping.api_path)
doc_record.ctgov_metadata_id = metadata&.id
end

doc_record.active = true
doc_record.data_type = item['data_type'] # TODO: normalize data type
doc_record.save!
end

# TODO: add inactive records that not present in the shema snapshot
end

# private
private

def fetch_schema_snapshot
# TODO: optimize using scope
# TODO: optimize using scope - fetch latest snapshot
Support::SchemaSnapshot.where(schema_name: @schema_name).order(created_at: :desc).first.snapshot
end

def find_or_initialize_documentation(table_name, column_name)
CtgovApi::Documentation.find_or_initialize_by(table_name: table_name, column_name: column_name)
Support::CtgovSchema.find_or_initialize_by(table_name: table_name, column_name: column_name)
end
end
5 changes: 1 addition & 4 deletions db/migrate/20241013041207_create_ctgov_mappings_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ class CreateCtgovMappingsTable < ActiveRecord::Migration[6.0]

def change
create_table "support.ctgov_mappings" do |t|
t.boolean :active, default: true
t.string :table_name
t.string :field_name
t.boolean :active, default: true
t.string :api_path
t.references :ctgov_metadata,
foreign_key: { to_table: "support.ctgov_metadata" },
null: true
t.timestamps
end

Expand Down
18 changes: 0 additions & 18 deletions db/migrate/20241023025650_create_ctgov_documentation_table.rb

This file was deleted.

16 changes: 16 additions & 0 deletions db/migrate/20241023025650_create_ctgov_schema_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateCtgovSchemaTable < ActiveRecord::Migration[6.0]
def change
create_table "support.ctgov_schema" do |t|
t.boolean :active, default: true, null: false
t.string :table_name, null: false
t.string :column_name, null: false
t.string :data_type, null: false
# t.boolean :nullable, default: true, null: false
t.string :description, null: true

t.timestamps
end

add_index :ctgov_schema, [:table_name, :column_name], unique: true, name: 'index_ctgov_documentation_on_table_and_column'
end
end

0 comments on commit 385654b

Please sign in to comment.