Skip to content

Commit 7a15b5c

Browse files
author
Matteo La Cognata
committed
Squashed commit of the following:
commit 22ab676 Author: Cuong Tran <[email protected]> Date: Thu Mar 30 15:17:37 2023 -0700 chore: remove broken badges from README.md commit 10a7a76 Author: Takumi KAGIYAMA <[email protected]> Date: Fri Mar 31 07:15:07 2023 +0900 Support `--frozen` option for routing annotations (ctran#979) The `--frozen` option previously deal only model annotations. This change will support route annotations as well. --------- Signed-off-by: kg8m <[email protected]> Co-authored-by: Cuong Tran <[email protected]> commit a28fef3 Author: Jim Jowdy <[email protected]> Date: Wed Mar 29 15:09:11 2023 -0700 Fix retrieve_indexes_from_table when indexes is empty and base table does not exist. (ctran#849) Some tables may have a table_name_prefix but no indexes. Previous versions of the code would strip the prefix and look for indexes on the resulting table which likely would not exist. This causes DB errors, at least in MySQL. So now check if the new table exists first before trying to show its indexes. commit 13b532d Author: Cuong Tran <[email protected]> Date: Wed Mar 29 01:51:15 2023 -0700 Update codeql-analysis.yml commit ea4cd00 Author: Lovro Bikić <[email protected]> Date: Wed Mar 29 10:31:01 2023 +0200 Add support for annotating check constraints (ctran#868) This adds annotation of check constraints with an option to disable/enable annotation. Most of the work done in this PR is based off of existing implementation for annotating indexes and foreign keys. Signed-off-by: Lovro Bikic <[email protected]> commit 76a1804 Author: Lovro Bikić <[email protected]> Date: Wed Mar 29 10:18:24 2023 +0200 Fix flaky specs (ctran#980) Signed-off-by: Lovro Bikic <[email protected]>
1 parent 8a9b8b0 commit 7a15b5c

12 files changed

+375
-65
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,61 @@
1-
name: "Code scanning - action"
1+
name: "CodeQL"
22

33
on:
44
push:
5-
branches-ignore:
6-
- pr/*
7-
- scratch/*
5+
branches: [ "develop" ]
86
pull_request:
7+
# The branches below must be a subset of the branches above
8+
branches: [ "develop" ]
99
schedule:
10-
- cron: '0 9 * * MON'
10+
- cron: '26 5 * * 4'
1111

1212
jobs:
13-
CodeQL-Build:
14-
13+
analyze:
14+
name: Analyze
1515
runs-on: ubuntu-latest
16+
permissions:
17+
actions: read
18+
contents: read
19+
security-events: write
20+
21+
strategy:
22+
fail-fast: false
23+
matrix:
24+
language: [ 'ruby' ]
1625

1726
steps:
1827
- name: Checkout repository
1928
uses: actions/checkout@v3
20-
with:
21-
# We must fetch at least the immediate parents so that if this is
22-
# a pull request then we can checkout the head.
23-
fetch-depth: 2
24-
25-
# If this run was triggered by a pull request event, then checkout
26-
# the head of the pull request instead of the merge commit.
27-
- run: git checkout HEAD^2
28-
if: ${{ github.event_name == 'pull_request' }}
29-
29+
3030
# Initializes the CodeQL tools for scanning.
3131
- name: Initialize CodeQL
3232
uses: github/codeql-action/init@v2
33-
# Override language selection by uncommenting this and choosing your languages
34-
# with:
35-
languages: ruby
33+
with:
34+
languages: ${{ matrix.language }}
35+
# If you wish to specify custom queries, you can do so here or in a config file.
36+
# By default, queries listed here will override any specified in a config file.
37+
# Prefix the list here with "+" to use these queries and those in the config file.
38+
39+
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
40+
# queries: security-extended,security-and-quality
3641

37-
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
42+
43+
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
3844
# If this step fails, then you should remove it and run the build manually (see below)
3945
- name: Autobuild
4046
uses: github/codeql-action/autobuild@v2
4147

4248
# ℹ️ Command-line programs to run using the OS shell.
43-
# 📚 https://git.io/JvXDl
49+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
4450

45-
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
46-
# and modify them (or add more) to build your code if your project
47-
# uses a compiled language
51+
# If the Autobuild fails above, remove it and uncomment the following three lines.
52+
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
4853

49-
#- run: |
50-
# make bootstrap
51-
# make release
54+
# - run: |
55+
# echo "Run, Build Application using script"
56+
# ./location_of_script_within_repo/buildscript.sh
5257

5358
- name: Perform CodeQL Analysis
5459
uses: github/codeql-action/analyze@v2
60+
with:
61+
category: "/language:${{matrix.language}}"

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
[![Gem Version](https://badge.fury.io/rb/annotate.svg)](http://badge.fury.io/rb/annotate)
44
[![Downloads count](https://img.shields.io/gem/dt/annotate.svg?style=flat)](https://rubygems.org/gems/annotate)
5-
[![Build status](https://travis-ci.org/ctran/annotate_models.svg?branch=develop)](https://travis-ci.org/ctran/annotate_models)
65
[![CI Status](https://github.com/ctran/annotate_models/workflows/CI/badge.svg)](https://github.com/ctran/annotate_models/actions?workflow=CI)
76
[![Coveralls](https://coveralls.io/repos/ctran/annotate_models/badge.svg?branch=develop)](https://coveralls.io/r/ctran/annotate_models?branch=develop)
87
[![Maintenability](https://codeclimate.com/github/ctran/annotate_models/badges/gpa.svg)](https://codeclimate.com/github/ctran/annotate_models)
9-
[![Inline docs](http://inch-ci.org/github/ctran/annotate_models.svg?branch=develop)](http://inch-ci.org/github/ctran/annotate_models)
108

119
Add a comment summarizing the current schema to the top or bottom of each of your...
1210

@@ -224,6 +222,7 @@ you can do so with a simple environment variable, instead of editing the
224222
-a, --active-admin Annotate active_admin models
225223
-v, --version Show the current version of this gem
226224
-m, --show-migration Include the migration version number in the annotation
225+
-c, --show-check-constraints List the table's check constraints in the annotation
227226
-k, --show-foreign-keys List the table's foreign key constraints in the annotation
228227
--ck, --complete-foreign-keys
229228
Complete foreign key names in the annotation

lib/annotate/annotate_models.rb

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,18 @@ def retrieve_indexes_from_table(klass)
125125

126126
# Try to search the table without prefix
127127
table_name_without_prefix = table_name.to_s.sub(klass.table_name_prefix, '')
128-
klass.connection.indexes(table_name_without_prefix)
128+
if klass.connection.table_exists?(table_name_without_prefix)
129+
klass.connection.indexes(table_name_without_prefix)
130+
else
131+
[]
132+
end
129133
end
130134

131135
# Use the column information in an ActiveRecord class
132136
# to create a comment block containing a line for
133137
# each column. The line contains the column name,
134138
# the type (and length), and any optional attributes
135-
def get_schema_info(klass, header, options = {})
139+
def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/MethodLength
136140
info = "# #{header}\n"
137141
info << get_schema_header_text(klass, options)
138142

@@ -179,6 +183,10 @@ def get_schema_info(klass, header, options = {})
179183
info << get_foreign_key_info(klass, options)
180184
end
181185

186+
if options[:show_check_constraints] && klass.table_exists?
187+
info << get_check_constraint_info(klass, options)
188+
end
189+
182190
info << get_schema_footer_text(klass, options)
183191
end
184192

@@ -353,6 +361,35 @@ def get_foreign_key_info(klass, options = {})
353361
fk_info
354362
end
355363

364+
def get_check_constraint_info(klass, options = {})
365+
cc_info = if options[:format_markdown]
366+
"#\n# ### Check Constraints\n#\n"
367+
else
368+
"#\n# Check Constraints\n#\n"
369+
end
370+
371+
return '' unless klass.connection.respond_to?(:supports_check_constraints?) &&
372+
klass.connection.supports_check_constraints? && klass.connection.respond_to?(:check_constraints)
373+
374+
check_constraints = klass.connection.check_constraints(klass.table_name)
375+
return '' if check_constraints.empty?
376+
377+
max_size = check_constraints.map { |check_constraint| check_constraint.name.size }.max + 1
378+
check_constraints.sort_by(&:name).each do |check_constraint|
379+
expression = check_constraint.expression ? "(#{check_constraint.expression.squish})" : nil
380+
381+
cc_info << if options[:format_markdown]
382+
cc_info_markdown = sprintf("# * `%s`", check_constraint.name)
383+
cc_info_markdown << sprintf(": `%s`", expression) if expression
384+
cc_info_markdown << "\n"
385+
else
386+
sprintf("# %-#{max_size}.#{max_size}s %s", check_constraint.name, expression).rstrip + "\n"
387+
end
388+
end
389+
390+
cc_info
391+
end
392+
356393
# Add a schema block to a file. If the file already contains
357394
# a schema info block (a comment starting with "== Schema Information"),
358395
# check if it matches the block that is already there. If so, leave it be.

lib/annotate/annotate_routes.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ def do_annotations(options = {})
2929
content, header_position = Helpers.strip_annotations(existing_text)
3030
new_content = annotate_routes(HeaderGenerator.generate(options), content, header_position, options)
3131
new_text = new_content.join("\n")
32-
33-
if rewrite_contents(existing_text, new_text)
32+
if rewrite_contents(existing_text, new_text, options[:frozen])
3433
puts "#{routes_file} was annotated."
3534
else
3635
puts "#{routes_file} was not changed."
@@ -40,13 +39,13 @@ def do_annotations(options = {})
4039
end
4140
end
4241

43-
def remove_annotations(_options={})
42+
def remove_annotations(options={})
4443
if routes_file_exist?
4544
existing_text = File.read(routes_file)
4645
content, header_position = Helpers.strip_annotations(existing_text)
4746
new_content = strip_on_removal(content, header_position)
4847
new_text = new_content.join("\n")
49-
if rewrite_contents(existing_text, new_text)
48+
if rewrite_contents(existing_text, new_text, options[:frozen])
5049
puts "Annotations were removed from #{routes_file}."
5150
else
5251
puts "#{routes_file} was not changed (Annotation did not exist)."
@@ -82,13 +81,15 @@ def strip_on_removal(content, header_position)
8281
content
8382
end
8483

85-
def rewrite_contents(existing_text, new_text)
86-
if existing_text == new_text
87-
false
88-
else
84+
def rewrite_contents(existing_text, new_text, frozen)
85+
content_changed = (existing_text != new_text)
86+
87+
if content_changed
88+
abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if frozen
8989
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
90-
true
9190
end
91+
92+
content_changed
9293
end
9394

9495
def annotate_routes(header, content, header_position, options = {})

lib/annotate/constants.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ module Constants
1818
:trace, :timestamp, :exclude_serializers, :classified_sort,
1919
:show_foreign_keys, :show_complete_foreign_keys,
2020
:exclude_scaffolds, :exclude_controllers, :exclude_helpers,
21-
:exclude_sti_subclasses, :ignore_unknown_models, :with_comment
21+
:exclude_sti_subclasses, :ignore_unknown_models, :with_comment,
22+
:show_check_constraints
2223
].freeze
2324

2425
OTHER_OPTIONS = [

lib/annotate/parser.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def commit
4848
end
4949
end
5050

51-
def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
51+
def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
5252
has_set_position = {}
5353

5454
option_parser.banner = 'Usage: annotate [options] [model_file]*'
@@ -173,6 +173,12 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
173173
env['include_version'] = 'yes'
174174
end
175175

176+
option_parser.on('-c',
177+
'--show-check-constraints',
178+
"List the table's check constraints in the annotation") do
179+
env['show_check_constraints'] = 'yes'
180+
end
181+
176182
option_parser.on('-k',
177183
'--show-foreign-keys',
178184
"List the table's foreign key constraints in the annotation") do

lib/generators/annotate/templates/auto_annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if Rails.env.development?
1717
'position_in_fixture' => 'before',
1818
'position_in_factory' => 'before',
1919
'position_in_serializer' => 'before',
20+
'show_check_constraints' => 'false',
2021
'show_foreign_keys' => 'true',
2122
'show_complete_foreign_keys' => 'false',
2223
'show_indexes' => 'true',

lib/tasks/annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ task annotate_models: :environment do
1818
options[:position_in_factory] = Annotate::Helpers.fallback(ENV['position_in_factory'], ENV['position'])
1919
options[:position_in_test] = Annotate::Helpers.fallback(ENV['position_in_test'], ENV['position'])
2020
options[:position_in_serializer] = Annotate::Helpers.fallback(ENV['position_in_serializer'], ENV['position'])
21+
options[:show_check_constraints] = Annotate::Helpers.true?(ENV['show_check_constraints'])
2122
options[:show_foreign_keys] = Annotate::Helpers.true?(ENV['show_foreign_keys'])
2223
options[:show_complete_foreign_keys] = Annotate::Helpers.true?(ENV['show_complete_foreign_keys'])
2324
options[:show_indexes] = Annotate::Helpers.true?(ENV['show_indexes'])

lib/tasks/annotate_routes.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ task :annotate_routes => :environment do
1414
options[:position_in_routes] = Annotate::Helpers.fallback(ENV['position_in_routes'], ENV['position'])
1515
options[:ignore_routes] = Annotate::Helpers.fallback(ENV['ignore_routes'], nil)
1616
options[:require] = ENV['require'] ? ENV['require'].split(',') : []
17+
options[:frozen] = Annotate::Helpers.true?(ENV['frozen'])
1718
options[:wrapper_open] = Annotate::Helpers.fallback(ENV['wrapper_open'], ENV['wrapper'])
1819
options[:wrapper_close] = Annotate::Helpers.fallback(ENV['wrapper_close'], ENV['wrapper'])
1920
AnnotateRoutes.do_annotations(options)

0 commit comments

Comments
 (0)