<commit_before>#!/bin/bash

FIRST_START_DONE="/etc/docker-mariadb-first-start-done"

# container first start
if [ ! -e "$FIRST_START_DONE" ]; then

  # fix permissions and ownership of /var/lib/mysql
  chown -R mysql:mysql /var/lib/mysql
  chmod 700 /var/lib/mysql

  # config sql queries
  TEMP_FILE='/tmp/mysql-start.sql'

  # The password for '<PASSWORD>'@'localhost' is auto generated.
  # The database inside of DATA_DIR may not have been generated with this password.
  # So, we need to set this for our database to be portable.
  # https://github.com/Painted-Fox/docker-mariadb/blob/master/scripts/first_run.sh
  DB_MAINT_PASS=$(cat /etc/mysql/debian.cnf | grep -m 1 "password\s*=\s*"| sed 's/^password\s*=\s*//')

  # database is uninitialized
  if [ -z "$(ls -A /var/lib/mysql)" ]; then

    # initializes the MySQL data directory and creates the system tables that it contains
    mysql_install_db --datadir=/var/lib/mysql

    # start MariaDB
    service mysql start || true

    # drop all user and test database
    cat > "$TEMP_FILE" <<-EOSQL
        DELETE FROM mysql.user ;
        DROP DATABASE IF EXISTS test ;
EOSQL

    # add root user on specified networks
    IFS=', ' read -a networks <<< "$ROOT_ALLOWED_NETWORKS"
    for network in "${networks[@]}"
    do
      echo "CREATE USER '$ROOT_USER'@'$network' IDENTIFIED BY '$ROOT_PWD' ;" >> "$TEMP_FILE"
      echo "GRANT ALL ON *.* TO '$ROOT_USER'@'$network' WITH GRANT OPTION ;" >> "$TEMP_FILE"
    done

    echo "CREATE USER 'debian-sys-maint'@'localhost' IDENTIFIED BY '$DB_MAINT_PASS' ;" >> "$TEMP_FILE"
    echo "GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '$DB_MAINT_PASS' ;" >> "$TEMP_FILE"
    
    # flush privileges
    echo 'FLUSH PRIVILEGES ;' >> "$TEMP_FILE"

    # execute config queries
    mysql -u root < $TEMP_FILE

    # prevent socket error on stop
    sleep 1

    # Stop MariaDB
    service mysql stop

  # database is initialized
  else

    # start MariaDB
    service mysql start || true

    # drop all user and test database
    cat > "$TEMP_FILE" <<-EOSQL
        DELETE FROM mysql.user where user = 'debian-sys-maint' ;
        FLUSH PRIVILEGES ;
        CREATE USER 'debian-sys-maint'@'localhost' IDENTIFIED BY '$DB_MAINT_PASS' ;
        GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '$DB_MAINT_PASS' ;
        FLUSH PRIVILEGES ;
EOSQL

    # execute config queries
    mysql -u $ROOT_USER -p$ROOT_PWD < $TEMP_FILE

    # prevent socket error on stop
    sleep 1<commit_msg>optimise sql queries<commit_after>#!/bin/bash

FIRST_START_DONE="/etc/docker-mariadb-first-start-done"

# container first start
if [ ! -e "$FIRST_START_DONE" ]; then

  # fix permissions and ownership of /var/lib/mysql
  chown -R mysql:mysql /var/lib/mysql
  chmod 700 /var/lib/mysql

  # config sql queries
  TEMP_FILE='/tmp/mysql-start.sql'

  # The password for 'debian-sys-maint'@'localhost' is auto generated.
  # The database inside of DATA_DIR may not have been generated with this password.
  # So, we need to set this for our database to be portable.
  # https://github.com/Painted-Fox/docker-mariadb/blob/master/scripts/first_run.sh
  DB_MAINT_PASS=$(cat /etc/mysql/debian.cnf | grep -m 1 "password\s*=\s*"| sed 's/^password\s*=\s*//')

  # database is uninitialized
  if [ -z "$(ls -A /var/lib/mysql)" ]; then

    # initializes the MySQL data directory and creates the system tables that it contains
    mysql_install_db --datadir=/var/lib/mysql

    # start MariaDB
    service mysql start || true

    # drop all user and test database
    cat > "$TEMP_FILE" <<-EOSQL
        DELETE FROM mysql.user ;
        DROP DATABASE IF EXISTS test ;
EOSQL

    # add root user on specified networks
    IFS=', ' read -a networks <<< "$ROOT_ALLOWED_NETWORKS"
    for network in "${networks[@]}"
    do
      echo "GRANT ALL ON *.* TO '$ROOT_USER'@'$network' IDENTIFIED BY '$ROOT_PWD' WITH GRANT OPTION ;" >> "$TEMP_FILE"
    done

    echo "GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '$DB_MAINT_PASS' ;" >> "$TEMP_FILE"
    
    # flush privileges
    echo 'FLUSH PRIVILEGES ;' >> "$TEMP_FILE"

    # execute config queries
    mysql -u root < $TEMP_FILE

    # prevent socket error on stop
    sleep 1

    # Stop MariaDB
    service mysql stop

  # database is initialized
  else

    # start MariaDB
    service mysql start || true

    # drop all user and test database
    cat > "$TEMP_FILE" <<-EOSQL
        DELETE FROM mysql.user where user = 'debian-sys-maint' ;
        FLUSH PRIVILEGES ;
        GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '$DB_MAINT_PASS' ;
        FLUSH PRIVILEGES ;
EOSQL

    # execute config queries
    mysql -u $ROOT_USER -p$ROOT_PWD < $TEMP_FILE

    # prevent socket error on stop
    sleep 1
{'fraction_non_alphanumeric': 0.1154562383612663, 'fraction_numerical': 0.002482929857231533, 'mean_word_length': 2.991742361684558, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 24, 'https://': 2, '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': '3819148', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1609, 'n_words': 490}