Skip to content

Instantly share code, notes, and snippets.

View mz026's full-sized avatar

Yang-Hsing Lin mz026

View GitHub Profile
@mz026
mz026 / read-access.sql
Created May 23, 2021 13:59 — forked from oinopion/read-access.sql
How to create read only user in PostgreSQL
-- Create a group
CREATE ROLE readaccess;
-- Grant access to existing tables
GRANT USAGE ON SCHEMA public TO readaccess;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readaccess;
-- Grant access to future tables
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readaccess;
-- list row counts
SELECT schemaname,relname,n_live_tup
FROM pg_stat_user_tables
ORDER BY n_live_tup DESC;
-- see vacuum records
SELECT
schemaname, relname,
last_vacuum, last_autovacuum,
@mz026
mz026 / recaptcha-react.js
Created July 24, 2018 02:52
POC of reCAPTCHA with React
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
const SITE_KEY = 'xxxx'
class ReCAPTCHA extends Component {
constructor(props) {
super(props)
this._execute = this._execute.bind(this)
@mz026
mz026 / test-conn.rake
Last active May 24, 2017 06:57
test connection pool v.s. thread v.s. `with_connection`
# with pool size = 2
def query thread_idx
p "#{thread_idx}: in thread"
p "#{thread_idx}: conn id: #{ActiveRecord::Base.connection.object_id}"
p "#{thread_idx}: #{User.count}"
puts "#{thread_idx}: finish query"
sleep 0.5
puts "#{thread_idx}: end thread"
end
@mz026
mz026 / add_random_id_generator.rb
Last active October 4, 2016 07:21
Random id generator of postgres
class AddRandomIdGenerator < ActiveRecord::Migration
# ref: http://www.rightbrainnetworks.com/blog/base36-conversion-in-postgresql/
# ref: http://rob.conery.io/2014/05/29/a-better-id-generator-for-postgresql/
def up
func_sql = <<-SQL
CREATE OR REPLACE FUNCTION base36_encode(IN digits bigint, IN min_width int = 0)
RETURNS varchar AS $$
DECLARE
chars char[];
ret varchar;
@mz026
mz026 / postgres_add_foreign_keys.rb
Last active August 30, 2016 05:14
Postgres create foreign key constraint
# add foreign key constraint
execute <<-SQL
alter table access_tokens
add constraint fk_access_token_user_id
foreign key (user_id)
references users(id)
on delete cascade
SQL
# add unique index
class Ajax {
static respondTo(request, resHash, code = 200) {
request.respond(code,
{ 'Content-Type': 'application/json' },
JSON.stringify(resHash));
}
constructor() {
this._ajax = sinon.useFakeXMLHttpRequest();
let requests = this.requests = [];
@mz026
mz026 / pre-commit
Last active August 29, 2015 14:15 — forked from avolkov/pre-commit
#!/bin/sh
###
# Strip whitespaces and convert to Unix newlines.
# Source: http://stackoverflow.com/a/3516525/86294
# Save this file into git config directory as .git/hooks/pre-commit
###
if git-rev-parse --verify HEAD >/dev/null 2>&1 ; then
against=HEAD
else
$buttons2x-sprites: sprite-map('buttons2x/*.png');
%buttons2x-sprite {
background-image: sprite-url($buttons2x-sprites);
}
@mixin buttons2x-sprite($sprite-name){
@extend %buttons2x-sprite;
background-repeat: no-repeat;
@mz026
mz026 / gist:33430e40db74124f50b7
Created November 18, 2014 03:55
RSpec required in controller params
# Usage:
# include_examples 'require_in_params', :tag, ->(group) { post :create, params }
shared_examples "require_in_params" do |attr, block|
it "returns 400 if no `#{attr}` in params" do
params.delete attr
self.instance_eval &block
expect(response.status).to eq(400)
end
end