Skip to content

Instantly share code, notes, and snippets.

View docteurklein's full-sized avatar

Florian Klein docteurklein

View GitHub Profile
@docteurklein
docteurklein / traverse.sql
Last active September 20, 2021 07:56
postgres query to generate links based on relations
;
drop function if exists explore(text);
create function explore(sql text) returns setof record
language 'plpgsql' as $$
begin
return query execute format($sql$
with node (tableoid, row) as (
%s
),
@docteurklein
docteurklein / dynamic.sql
Created January 18, 2021 16:00
postgres dynamic sql
drop function if exists exec(text, text[]);
create function exec(sql text, params text[] default '{}') returns void
language 'plpgsql' as $$
begin
execute format(sql, variadic params);
end
$$;
-- select exec('select 1');
drop function if exists query(text, text[]);
@docteurklein
docteurklein / test.sql
Created September 21, 2020 10:14
AGE demo
load 'age';
SET search_path = ag_catalog, "$user", public;
create table if not exists history (year, event) as values (1996, 'postgresql'), (2016, 'agensgraph');
select drop_graph('g', true);
select create_graph('g');
select * from cypher('g', $$
create (:dev {name: 'someone', year: 2015})

declarative DDL

what?

Define your database schema in a declarative way, by providing the wanted state, and this tool will calculate a diff to reconcile it with reality.

how?

Let's imagine a scenario:

@docteurklein
docteurklein / index.md
Last active May 7, 2020 10:15
Work on a cabal dependency to contribute upstream

Work on a cabal dependency to contribute upstream

The haskell dependency management can be quite intimidating at first, but after a while you get used to it and realize it's very similar to rust's cargo (so many things are similar between rust and haskell, by the way).

One of the things I found particularly easy (once you know how) is to edit a dependency locally to work on it.

So imagine you have a cabal project with a dependency:

use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
$connFrom = DriverManager::getConnection([
'url' => "mysql://root:[email protected]/test1",
], new Configuration);
$connTo = DriverManager::getConnection([
@docteurklein
docteurklein / 0_setup.sql
Last active January 14, 2020 12:34
psql -v ON_ERROR_STOP=1 -c begin $(find . -name '*.sql' -printf ' -f %f\n' | sort -V | xargs) -c commit
create table if not exists migration (
id bigint primary key,
at timestamptz not null default clock_timestamp()
);
@docteurklein
docteurklein / migrate.sql
Created January 13, 2020 21:31
postgres-only transactional migration script
begin;
create table if not exists migration (id bigint primary key, at timestamptz not null default clock_timestamp());
drop function if exists migrate;
create function migrate() returns bigint language plpgsql as $$
declare current_migration bigint;
begin
select coalesce((select id from migration order by id desc limit 1), 0) into current_migration;
create function es.project(event es.events) returns void
language plpgsql as $$
begin
case event.type
when 'user_registered' then
insert into es.active_users
(user_id , name , sha256 , updated_at) values
(event.aggregate_id , event.payload->>'name' , event.payload->>'sha256' , event.added_at);
when 'user_changed_password' then
update es.active_users set
error[E0599]: no method named `map` found for type `postgres::notification::BlockingIter<'_>` in the current scope
  --> src/main.rs:24:19
   |
24 |     notifications.map(|notification| {
   |                   ^^^
   |
   = note: the method `map` exists but the following trait bounds were not satisfied:
           `&mut postgres::notification::BlockingIter<'_> : fallible_iterator::FallibleIterator`
 `&amp;mut postgres::notification::BlockingIter&lt;'_&gt; : std::iter::Iterator`