Skip to content

Instantly share code, notes, and snippets.

View chunpan's full-sized avatar

Chun Pan (Billy) chunpan

View GitHub Profile
@chunpan
chunpan / find_zero_dates.rb
Last active July 20, 2021 01:35 — forked from jdvkivu/find_zero_dates.rb
Ruby on Rails script to check mysql zero dates
#
# Check for every table with temporal columns if there are records that have dates set to '0000-00-00'
conn = ActiveRecord::Base.connection
# get a list of tables and columns with a date type excluding merge tables
query = "select concat(`table_schema`,'.',`table_name`) as table_name,`COLUMN_NAME` from information_schema.`columns` where data_type like '%date%' AND concat(`table_schema`,'.',`table_name`) not in (select concat(`table_schema`,'.',`table_name`) from information_schema.tables where engine='mrg_myisam');"
list = conn.select_all( query )
# transform the list a bit for later querying
@chunpan
chunpan / auto_truncate_string_column.rb
Last active April 21, 2021 22:17
Ruby Module to Allow Auto Truncation of String Columns using ActiveRecord Callbacks
require 'active_support/core_ext/class/attribute'
require 'active_support/concern'
# This module enables a simple declaration for the including ActiveRecord model
# class to specify one or more columns (attributes) to automatically truncate
# its length to a max value or as defined by the column width in your schema.
#
# Usage example:
#
# class MyModel << ActiveRecord::Base
@chunpan
chunpan / feature_toggle_service.rb
Created February 26, 2017 05:12
A Simple File-Based Feature Toggler
# A service to properly route feature toggles, similar to Promote's Gate mechanism,
# but greatly simplified.
# @see https://martinfowler.com/articles/feature-toggles.html
class FeatureToggleService
attr_reader :config
DEFAULT_CONFIG_PATH = Rails.root.join('config', 'feature_toggles.yml')
def initialize(config: nil)
@config =

Keybase proof

I hereby claim:

  • I am chunpan on github.
  • I am bpan (https://keybase.io/bpan) on keybase.
  • I have a public key whose fingerprint is 25B5 823C F1D5 AE22 04AD 48B9 BDD5 B72A BCEE 81E8

To claim this, I am signing this object: