Knexjs upsert with update

import Knex from "knex";

const knex = Knex({
  client: 'pg',
  connection: process.env.DATABASE_URL,
});

const records = [
  { id: 2, login: "admin" },
];

async function upsert() {
  const result = await knex.raw(
      `? ON CONFLICT (id)
              DO UPDATE SET
              login = EXCLUDED.login,
              updated_at = CURRENT_TIMESTAMP
            RETURNING *;`,
      [knex("users").insert(records)],
    );
}
27 марта 2024, 09:55

Kill process by pattern

pkill -9 -f pattern
24 марта 2024, 10:31

PhpStorm licence key

???
5 марта 2024, 14:58

Fix postgres sequence after insert

Run the output of this sql command
SELECT 
    'SELECT SETVAL(' ||
       quote_literal(quote_ident(sequence_namespace.nspname) || '.' || quote_ident(class_sequence.relname)) ||
       ', COALESCE(MAX(' ||quote_ident(pg_attribute.attname)|| '), 1) ) FROM ' ||
       quote_ident(table_namespace.nspname)|| '.'||quote_ident(class_table.relname)|| ';'
FROM pg_depend 
    INNER JOIN pg_class AS class_sequence
        ON class_sequence.oid = pg_depend.objid 
            AND class_sequence.relkind = 'S'
    INNER JOIN pg_class AS class_table
        ON class_table.oid = pg_depend.refobjid
    INNER JOIN pg_attribute 
        ON pg_attribute.attrelid = class_table.oid
            AND pg_depend.refobjsubid = pg_attribute.attnum
    INNER JOIN pg_namespace as table_namespace
        ON table_namespace.oid = class_table.relnamespace
    INNER JOIN pg_namespace AS sequence_namespace
        ON sequence_namespace.oid = class_sequence.relnamespace
ORDER BY sequence_namespace.nspname, class_sequence.relname;
Alternatives List of all sequence
SELECT c.relname FROM pg_class c WHERE c.relkind = 'S' order BY c.relname;
Fix sequence (if it's named id)
SELECT 
concat('SELECT SETVAL(''public.', c.relname,''', COALESCE(MAX(id), 1) ) FROM public.', replace(c.relname, '_id_seq', ''), ';') FROM pg_class c WHERE c.relkind = 'S' order BY c.relname;
4 марта 2024, 17:26

Install nodejs 20 on ubuntu

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
9 февраля 2024, 12:52

Copy files from/to docker containers

docker cp <source_path>  <container_id>:<destination_path>
docker cp <container_id>:<source_path> <destination_path>
8 февраля 2024, 21:23

Generate random string with openssl

 openssl rand -base64 32
5 февраля 2024, 20:29

Proxy and cache external resource with nginx

proxy_cache_path /home/nginx levels=1:2 keys_zone=CACHE:50m max_size=12G inactive=30d;

server {
	server_name example.com;

	location / {
		proxy_cache_key "$uri";
		proxy_pass https://$path$is_args$args;
		resolver 8.8.8.8;
		proxy_cache            CACHE;
		access_log      off;
		expires max;
		add_header X-Proxy-Cache $upstream_cache_status;
		proxy_set_header Referer "";
		proxy_cache_valid 200 30d;
		proxy_cache_valid 403 10m;
		proxy_cache_valid 404 10m;
		
		proxy_ssl_server_name on;
                proxy_ssl_name $proxy_host;
	}

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
15 декабря 2023, 12:59

Create .htpasswd file

sudo sh -c "echo -n 'sammy:' >> /etc/nginx/.htpasswd"
sudo sh -c "openssl passwd -apr1 >> /etc/nginx/.htpasswd"
20 июня 2023, 01:02

List all services on ubuntu

systemctl list-units --type service
4 апреля 2023, 08:42

Docker install on ubuntu 20.04

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get --assume-yes install docker-ce docker-ce-cli containerd.io
16 января 2023, 11:15

GPG error: https://download.docker.com/linux/ubuntu focal InRelease: The following signatures couldn't be verified because the public key is not available

sudo curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | sudo gpg --dearmor --yes -o /usr/share/keyrings/docker-archive-keyring.gpg
sudo chmod a+r /usr/share/keyrings/docker-archive-keyring.gpg
31 августа 2022, 18:34

AWS resize fs

aws
df -hT
lsblk
growpart /dev/xvda 1
resize2fs /dev/xvda1
31 августа 2022, 11:33

Update gitlab to specific version

apt install gitlab-ee=14.10.3-ee.0
4 июня 2022, 13:09

Install openvpn server on ubuntu

wget https://git.io/vpn -O openvpn-install.sh
sudo bash openvpn-install.sh
19 апреля 2022, 09:59

Gitlab invalid: EXPKEYSIG

curl -L https://packages.gitlab.com/gitlab/gitlab-ee/gpgkey | sudo apt-key add -
26 марта 2022, 02:55

Enable file public url access on AMAZON S3

s3
Write in bucket policy
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::my_bucket_name/*"
            ]
        }
    ]
}
22 марта 2022, 16:01

Install vue app with npx

npx @vue/cli create --default my-vue-app
15 марта 2022, 16:28

Docker removes volumes

docker-compose down -v
26 января 2022, 22:22

Copy schema table in postgres

CREATE TABLE new_table_name (LIKE old_table_name INCLUDING ALL);
2 ноября 2021, 11:33