Skip to content
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

Updated COLUMN_PATTERN to handle optional metadata (e.g., constraints or descriptions) enclosed in parentheses. #170

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/annotate_rb/model_annotator/annotation_diff_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ module ModelAnnotator
# Compares the current file content and new annotation block and generates the column annotation differences
class AnnotationDiffGenerator
HEADER_PATTERN = /(^# Table name:.*?\n(#.*\r?\n)*\r?)/
COLUMN_PATTERN = /^#[\t ]+[\w*.`\[\]():]+[\t ]+.+$/
# Example matches:
# - "# id :uuid not null, primary key"
# - "# title(length 255) :string not null"
# - "# status(a/b/c) :string not null"
# - "# created_at :datetime not null"
# - "# updated_at :datetime not null"
COLUMN_PATTERN = /^#[\t ]+[\w*.`\[\]():]+(?:\(.*?\))?[\t ]+.+$/

class << self
def call(file_content, annotation_block)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,55 @@ class User < ApplicationRecord
expect(subject.changed?).to eq(true)
end
end

context "when a new column with metadata in parentheses is added" do
let(:annotation_block) do
<<~SCHEMA
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# name([sensitivity: medium]) :string(50) not null
# status(active/pending/inactive) :string not null
#
SCHEMA
end
let(:file_content) do
<<~FILE
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# name([sensitivity: medium]) :string(50) not null
#
class User < ApplicationRecord
end
FILE
end

let(:current_columns) do
[
"# id :bigint not null, primary key",
"# name([sensitivity: medium]) :string(50) not null",
"# Table name: users"
]
end
let(:new_columns) do
[
"# id :bigint not null, primary key",
"# name([sensitivity: medium]) :string(50) not null",
"# status(active/pending/inactive) :string not null",
"# Table name: users"
]
end

it "returns an AnnotationDiff object with the expected old and new columns" do
test_columns_match_expected

expect(subject.changed?).to eq(true)
end
end
end
end