Skip to content

Instantly share code, notes, and snippets.

@casperbrike
casperbrike / cleanup.sh
Created November 13, 2023 21:32
Cleanup schema.rb
git restore db/schema.rb
pg_dump -U username -d database_name -a -F t -f data.dump
rails db:drop
rails db:create
rails db:schema:load
rails db:migrate
pg_restore -U username -d database_name data.dump
@casperbrike
casperbrike / table_rows_count.sql
Created September 28, 2023 12:13
Show list of tables and their rows count (PostgreSQL)
WITH tbl AS
(SELECT table_schema,
TABLE_NAME
FROM information_schema.tables
WHERE TABLE_NAME not like 'pg_%'
AND table_schema in ('public'))
SELECT table_schema,
TABLE_NAME,
(xpath('/row/c/text()', query_to_xml(format('select count(*) as c from %I.%I', table_schema, TABLE_NAME), FALSE, TRUE, '')))[1]::text::int AS rows_n
FROM tbl
@casperbrike
casperbrike / controller.rb
Created May 14, 2023 18:30
Get Rails controller and action from route path
Rails.application.routes.recognize_path '/meeting_packs/1/view'
@casperbrike
casperbrike / all_factories.rb
Last active March 13, 2023 19:48
List all FactoryBot factories
FactoryBot.factories.instance_variable_get(:@items).keys.sort
@casperbrike
casperbrike / how_to.rb
Created January 31, 2021 09:10
How to find Books that belong to desired Categories with Rails?
# The logic here is dishonestly stolen from Gutentag gem:
# https://github.com/pat/gutentag/blob/0dbcd1dbcd4de8930e589cf964498abe46127a98/lib/gutentag/tagged_with/query.rb#L11
# Models definitions
class Book < ApplicationRecord
has_many :category_mappings, as: :categorizable, dependent: :destroy
has_many :categories, through: :category_mappings
end
@casperbrike
casperbrike / puma.service
Last active December 11, 2023 10:32
Puma service for systemd
# username
# appname
# gemset
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Type=forking