Skip to content

Instantly share code, notes, and snippets.

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

Andrei Makarov amkisko

😺
Hymyillen suora selkä!
  • Kisko Labs
  • Helsinki, Finland
View GitHub Profile
@amkisko
amkisko / db.rake
Last active May 3, 2024 07:24
Rails database migrations cleaner/archiver/squasher
namespace :db do
namespace :migrate do
task archive: :environment do
Rake::Task["db:migrate"].invoke
expires_in = 90.days
files = Dir.glob(Rails.root.join("db/migrate/*").to_s)
files.each do |path|
name = File.basename(path)
timestamp, _ = name.split("_", 2)
@amkisko
amkisko / metric_record.rb
Last active March 27, 2024 12:46
Metric record helper
module MetricRecord
extend ActiveSupport::Concern
included do
validates :date, :group, :period, presence: true
validates :date, uniqueness: {scope: %i[group period]}
scope :for_date,
->(date) do
where(date: date_for_period(date, periods.keys.first))
@amkisko
amkisko / create_table_migration.rb
Last active April 30, 2024 13:06
pg_party range partitioning helper concern for ActiveRecord and PostgreSQL
class CreateSystemMetrics < ActiveRecord::Migration[7.1]
def up
safety_assured do
create_range_partition :system_metrics, partition_key: -> { "date" } do |t|
t.decimal :value, null: false, default: 0
t.integer :group, null: false
t.integer :period, null: false
t.date :date, null: false
@amkisko
amkisko / abstract.md
Last active April 16, 2024 09:15
ActiveAdmin v4 tailwind configuration
$ rails new . -n ActiveAdminDemo -c tailwind -a propshaft --skip-test --skip-system-test

$ rails g active_admin:install --skip-users
$ rails tailwindcss:install
$ rails generate active_admin:assets
$ cat tailwind-active_admin.config.js | sed 's/require(`@activeadmin\/activeadmin\/plugin`)/require(`${activeAdminPath}\/plugin.js`)/g' > config/tailwind-active_admin.config.js
$ rm tailwind-active_admin.config.js
$ bundle binstub tailwindcss-rails
$ rails generate active_admin:views
@amkisko
amkisko / aws_rds_postgresql_pg_hba_connection_error.md
Last active February 17, 2024 09:03
AWS RDS PostgreSQL 14, 15, 16 version pg_hba.conf error during connection

The error

2024-01-01T01:02:03 [METADATA_MANAGE ]E: RetCode: SQL_ERROR SqlState: 08001 NativeError: 101 Message: [unixODBC]FATAL: no pg_hba.conf entry for host "172.27.32.78", user "app_production", database "app_production", no encryption [1022502] (ar_odbc_conn.c:579)

Affected drivers

Any driver which does not support SSL and/or password encryption. E.g. PowerBI ODBC or even AWS DMS might be the affected software.

Analysis

@amkisko
amkisko / importmaps.rb
Created February 6, 2024 11:37
Rails custom importmaps snippet
Rails::Application.send(:attr_accessor, :importmaps)
module Importmap::CustomImportmapTagsHelper
def javascript_custom_importmap_tags(name, entry_point: "application")
importmap = Rails.application.importmaps[name.to_sym]
return unless importmap
javascript_importmap_tags(entry_point, importmap: importmap)
end
end
@amkisko
amkisko / README.md
Last active January 9, 2024 12:39
Stimulus single application typescript decorator for controllers

stimulus.js typescript decorator

Let's you automate registering of new controllers for Stimulus.js by using TypeScript decorator @stimulusController. Good alternative for stimulus-vite-helpers.

Features

  • developer can set custom name for the controller
  • developer can set custom application for every controller
  • by default it uses window.StimulusApplication, also initialises one
@amkisko
amkisko / application.html.erb
Last active October 16, 2023 15:38
ViewAsset Rails assets autoloading for views by applying single convention for assets paths
<html>
<head>
<%= vite_client_tag %>
<%= view_asset_render_head %>
</head>
<body>
<%= yield %>
<%= view_asset_render_body %>
<% if Rails.env.development? %>
<%= view_asset_render_debug %>
@amkisko
amkisko / sidekiq_alive.rb
Last active October 15, 2023 08:37
sidekiq_alive simple alternative implementation (gem sidekiq_status_monitor)
# PATH: config/initializers/sidekiq_alive.rb
# AUTHOR: Andrei Makarov (github.com/amkisko)
# NOTE: now available as gem sidekiq_status_monitor (https://rubygems.org/gems/sidekiq_status_monitor)
class SidekiqAliveServer
attr_accessor :workers_size_threshold,
:process_set_size_threshold,
:queues_size_threshold,
:queue_latency_threshold,
@amkisko
amkisko / pre-commit
Last active October 15, 2023 08:38
husky pre-commit script for lint-staged prompt and compatibility with custom non-console git clients
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
if [ -t 1 ]; then
exec < /dev/tty
echo "\033[31m$1\033[0m"
read -p "Do you want to run lint-staged? [y/n] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then