Skip to content

Instantly share code, notes, and snippets.

View rlburkes's full-sized avatar

Randy Burkes rlburkes

View GitHub Profile
@rlburkes
rlburkes / foo.sql
Created February 17, 2018 21:21
Crazy Migration
CREATE TRIGGER migrate_products AFTER UPDATE OF migrated ON products FOR EACH ROW WHEN (OLD.migrated IS NULL AND NEW.migrated IS true) EXECUTE PROCEDURE populate_referenced_entities();
CREATE OR REPLACE FUNCTION batch_set_migrated() RETURNS INTEGER LANGUAGE plpgsql AS $$
DECLARE batched_count INTEGER = 1;
BEGIN
WITH unmigrated_products AS (
SELECT id
FROM products
WHERE migrated IS NULL
LIMIT 50
{
"id": "{SYSTEM_ID}",
"parent_id": "{PARENT_SYSTEM_ID}",
"product_type": [ "leaf" ],
"updated_at": "2016-04-07T12:52:03.059Z",
"indexed_at": "2016-04-07T13:29:54.262+00:00",
"created_at": "2016-04-07T13:29:54.262+00:00",
"list_membership": [
"{SYSTEM_ID}"
],
class ProductAttribute < ActiveRecord::Base
include DiscriminableModel
uses_type_column :data_type do |data_type|
"#{data_type.to_s.camelize}ProductAttribute".safe_constantize
end
...
end
# Wouldn't it be great if you could have STI like functionality
# without needing to encode strings of class names in the database?
# Well today is your lucky day! Discriminable Model is here to help.
#
# Simply specify your models desired type column, and provide a block to
# do the discrimination. If you want the whole STI-esque shebang of properly
# typed finder methods you can supply an array of 'discriminate_types' that will
# be used to apply an appropriate type.
#
# class MyModel < ActiveRecord::Base
module WorkerExtensions
WorkerProcess = Struct.new(:id, :name, :app_name, :size) do
ONE_X_WORKER_SIZE = 512.0
TWO_X_WORKER_SIZE = 1024.0
def memory_ceiling
size == 1 ? ONE_X_WORKER_SIZE : TWO_X_WORKER_SIZE
end
end
@rlburkes
rlburkes / djyiap.rb
Last active August 29, 2015 14:08
Example of what Delayed::Plugins::YouthInAsiaPlugin might look like
# Automatically kill jobs' worker when it finishes
# if it used more memory than allotted.
# Ali-G: Lets talk about doctors who is ending the life of people who is suffering.
# Ali-G What’s it got to do with the youth in Asia I mean it aiint their fault these peoples is dying, they’s thousands of miles away.
# Doctor: Euthanasia means mercy killing, it literally means dying well. We're not talking about the youth in Asia, (or) the youth in Africa.
# Ali-G: But why is you blaming that on the asian kids or whatever?
module Delayed
module Plugins
class YouthInAsiaPlugin < Delayed::Plugin
NoMethodError: undefined method `meets_all_requirements?' for #<ProductExport:0x007fae799b32a8>
@rlburkes
rlburkes / 3.rb
Last active August 29, 2015 13:56
class MarkProductExportsWhichMeetAllRequirements < ActiveRecord::Migration
class Export < ActiveRecord::Base
attr_accessible :meets_requirements
self.inheritance_column = nil
def meets_all_requirements?
# Do some complex business logic which cannot be expressed via sql query
end
@rlburkes
rlburkes / 2.rb
Last active August 29, 2015 13:56
class MarkProductExportsWhichMeetAllRequirements < ActiveRecord::Migration
class Export < ActiveRecord::Base
attr_accessible :meets_requirements
def meets_all_requirements?
# Do some complex business logic which cannot be expressed via sql query
end
end
@rlburkes
rlburkes / 1.rb
Last active August 29, 2015 13:56
class MarkProductExportsWhichMeetAllRequirements < ActiveRecord::Migration
Export = Class.new(ActiveRecord::Base)
class ProductExport < Export
attr_accessible :meets_requirements
def meets_all_requirements?
# Do some complex business logic which cannot be expressed via sql query
end