Skip to content

Instantly share code, notes, and snippets.

View wosephjeber's full-sized avatar

Joe Weber wosephjeber

View GitHub Profile
@wosephjeber
wosephjeber / instructions.md
Last active January 8, 2025 19:03
Ecto migration for renaming table with indexes and constraints

Renaming table in Ecto migration

I recently wanted to rename a model and its postgres table in a Phoenix app. Renaming the table was simple and documented, but the table also had constraints, sequences, and indexes that needed to be updated in order for the Ecto model to be able to rely on default naming conventions. I couldn't find any examples of what this would look like but was eventually able to figure it out. For anyone else in the same situation, hopefully this example helps.

In the example below, I'm renaming the Permission model to Membership. This model belongs to a User and an Account, so it has foreign key constraints that need to be renamed.

defmodule MyApp.Repo.Migrations.RenamePermissionsToMemberships do
  use Ecto.Migration
@wosephjeber
wosephjeber / git_revision.js
Last active December 16, 2024 16:28
Get branch and commit names from Node (synchronously)
const { execSync } = require('child_process');
function executeGitCommand(command) {
return execSync(command)
.toString('utf8')
.replace(/[\n\r\s]+$/, '');
}
const BRANCH = executeGitCommand('git rev-parse --abbrev-ref HEAD');
const COMMIT_SHA = executeGitCommand('git rev-parse HEAD');
@wosephjeber
wosephjeber / ngrok-installation.md
Last active November 14, 2024 09:06
Installing ngrok on Mac

Installing ngrok on OSX

For Homebrew v2.6.x and below:

brew cask install ngrok

For Homebrew v2.7.x and above:

@wosephjeber
wosephjeber / note.md
Last active October 17, 2024 21:11
Resolving lockfile conflicts

Merge conflicts in lockfiles can be pretty gnarly to resolve. Here are my thoughts on how to resolve those safely (using yarn.lock as an example).

TLDR:

Don’t resolve conflicts manually or delete the lockfile. Revert the staged changes to the lockfile and run yarn install again.

Very long, only read if you’re interested:

Resolving merge conflicts manually (or editing a lockfile manually) can be a bad idea. Knowing which changes to accept or reject is very confusing, and manual edits can interfere with tools like yarn or npm’s ability to perform dependency resolution when changes were made to package.json.

@wosephjeber
wosephjeber / waitFor.js
Created November 29, 2023 21:06
[WIP] An RTL-like `waitFor` utility, without the DOM stuff
export default async function waitFor(callback, { timeout = 1000 } = {}) {
let intervalTimer;
let lastError = null;
let pending = false;
let timeoutTimer;
return new Promise((resolve, reject) => {
function onDone(result) {
clearInterval(intervalTimer);
clearTimeout(timeoutTimer);
@wosephjeber
wosephjeber / instructions.md
Last active October 12, 2023 10:05
Inspecting Chrome extension interfaces with React DevTools

Inspecting Chrome extension interfaces with React DevTools

Chrome does not allow extensions to modify other extensions, so React DevTools are not available in the Chrome DevTools interface when inspecting a Chrome extension interface. The React team has provided a standalone app that can be connected manually, but it requires some setup. The React DevTools readme includes some instructions, but they leave out an important step if you're debugging within a Chrome extension.

Here's an overview of how to set this up locally for an extension:

Run the standalone React DevTools app

The easiest way to start the standalone app is:

@wosephjeber
wosephjeber / notes.md
Created September 13, 2023 18:19
Notes from learning SolidJS

I'm learning SolidJS coming from React. I'm taking notes here on things that trip me up in the process.

  • Don't destructure props. It breaks reactivity with signals passed down as props.
  • Use untrack() to reference signal values in an effect without rerunning the effect when those signals change. Might be a less common use case? (Mine was referencing updated signal values in an interval started by the effect).
@wosephjeber
wosephjeber / index.js
Last active September 11, 2023 19:02
Quick Node script to find largest dependency in package.json
const { dependencies } = require('../package.json');
const deps = Object.keys(dependencies).map((p) => `${p}@${dependencies[p]}`);
function pluralize(count, singular, plural) {
if (count === 1) return `${count} ${singular}`;
return `${count} ${plural}`;
}
@wosephjeber
wosephjeber / find_and_kill_process.sh
Last active July 2, 2023 08:02
Find and kill ruby process using specified port
# Puma occasionally doesn't kill all ruby processes when
# I shut down my local web server. Here are the steps
# to find and kill the processes.
# Find pid of process running on specified port
lsof -i :3000
# Kill process
sudo kill -9 [pid]
@wosephjeber
wosephjeber / get_all_regex_matches.ts
Created March 17, 2023 13:03
Get all regex matches in a string
function getAllRegexMatches(regex: RegExp, string: string) {
if (regex.constructor !== RegExp) {
throw new Error('not RegExp');
}
const matches = [];
let match = regex.exec(string);
if (regex.global) {
while (match) {