Skip to content

Instantly share code, notes, and snippets.

View chanmix51's full-sized avatar

Grégoire HUBERT chanmix51

View GitHub Profile
@chanmix51
chanmix51 / gist:9de65b6290253cbb0280
Created October 14, 2014 16:24
Creating an UTF-8 postgresql database template
update pg_catalog.pg_database set datistemplate = false where datname = 'template1';
drop database template1;
create database template1 template template0 encoding 'utf8' lc_collate 'fr_FR.utf8' lc_ctype 'fr_FR.utf8';
update pg_catalog.pg_database set datistemplate = true where datname = 'template1';
@chanmix51
chanmix51 / bad_chars.txt
Created June 4, 2014 09:20
Latin1 accentuated chars in UTF8
é é
è è
à Ã
ù ù
ç ç
î î
ô ô
@chanmix51
chanmix51 / deduplicate.sql
Created May 29, 2014 09:52
Remove duplicate rows from a table
with
dup_id as (select id, count(*) from school group by id having count(*) > 1),
del_dup as (delete from only school using dup_id where school.id = dup_id.id returning school.*)
insert into school select distinct on (id) del_dup.* from del_dup order by del_dup.id returning *;
@chanmix51
chanmix51 / restart_sequences.sql
Last active August 29, 2015 14:01
Restart sequences after a migration
create or replace function restart_sequence(seq_name varchar) returns void language plpgsql as $func$
declare
maxint bigint;
table_name varchar;
begin
table_name := left(seq_name, -7);
execute 'select max(id) + 1 from '||table_name into maxint;
if maxint is not null then
execute 'alter sequence '||seq_name||' restart with '||cast(maxint as text);
end if;
@chanmix51
chanmix51 / create_sandbox.sh
Last active August 29, 2015 13:56
Create a working Pomm & Silex sandbow in a minute
#!/bin/bash
echo "Postgresql setup";
echo -n "What is your Postgresql username [$USER]:> ";
read db_username;
echo -n "What is this user's password (empty if none) [] :> ";
read db_password;
echo -n "What is the server address (IP address or socket directory [localhost] :> ";
read db_host;
echo -n "What is the server port [5432] :> ";
@chanmix51
chanmix51 / statistics.sql
Created February 24, 2014 12:27
statistic over time ranges
WITH
timerange (ts) AS (SELECT generate_series(date_trunc('hour', min(collected_at)), date_trunc('hour', max(collected_at)) + '1 day'::interval, '1 hour'::interval) FROM statistic)
SELECT
ts,
count(stat),
stddev(stat),
avg(stat)
FROM
timerange t
LEFT JOIN statistic s ON s.collected_at <@ tsrange(t.ts, t.ts + '1 hour'::interval, '[)')
@chanmix51
chanmix51 / readonlyuser.sql
Created December 19, 2013 09:22
read only user in postgresql
CREATE USER backup_user WITH ENCRYPTED PASSWORD 'password';
GRANT CONNECT ON DATABASE production to backup_user;
\c production
GRANT USAGE ON SCHEMA public to backup_user; /*thanks Dominic!*/
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO backup_user;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO backup_user;
@chanmix51
chanmix51 / dropfew.js
Created November 25, 2013 15:59
how to remove a subset of a mongodb collection that only has certain fields.
db.collection.find({"changes.objectSid": { $exists: 1}, "changes.pwdLastSet": { $exists: 1 }}).forEach(function(doc) {
var other_found = false;
for (key in doc.changes) {
if (key != 'objectSid' && key != 'pwdLastSet') {
@chanmix51
chanmix51 / AddressType.php
Last active December 28, 2015 05:09
Using composite types with PgRow
<?php
namespace My\Database\Type;
use \Pomm\Type\Composite;
class Address extends Composite
{
public $place;
public $postal_code;
@chanmix51
chanmix51 / findAll.php
Last active December 28, 2015 00:59
Custom queries using Pomm 1.2
<?php // CustomerMap.php
// ...
public function findAllWithOrderCount()
{
return $this->queryWithOrderCount(new Where());
// SELECT ... WHERE true;
}