Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
alpaca-tc / ridgepole_hashed_index_name.rb
Created May 23, 2021 13:10
ridgepoleで定義するindex名をhashのランダム名で定義する(古いRailsっぽい挙動)
# Schemafileから
# require_relative './lib/ridgepole_hashed_index_name'
# して使う
module RidgepoleHashedIndexName
def index_name(table_name, options)
if options.is_a?(Hash) && options[:column]
identifier = super
hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10)
"index_rails_#{hashed_identifier}"
@alpaca-tc
alpaca-tc / active_record_dowsing.rb
Created May 23, 2021 13:05
クエリに発行元のコメントを含める処理の簡易版
# frozen_string_literal: true
dowsing = Module.new do
def execute(sql, *)
source_location = query_source_location(caller.lazy)
sql = "#{sql} /* #{source_location} */" if source_location
super
end
private
@alpaca-tc
alpaca-tc / missing_model_i18n_spec.rb
Created May 23, 2021 12:55
ActiveRecordのi18nの定義漏れをテストする
# frozen_string_literal: true
RSpec.describe 'ActiveRecord i18n' do
let(:table_names) { ActiveRecord::Base.connection.tables }
let(:models) do
table_names.map do |table_name|
table_to_model(table_name)
rescue NameError
nil
@alpaca-tc
alpaca-tc / db_seed_spec.rb
Created May 23, 2021 12:51
rake db:seed が例外を吐かないことをテストする
# frozen_string_literal: true
RSpec.describe 'rake db:seed' do
it 'works fine' do
expect { load('db/seeds.rb') }.to_not raise_error
end
end
@alpaca-tc
alpaca-tc / activerecord_duplicated_indexes_spec.rb
Created May 23, 2021 12:49
重複するインデックスを検知する
# frozen_string_literal: true
RSpec.describe 'ActiveRecord duplicated indexes' do
let(:connection) do
ActiveRecord::Base.connection
end
it 'is not found' do
tables = connection.tables
duplicated_indexes = {}
@alpaca-tc
alpaca-tc / missing_foreign_keys_spec.rb
Created May 23, 2021 12:45
定義漏れの外部キーを自動検知する
# frozen_string_literal: true
RSpec.describe 'ActiveRecord foreign key' do
let(:connection) { ActiveRecord::Base.connection }
let(:tables) { connection.tables }
def foreign_key_exists?(reflection, foreign_keys)
to_table_name = reflection.klass.table_name
reflection.foreign_key
https://github.com/kufu/activerecord-bitemporal/pull/70/files#diff-77aac43b146505859dd74b1fd3e151c0e94174f5abc85475fe1dfba10a5c64bfL330
git clone https://github.com/kufu/activerecord-bitemporal
hub checkout https://github.com/kufu/activerecord-bitemporal/pull/70
# L330をObjectに戻す
vi lib/activerecord-bitemporal/bitemporal.rb
rbenv local 3.0.0
bundle i --path .bundle
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", '~> 6.1.0'
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", '~> 6.0.0'
@alpaca-tc
alpaca-tc / activemodel-inspector.rb
Created September 20, 2020 08:01
こういうのが欲しいけれど、どうやったらAM::Modelに入りうるだろうか...
module ActiveModelAttributesInspector
# @return [String]
def inspect
values = attributes.map { |key, value| "#{key}: #{value.inspect}" }.join(', ')
%(<#{self.class.name} #{values}>)
end
# @param pp [PP]
#
# @return [void]