<commit_before>#!/bin/zsh

__pg-check() {
  if [ "$#" = 2 ]; then
    echo "Usage: pg $1 <db_name>"
    return 1
  fi
  return 0
}

__pg-check2() {
  if [ "$#" = 3 ]; then
    echo "Usage: pg $1 <origin> <target>"
    return 1
  fi
  return 0
}

_pg-ls() {
  psql postgres -Atq -c "select d.datname from pg_catalog.pg_database d;"
}

_pg-kill-connections() {
  local db_name="$*"
  if __pg-check "kill-connections" "$db_name"; then
    psql postgres > /dev/null <<EOF
    SELECT pg_terminate_backend(pid)
    FROM pg_stat_activity
    WHERE pid <> pg_backend_pid()
    AND datname='$db_name';
EOF
  fi
}

_pg-create() {
  local db_name="$*"
  __pg-check "create" "$db_name" && \
    createdb "$db_name"
}

_pg-drop() {
  local db_name="$*"
  if __pg-check "drop" "$db_name"; then
    _pg-kill-connections "$db_name"
    dropdb --if-exists "$db_name"
  fi
}

_pg-cp() {
  local origin="$1"
  local target="$2"
  if __pg-check2 "cp" "$origin" "$target"; then
    _pg-kill-connections "$origin"
    psql postgres &> /dev/null <<EOF
      CREATE DATABASE "$target" WITH TEMPLATE "$origin";
EOF
  fi
}

_pg-mv() {
  local origin="$1"
  local target="$2"
  if __pg-check2 "mv" "$origin" "$target"; then
    _pg-kill-connections "$origin"
    psql postgres &> /dev/null <<EOF
      ALTER DATABASE "$origin" RENAME TO "$target";
EOF
  fi
}

_pg-dump-table() {
  local db_name="$1"
  local table_name="$2"
  if [ "$#" != 2 ]; then
    echo "Usage: pg dump-table <db_name> <table_name>"
    pg_dump --table="$table_name" --data-only --column-inserts "$db_name"
  fi
}

pg() {
  local command="$1"; shift
  case "$command" in
    ls)
      _pg-ls "$@"
      ;;
    kill-connections)
      _pg-kill-connections "$@"
      ;;
    create)
      _pg-create "$@"
      ;;
    drop)
      _pg-drop "$@"<commit_msg>i probably need more sleep.<commit_after>#!/bin/zsh

__pg-check() {
  if [ "$#" != 2 ]; then
    echo "Usage: pg $1 <db_name>"
    return 1
  fi
}

__pg-check2() {
  if [ "$#" != 3 ]; then
    echo "Usage: pg $1 <origin> <target>"
    return 1
  fi
}

_pg-ls() {
  psql postgres -Atq -c "select d.datname from pg_catalog.pg_database d;"
}

_pg-kill-connections() {
  local db_name="$*"
  if __pg-check "kill-connections" "$db_name"; then
    psql postgres > /dev/null <<EOF
    SELECT pg_terminate_backend(pid)
    FROM pg_stat_activity
    WHERE pid <> pg_backend_pid()
    AND datname='$db_name';
EOF
  fi
}

_pg-create() {
  local db_name="$*"
  __pg-check "create" "$db_name" && \
    createdb "$db_name"
}

_pg-drop() {
  local db_name="$*"
  if __pg-check "drop" "$db_name"; then
    _pg-kill-connections "$db_name"
    dropdb --if-exists "$db_name"
  fi
}

_pg-cp() {
  local origin="$1"
  local target="$2"
  if __pg-check2 "cp" "$origin" "$target"; then
    _pg-kill-connections "$origin"
    psql postgres &> /dev/null <<EOF
      CREATE DATABASE "$target" WITH TEMPLATE "$origin";
EOF
  fi
}

_pg-mv() {
  local origin="$1"
  local target="$2"
  if __pg-check2 "mv" "$origin" "$target"; then
    _pg-kill-connections "$origin"
    psql postgres &> /dev/null <<EOF
      ALTER DATABASE "$origin" RENAME TO "$target";
EOF
  fi
}

_pg-dump-table() {
  local db_name="$1"
  local table_name="$2"
  if [ "$#" != 2 ]; then
    echo "Usage: pg dump-table <db_name> <table_name>"
  else
    pg_dump --table="$table_name" --data-only --column-inserts "$db_name"
  fi
}

pg() {
  local command="$1"; shift
  case "$command" in
    ls)
      _pg-ls "$@"
      ;;
    kill-connections)
      _pg-kill-connections "$@"
      ;;
    create)
      _pg-create "$@"
      ;;
    drop)
      _pg-drop "$@"
{'fraction_non_alphanumeric': 0.1760778859527121, 'fraction_numerical': 0.010013908205841447, 'mean_word_length': 2.7654450261780106, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6138204', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1391, 'n_words': 341}