Подробная шпаргалка по GORM (Go ORM) на примере блога с сущностями User, Post, Tag + доп. пример полиморфизма (Comment).
go get -u gorm.io/gorm
go get -u gorm.io/driver/postgres| CREATE EXTENSION IF NOT EXISTS "unaccent" | |
| CREATE OR REPLACE FUNCTION slugify("value" TEXT) | |
| RETURNS TEXT AS $$ | |
| -- removes accents (diacritic signs) from a given string -- | |
| WITH "unaccented" AS ( | |
| SELECT unaccent("value") AS "value" | |
| ), | |
| -- lowercases the string | |
| "lowercase" AS ( |
My largest Sidekiq application had a memory leak and I was able to find and fix it in just few hours spent on analyzing Ruby's heap. In this post I'll show my profiling setup.
As you might know Ruby 2.1 introduced a few great changes to ObjectSpace, so now it's much easier to find a line of code that is allocating too many objects. Here is great post explaining how it's working.
I was too lazy to set up some seeding and run it locally, so I checked that test suite passes when profiling is enabled and pushed debugging to production. Production environment also suited me better since my jobs data can't be fully random generated.
So, in order to profile your worker, add the sidekiq_profiler.rb (below) to your project
Adjust number of jobs you want your worker to process before you have heap dumped.
Run a sample worker: PROFILE=1 sidekiq -C config/sidekiq.yml and wait for jobs to be processed.
| require "active_record" | |
| ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | |
| ActiveRecord::Migration.class_eval do | |
| create_table(:records) do |t| | |
| t.string :column | |
| end | |
| end | |
| data = 50_000.times.map { |i| Hash[column: "Column #{i}"] } |
These are the steps I took to change a modest project from CoffeeScript to ES6.
My ~2000LoC project took me around 6 hours to port, but I sunk 3 hours into a stupid mistake, and 2 into figuring out these steps, so with these steps & warnings at your disposal already, you should be able to do bigger projects in considerably less time.
Automatic tools only take you so far, there will be some manual fixing. Sometimes the generated code clearly doesn't look like a human wrote it, and sometimes there are bugs.
In this last case, when porting software trips over a particular bit of CoffeeScript, comment this bit out, and try transpiling the file again. If this is successfull, you'll see the commented CoffeeScript inside the JS, and you can port that bit yourself.
Okay let's dive right in!
| # if you want to render flamegraphs | |
| gem "stackprof", require: false # required by flamegraph | |
| gem "flamegraph", require: false |
| module FactoryGirl | |
| module Doctor | |
| module FloatDuration | |
| refine Float do | |
| def duration | |
| t = self | |
| format("%02d:%02d.%03d", t / 60, t % 60, t.modulo(1) * 1000) | |
| end | |
| end | |
| end |
Иногда при работе с несколькими удалёнными репозиториями в git, может произойти страшное: git push --force в не тот remote и/или не в ту ветку.
Такое может случиться, например, если вы используете [Deis], в котором деплой запускается при git push нужного коммита в сборщик, когда при отладке деплоя после очередного git commit --amend по запарке вместо git push deis master --force делается просто git push --force. Упс.
Как результат, последние коммиты коллег безвозвратно потеряны, и вы чувствуете неотвратимость их ярости…
Но это git, а значит всё можно починить!