Skip to content

Instantly share code, notes, and snippets.

View bjeanes's full-sized avatar
🕊️

Bo Jeanes bjeanes

🕊️
View GitHub Profile
@bjeanes
bjeanes / authentication.rb
Last active January 25, 2022 16:05
Example of custom code to emulate a programmatic core in Rodauth
# frozen_string_literal: true
module Authentication
# Rodauth is pretty coupled to being in a request context, but this will provide the minimum necessary to be able
# to generate correct URLs
def self.rodauth(configuration_name = :user, params: {})
url_options = Rails.application.config.action_mailer.default_url_options
host = url_options[:host]
host += ":#{url_options[:port]}" if url_options.key?(:port)
base_url = "#{url_options[:protocol] || 'http'}://#{host}"
@bjeanes
bjeanes / rodauth__features__rails_conventions.rb
Created November 23, 2020 19:55
Make Rodauth routes, tables
require 'rodauth'
module Rodauth
# Clean up how we configure all of our tables, columns, etc., to use values which are more consistent with Rails than
# the defaults as well as use Rails mailers
Feature.define(:rails_conventions, :RailsConventions) do
depends :rails # rodauth-rails feature
def post_configure
# Rodauth uses dash-separated paths by default, so we'll make these underscores by default
# frozen_string_literal: true
require 'rodauth'
# Rodauth calls `clear_session` every time it updates the session. This includes when logging in. Unfortunately, this
# means that logging in as an admin will log you out as a user, and vice versa.
#
# This feature introduces a notion of a "scoped" session (inside Rodauth only) that has the following behaviours:
#
# * Adding a key to the session adds it as a sub-hash, scoped by the configuration name.
@bjeanes
bjeanes / gl2gt.rb
Last active October 26, 2020 22:38
scrappy script to migrate Gitlab->Gitea
#!/usr/bin/env ruby
## NOTE: sometimes (probably because GL can't handle load) the repo is created but the import/migrate fails.
## go to <gitea>/admin/repos?sort=size to find 0-byte repos, delete them, and try again. Unfortunately,
## deleting these repos (either because of that page or because the repo isn't fully initialised) doesn't
## clean up on disk, so I had to manually `rm -rf` them too.
require 'json'
require 'uri'
require 'net/https'
@bjeanes
bjeanes / README.md
Last active June 5, 2020 06:37
Using a Yubikey on OS X in non-QWERTY layouts

Using a Yubikey on OS X in non-QWERTY layouts

Using Karabiner This uses Karabiner (previously called KeyRemap4Macbook) to re-map just the Yubikey device back to QWERTY at the OS-level, but only when you aren't already using QWERTY!

Instructions

  1. Install Karabiner.
@bjeanes
bjeanes / email.sql
Created April 7, 2016 01:44
Email domain type for postgres
CREATE EXTENSION IF NOT EXISTS citext;
-- http://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690
CREATE DOMAIN email AS citext
CHECK (
VALUE IS NULL OR (
-- is not more than 254 chars
length(VALUE) <= 254 AND
-- vaguely looks like an email address
@bjeanes
bjeanes / uuid64.rb
Last active June 5, 2020 00:09
Compact URL-safe UUID representation. It might almost be worth having a base66 version (there are 66 url-safe chars)
require 'securerandom'
require 'base64'
module UUID64
extend self
def generate
encode SecureRandom.uuid
end
require 'command/result'
require 'command/result/switch'
module Command
def self.included(klass)
klass.extend Command::ClassMethods
end
module ClassMethods
def call(**options, &block)
@bjeanes
bjeanes / no_new_nulls_trigger.sql
Last active June 5, 2020 00:07
Trigger to prevent introducing new NULL values to a list of specified columns. I'm using this because I have an 8mil-row, 100GB (with indexes) table that I can't afford to lock to add a NOT NULL constraint. This allows me to start enforcing data now, and back-fill the values without downtime. Later, when I can schedule downtime, another constrai…
/*
* Prevent INSERTs or UPDATEs that introduce NULLs. Useful for when the
* table is too big to add NOT NULL constraints without downtime that
* can't currently be performed.
*
* Use like:
*
* CREATE TRIGGER <name>
* AFTER INSERT OR UPDATE
* ON <table>

Postgres does not really fully support unicode the way some users might expect. Unicode (much to some people's chagrin) supports multiple ways of encoding complex characters.

For instance, the character could be:

  • ば (\u306f\u3099)
  • ば (\u3070)

Like-wise, and á are different (\u0061\u0301 and \u00e1).