Skip to content

Instantly share code, notes, and snippets.

How to run examples

  1. Run $ createdb uniq-db-test to create DB
  2. Run example with Ruby (e.g., $ ruby 1_find_or_create_by_single_thread.rb)

Benchmark output

With many successful INSERTs

Warming up --------------------------------------
@DmitryTsepelev
DmitryTsepelev / 1_graphql_ruby_n_plus_one.md
Last active January 5, 2024 15:16
N+1 in graphql-ruby

How to run examples:

  1. Run $ createdb nplusonedb to create DB
  2. Run specs $ rspec demo.rb
@DmitryTsepelev
DmitryTsepelev / change_column_type.rb
Created August 27, 2020 08:46
How to change column type (e.g., int -> bigint) without downtime
ActiveRecord::Migration.remove_foreign_key(:current_table, :foreign_table) # no lock
ActiveRecord::Migration.add_column(:current_table, :column_bigint, :bigint) # no lock
copy_data = lambda do
CurrentTable.where(column_bigint: nil).where.not(column: nil).in_batches do |batch|
batch.update_all("column_bigint = column")
end
end
import Control.Applicative hiding (many)
import Data.Char (toLower, isAlphaNum)
import Data.Either (isRight)
import Data.List (isPrefixOf)
data Parser a = Parser { parse :: String -> Either String (String, a) }
satisfy :: (Char -> Bool) -> Parser Char
satisfy pr = Parser f where
f "" = Left "unexpected end of input"

Snippets for the article about natural language programming with Ruby

Examples for my metaprogramming/DSL talk

Setup

Results gathered from my MacBook Pro Mid 2014 (2,5 GHz Quad-Core Intel Core i7, 16 GB 1600 MHz DDR3)

GraphQL Parsing

                                   user     system      total        real
1 fields, 0 nested levels:     0.000153   0.000002   0.000155 (  0.000152)
1 fields, 2 nested levels:     0.000188   0.000001   0.000189 (  0.000187)
@DmitryTsepelev
DmitryTsepelev / graphql_controller.rb
Created January 20, 2020 08:14
ruby-graphql + HTTP GET
class GraphqlController < ApplicationController
include EnsureHash
def execute
result = GraphqlSchema.execute(
params[:query],
variables: ensure_hash(params[:variables]),
context: context,
operation_name: params[:operationName],
)

How to run examples:

  1. Run $ createdb railstestdb to create DB
  2. Run examples with Ruby $ ruby demo.rb
@DmitryTsepelev
DmitryTsepelev / boilerplate.rb
Last active September 22, 2019 09:45
GraphQL field extensions
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "graphql"
end
class BaseObject < GraphQL::Schema::Object
end