Skip to content

Instantly share code, notes, and snippets.

🔥
🚒

Tom Lord tom-lord

🔥
🚒
Block or report user

Report or block tom-lord

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@tom-lord
tom-lord / rails_admin.de.yml
Created Dec 19, 2018 — forked from soemo/rails_admin.de.yml
German translation for RailsAdmin
View rails_admin.de.yml
# German rails_admin translation
de:
admin:
home:
name: "Home"
pagination:
previous: "« Vor"
next: "Zurück »"
truncate: ""
View lease_deals_index_migration_2.rb
class AddOrderedDealershipIdExpiredAtIndexToLeaseDeals < ActiveRecord::Migration[5.2]
def change
remove_index :lease_deals, name: :index_lease_deals_on_dealership_id_and_expired_at, algorithm: :concurrently
add_index :lease_deals, %i[id dealership_id expired_at], where: 'expired_at IS NULL', using: :btree, order: { id: :asc }, algorithm: :concurrently
end
end
View lease_deal_expire_all_3.rb
class LeaseDeal < ApplicationRecord
def self.expire_all(time: Time.current, batch_size: 1000)
ids = pluck(:id)
ids.each_slice(batch_size) do |ids_slice|
unscoped.where(id: ids_slice).update_all(expired_at: time)
end
end
end
View active_record_in_batches.rb
# Snippets taken from:
# https://github.com/rails/rails/blob/fc5dd0b85189811062c85520fd70de8389b55aeb/activerecord/lib/active_record/relation/batches.rb#L201
def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil)
relation = self
# ...
batch_limit = of
# ...
relation = relation.reorder(batch_order).limit(batch_limit)
relation = apply_limits(relation, start, finish)
View lease_deals_explain_3.sql
Limit (cost=0.11..4232.55 rows=1000 width=4)
-> Index Scan using lease_deals_pkey on lease_deals (cost=0.11..3130311.24 rows=739600 width=4)
Filter: ((expired_at IS NULL) AND (updated_at < '2018-10-09 09:26:33.507679'::timestamp without time zone) AND (dealership_id = 5678))
View lease_deals_explain_2.sql
Bitmap Heap Scan on lease_deals (cost=448.02..903.13 rows=5903 width=205)
Recheck Cond: ((dealership_id = 1234) AND (expired_at IS NULL))
Filter: ((updated_at < '2018-10-09 09:34:11.818701'::timestamp without time zone))
-> Bitmap Index Scan on index_lease_deals_on_dealership_id_and_expired_at (cost=0.00..411.04 rows=6724 width=0)
Index Cond: (dealership_id = 1234)
View lease_deals_index_migration.rb
class AddPartialIndexOnDealershipIdAndExpiredAtToLeaseDeals < ActiveRecord::Migration[5.2]
def change
add_index :lease_deals, %i[dealership_id expired_at], where: 'expired_at IS NULL', algorithm: :concurrently
end
end
View lease_deals_explain_1.sql
Bitmap Heap Scan on lease_deals (cost=172.99..18520.04 rows=5903 width=205)
Recheck Cond: (dealership_id = 1234)
Filter: ((expired_at IS NULL) AND (updated_at < '2018-10-09 09:34:11.818701'::timestamp without time zone))
-> Bitmap Index Scan on index_lease_deals_on_dealership_id (cost=0.00..172.70 rows=9724 width=0)
Index Cond: (dealership_id = 1234)
View lease_deals_error_2.sql
SELECT "lease_deals"."id"
FROM "lease_deals"
WHERE "lease_deals"."expired_at" IS NULL
AND "lease_deals"."dealership_id" = 1234
AND ( updated_at < '2018-10-09 09:34:11.818701' )
ORDER BY "lease_deals"."id" ASC
LIMIT 1000
View lease_deal_expire_all_2.rb
class LeaseDeal < ApplicationRecord
def self.expire_all(time: Time.current, batch_size: 1000)
in_batches(of: batch_size).update_all(expired_at: time)
end
end
You can’t perform that action at this time.