gid, uid in Dockerfile

Tagged gid, uid, dockerfile  Languages bash
# Debian
RUN addgroup --gid 10000 app && adduser --gid 10000 --uid 10001 app && chown -R app /app/ && chgrp -R app /app/ && chmod -R 740 /app/

# Alpine
RUN addgroup --gid 10000 app && adduser -u 10001 app -D -G app && chown -R app /app/ && chgrp -R app /app/ && chmod -R 740 /app/

Add the user to the host system too.

ActiveRecord after_create/after_save and associations

Tagged rails, after_save, after_create, association  Languages ruby

The order of ActiveRecord callbacks matter when for example they need to access associations (e.g. has_many).

For example, the calculate_doors will be called before the associations are saved to the database if you use car.doors.build(name: :backdoor):

class Car < ApplicationRecord
  after_create :calculate_doors
  has_many :doors

This can lead to subtle errors.

Changing the order will fix the issue:

class Car < ApplicationRecord
  has_many :doors
  after_create :create_doors

Tested with Rails 6.1. Other versions might work differently.

See https://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html for details:

Placing your callbacks after associations is usually a good practice.

Preventing division by zero in PostgreSQL

Tagged postgres, nullif, division, zero  Languages sql
localhost=# select 1 / 0;
ERROR:  22012: division by zero
LOCATION:  int4div, int.c:824

Hmmm:

localhost=# select 1 / NULL;
┌──────────┐
│ ?column? │
├──────────┤
│        ¤ │
└──────────┘
(1 row)

Time: 7.067 ms

Ergo:

localhost=# select 1 / NULLIF(0, 0);
┌──────────┐
│ ?column? │
├──────────┤
│        ¤ │
└──────────┘
(1 row)

Time: 0.303 ms

Postgrest docker-compose.yml

Tagged docker, postgrest  Languages yaml
version: "3"
services:
  postgrest:
    container_name: postgrest
    image: postgrest/postgrest:v7.0.1
    # entrypoint: ''
    # command: ls -l
    ports:
      # Forward host port 8080 to postgrest
      - "8080:3000"
    environment:
      PGRST_DB_URI: postgres://user:[email protected]:5432/db_name
      # The schema that Postgrest exposes
      PGRST_DB_SCHEMA: public
      # The role that is used to perform unauthenticated queries
      PGRST_DB_ANON_ROLE: app_user
      PGRST_SERVER_PROXY_URI: "http://127.0.0.1:3001"

  swagger:
    image: swaggerapi/swagger-ui
    ports:
      # Forward host port 8081 to swagger
      - "8081:8080"
    expose:
      - "8080"
    environment:
      # Tell Swagger to document the postgrest API
      API_URL: http://localhost:8080/

Exactly-once delivery with RabbitMQ

Tagged reconciliation, rabbitmq, messaging, exactly-once, amqp  Languages 

Use late acknowledgment and idempotency to achieve fake exactly-once delivery with RabbitMQ.

Late acknowledgment = acknowledge the message after the database transaction has been committed.

Idempotency = don’t process the same message twice, or ensure the effect is the same when processing the same message multiple times

Reconciliation step = if you need 100% reliability create a process (manual or automatic) that checks that all work is done, if not resend the message