Skip to content

Instantly share code, notes, and snippets.

@mlt
mlt / rails_sqitch_dumper.rb
Last active January 23, 2021 06:15
Mixin for ActiveRecord/Rails to dump Sqitch metadata along with DB schema/structure
# Chain sqitch metadata dump to structure.sql dumping
#
# Structure dump does not include any data
# whilst Sqitch expect its metadata at all times.
# This allows to cache DB schema in a single sql dump to speed up tests
# while allowing continuous deployment of new changes with sqitch.
#
# sqitch deploy ...
# bundle exec rails db:structure:dump RAILS_ENV=test
# bundle exec rails test
@mlt
mlt / pg-split-dump.py
Last active December 10, 2020 23:21
Slightly modified from http://furius.ca/pubcode/pub/conf/bin/pg-split-dump to get type/schema/name.sql structure with all overloaded function definitions in same file
#!/usr/bin/env python
"""
Split a PostgreSQL plain dump file into files for each of its objects.
This is used to compare two SQL database contents: both contents are dumped to a
plain text file, and they are then processed through this script which generates
a file for each database object, which can then be compared using a directory
diff program.
"""
@mlt
mlt / lookup_table.rb
Last active July 2, 2020 05:22
A Rails mixin to make inquiries with lookup tables, e.g. when used in association
# Mix this into an AR class for lookup tables to perform an inquiry.
# For example, if Movie has Genre and genres table has id->genre mapping
# then one could use the_movie.genre.comedy? instead of the_movie.genre.genre == 'comedy'
module LookupTable
def self.included(base)
raise "#{name} can only be included into classes that inherit from ActiveRecord::Base, #{base.name} does not." unless base < ActiveRecord::Base
base.instance_variable_set :@lookup_column, base.name.demodulize.underscore
base.extend(ClassMethods)
end
@mlt
mlt / json.js
Created December 6, 2019 00:56
x-editable json input editing
/**
JSON editable input.
Internally value stored as {city: "Moscow", street: "Lenina", building: "15"}
Supported editors:
- https://github.com/json-editor/json-editor
- https://github.com/josdejong/jsoneditor
@class json
@extends abstractinput
@mlt
mlt / Update-AUPackages.md
Last active April 20, 2024 12:35
Update-AUPackages Report #powershell #chocolatey
@mlt
mlt / Update-Force-Test-0.md
Created December 19, 2018 23:45
Update Force Test Report #powershell #chocolatey
@mlt
mlt / RParseEval.c
Last active November 30, 2018 19:51
R_ParseVector won't report an error to embedder. Modified example from tests/Embedding/
#include "embeddedRCall.h"
#include <R_ext/Parse.h>
int
main(int argc, char *argv[])
{
SEXP e, tmp;
int hadError;
ParseStatus status;
@mlt
mlt / excel_column.sql
Last active February 5, 2023 18:40
Calculate MS Excel column name from its number in PostgreSQL
CREATE FUNCTION excel_column(col integer)
RETURNS text AS
$BODY$
WITH RECURSIVE t(n, out) AS (
SELECT col/26-(col%26=0)::int, chr((col-1)%26 + 65)
UNION ALL
SELECT n/26-(n%26=0)::int, chr((n-1)%26 + 65) || out FROM t
where n>0
)
SELECT out FROM t where n=0;
@mlt
mlt / some_controller.rb
Last active February 15, 2023 12:50
Stream PostgreSQL query with potentially huge result set as CSV using gzip compression in Rails and low memory overhead
headers['X-Accel-Buffering'] = 'no'
headers['Cache-Control'] = 'no-cache'
headers['Content-Type'] = 'text/csv; charset=utf-8'
headers['Content-Disposition'] = 'inline; filename="data.csv"'
headers['Content-Encoding'] = 'gzip'
sql = "select * from something;"
self.response_body = SqlToCsvStreamer.new(sql)
import os, sys, glob, arcpy, string, zipfile, time, urllib, json
import shutil, urllib2
from contextlib import closing
def printMessage(messageString):
#print messageString
arcpy.AddMessage(messageString)
def duration_human(seconds):
seconds = long(round(seconds))