Skip to content

Instantly share code, notes, and snippets.

View pioz's full-sized avatar
🧙‍♂️
[object Object]

Enrico pioz

🧙‍♂️
[object Object]
View GitHub Profile
@pioz
pioz / amazon.rb
Last active September 26, 2024 14:28
Ruby script to get Amazon expenses
require 'date'
months_mapping = {
'gennaio' => '01',
'febbraio' => '02',
'marzo' => '03',
'aprile' => '04',
'maggio' => '05',
'giugno' => '06',
'luglio' => '07',
@pioz
pioz / caffe.py
Last active February 9, 2024 15:50
Run caffeinate from Mac OS X menu bar
import rumps
import subprocess
class CaffeinateApp(rumps.App):
def __init__(self):
super(CaffeinateApp, self).__init__("Caffeinate")
self.icon = "inactive_icon.png"
self.caffeinate_process = None
self.menu = ["Run Caffeinate"]
@pioz
pioz / tenants.rb
Last active April 23, 2024 07:49
Tenants
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'activerecord'
gem 'mysql2'
end
require 'active_record'
require 'logger'
require 'net/http'
PUSHOVER_USER_KEY = 'xxx'.freeze
PUSHOVER_API_TOKEN = 'xxx'.freeze
def send_notification(message)
uri = URI.parse('https://api.pushover.net/1/messages.json')
request = Net::HTTP::Post.new(uri)
request.set_form_data(
@pioz
pioz / copy_migration_version.py
Last active September 11, 2024 09:33
Sublime Text plugin to copy Rails migration version
import sublime
import sublime_plugin
import os
import json
class CopyMigrationVersionCommand(sublime_plugin.WindowCommand):
def run(self):
file_name = self.window.active_view().file_name()
if file_name:
version = os.path.basename(file_name).split('_')[0]

Implementing Advisory Locks in SQLite3

In my Rails model, I initially had a function to handle advisory locks in PostgreSQL. Here's the code, which works as expected:

# Postgresql version
def with_advisory_lock
  self.class.connection.execute("SELECT pg_advisory_lock(#{self.id})")
  yield