Skip to content

Instantly share code, notes, and snippets.

View amkisko's full-sized avatar
😺
Hymyillen suora selkä!

Andrei Makarov amkisko

😺
Hymyillen suora selkä!
View GitHub Profile
@amkisko
amkisko / debug_helper.rb
Last active June 1, 2025 14:16
Amazing debugging for grape-ruby, rspec test examples, show SQL traces, show stack calls and all exceptions in stack, helper to find out which spec file stuck, store logs to files
require "amazing_print"
class Debug
attr_reader :messages, :before_gc_stats, :after_gc_stats, :time_report, :mem_report
def initialize(with_sql: false, with_stack: false, store_file: false, console_print: false, output_path: nil, context: nil, file_prefix: nil, messages: [])
@with_sql = with_sql
@with_stack = with_stack
@console_print = console_print
@context = context
@messages = messages
@amkisko
amkisko / debug.rb
Created April 4, 2025 11:03
Amazing Rails debug with TracePoint and sql.active_record notifications
require "amazing_print"
class Debug
def self.trace(with_sql: false)
if with_sql
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |event|
payload = event.payload[:sql]
next if payload.match?(/^(SELECT|SET|SHOW|BEGIN|COMMIT|ROLLBACK|RELEASE|SAVEPOINT)/)
# next if payload.include?("audits")
event.payload[:type_casted_binds].each_with_index do |bind, index|
@amkisko
amkisko / example_spec.rb
Created March 28, 2025 13:08
ActiveRecord SQL logger helper, shows only INSERT/UPDATE/DELETE
it "does something" do
log_sql do
request
expect(create_call).to_not raise_exception
end
end
@amkisko
amkisko / api_logging_spec.rb
Last active March 12, 2025 10:37
Customizable logger for grape ruby
require "rails_helper"
require "stringio"
require "json"
RSpec.describe API do
include Rack::Test::Methods
def app
API
end
@amkisko
amkisko / .pryrc
Created March 3, 2025 10:30
Remote Rails console user tracking & history management, console prompt to show commits-diff and environment
require "amazing_print"
AmazingPrint.pry!
app_id =
ENV["APP_ID"].presence ||
Rails.application.class.name.deconstantize.parameterize
app_env = ENV["ENVIRONMENT"].presence || Rails.env
commit_sha = ENV["GIT_COMMIT_SHA"]&.slice(0, 7)
commit_sha = `git rev-parse --short HEAD`.strip unless Rails.env.production?
@amkisko
amkisko / solid_queue_alive.rb
Last active May 23, 2025 10:06
Rails solid_queue / solid queue alive server livenessProbe readinessProbe health check kubernetes helper
class SolidQueueAliveServer
def initialize(host: nil, port: nil, engine: nil, logger: nil)
@host = host || "0.0.0.0"
@port = port || 7433
@engine = engine || "puma"
@logger = logger || Rails.logger
end
def run!
require "rackup"
@amkisko
amkisko / database_token.rb
Last active January 14, 2025 19:10
Azure Container App Managed Identity token authentication for Rails PostgreSQL adapter
require "net/http"
require "json"
module AzureTokenAuthentication
module Config
# NOTE: https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity#rest-endpoint-reference
class << self
# endpoint example: "http://localhost:42356/msi/token"
def endpoint = ENV.fetch("IDENTITY_ENDPOINT")
#!/usr/bin/env bash
if [ $# -ne 1 ]; then
echo "Usage: $0 YYYY-MM-DD"
exit 1
fi
TARGET_DATE=$1
if ! [[ $TARGET_DATE =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
@amkisko
amkisko / deploy-heroku.sh
Created December 18, 2024 15:43
Heroku deploy script for multi-stage docker containers with Rails
#!/bin/bash
app_name="my-heroku-app"
environment="production"
db_migrate_container="migrate"
app_containers="web worker"
app_url="https://amkisko.github.io"
github_org="amkisko"
github_repo="my-app"
@amkisko
amkisko / diff_coverage_reporter.rb
Last active December 11, 2024 07:45
rspec helper for reporting missing test coverage for changed lines according to git diff with selected branch and simplecov json resultset
class DiffCoverageReporter
attr_reader :coverage_file, :default_branch
def initialize(file_path: "coverage/.resultset.json", default_branch: "master")
@coverage_file = Rails.root.join(file_path)
@default_branch = default_branch
end
def coverage_report
JSON.parse(coverage_file.read).dig("RSpec", "coverage")
end