Skip to content

Instantly share code, notes, and snippets.

@clarkdave
clarkdave / pg_sequence.rb
Last active December 19, 2015 09:39
basic support for PostgreSQL sequences in ActiveRecord migrations - place in an initializer
module PgSequence
module PostgreSQLAdapter
def create_sequence(name)
execute "CREATE SEQUENCE #{name}"
end
def drop_sequence(name)
execute "DROP SEQUENCE #{name}"
end
end
@clarkdave
clarkdave / pg_enum.rb
Last active July 18, 2019 15:37
basic support for PostgreSQL Enums in ActiveRecord - to be placed in an initializer
##
# This provides `create_enum` and `drop_enum` methods for migrations, which creates
# a Postgres ENUM type with the provided values.
#
# It'll also dump these into the schema.rb to be loaded into another DB (e.g. rake db:test:load)
#
# In order to reference the new enums as actual types, ActiveRecord needs to know about them, so
# make sure they are all represented in the `types` array below
#
# Then you can reference them in your migrations, e.g.
@clarkdave
clarkdave / knife-ssh-on-vpc-servers.sh
Last active December 19, 2015 19:49
(CHEF) knife ssh on servers in an Amazon VPC
knife ssh 'name:*' 'uptime' -G user@gateway.example.com -a private_ipaddress -x user
# to run chef-client on all servers:
knife ssh 'name:*' 'sudo chef-client' -G user@gateway.example.com -a private_ipaddress -x user
@clarkdave
clarkdave / json_enhancements.rb
Created July 17, 2013 14:55
(CHEF) Install PostgreSQL json_enhancements & hstore
package 'postgresql-server-dev-9.2'
package 'postgresql-contrib-9.2'
# without hstore this won't build without fiddling, so it's easiest just to install hstore too
bash 'build_json_enhancements' do
code <<-CODE
cd /var/cache/chef
wget https://bitbucket.org/qooleot/json_enhancements/get/master.tar.gz
tar -xvzf master.tar.gz
@clarkdave
clarkdave / pg_interval.rb
Last active July 9, 2019 00:57
Support for PostgreSQL `interval` type in Rails 4. Although ActiveRecord supports `interval` by default, it turns it into a string (and tells Postgres the column is type string, too). This means you don't get any proper interval goodness. By sticking this code into an initialiser, ActiveRecord will create proper `interval` column types in Postgr…
#
# This will force ActiveRecord to create proper `interval` column types in PostgreSQL
#
# def change
# add_column :leases, :period, :interval
# end
#
# This applies to a generated `schema.rb` file too.
#
# No special OID type is applied to an `interval` type. Rails will treat it as a string, although
@clarkdave
clarkdave / cloudfront-invalidation-grunt.js
Created October 21, 2013 12:18
Create an AWS Cloudfront invalidation in a Grunt task
grunt.registerTask('cloudfront:invalidate', 'invalidate lib on cloudfront', function() {
var finished = this.async(),
aws = require('aws-sdk'),
path = '/libs/ll.sdk-' + sdk_version + '.js'
;
// set aws keys & region from config
aws.config.update({
@clarkdave
clarkdave / change-postgresql-data-dir.rb
Last active August 29, 2015 13:57
(CHEF) change PostgreSQL (9.2) data directory
# changing the postgresql data directory is an annoyingly non-trivial and fragile operation
# this should mostly do it but there are lots of things that can go wrong and if/when they do
# you will have lots of fun trying to eradicate broken postgres installs between chef runs
#
config_dir = "etc/postgresql/#{node[:postgresql][:version]}/main"
bash 'change_data_directory' do
code <<-CODE
mkdir /tmp/pg_conf_backup
@clarkdave
clarkdave / pgsql-query-json-array.sql
Created March 26, 2014 16:05
(PostgreSQL) Query something in a JSON array in a WHERE clause
-- when you have a record which looks like this
--
-- id: 5,
-- properties: {
-- ages: [20, 30]
-- }
--
-- it is a bit of a pain if you need to query based on the contents of the "ages" array inside the JSON object "properties"
-- because PG currently lacks easy to use operators to work with JSON arrays
@clarkdave
clarkdave / ansible-prestashop.yml
Created April 25, 2014 10:17
(Ansible) Playbook to provision a Ubuntu VM for Prestashop, designed for vagrant
# this will provision a Ubuntu box with a basic lamp stack including a db/user for Prestashop
# it downloads Prestashop 1.5 into /tmp but doesn't install it, although you could quite easily
# extend this playbook to do that as you can run the prestashop installer via the shell
---
- hosts: all
sudo: True
tasks:
- name: apt-get update
command: apt-get update
@clarkdave
clarkdave / logstash.conf
Created May 10, 2014 08:16
Logstash / Sentry config
input {
redis {
'data_type' => "list"
'host' => "127.0.0.1"
'key' => "logstash"
'type' => "redis-input"
}
}
filter {