Skip to content

Instantly share code, notes, and snippets.

@sharkey11
sharkey11 / missing_fks.rb
Last active November 29, 2023 00:07
This script will look through your Schema and find any "foreign key" columns that are missing actual DB foreign keys. This could lead to orphaned rows and dirty data.
# Parse and identify potential foreign key issues in a Rails schema.rb file.
# TO USE THIS, SAVE THE FILE AND RUN IT USING RAILS RUNNER. EXAMPLE:
# `rails runner missing_fks.rb`
def extract_table_names(schema_lines)
schema_lines.select { |line| line.strip.start_with?('create_table') }
.map { |line| line.split('"')[1] }
end