Randomise Ticket ID

@SimonW @Angel Thanks for your help ! :smiley:

I’ve created a bash script that runs automatically every day at 00:01 via cron until such a function is implemented (maybe one day) natively in RT :

#!/bin/bash

# Define log file
LOG_FILE="/var/log/rtir/update_sequence.log"

# Create or empty log file
> $LOG_FILE

# Message logging function
log() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE
}

log "Script started."

# Retrieve and format the current date
formatted_date=$(date +%y%m%d000)
log "Formatted date: $formatted_date"

# Define database variables
DB_NAME="rt"
TABLE_NAME="tickets"
COLUMN_NAME="id"
SEQUENCE_NAME="${TABLE_NAME}_${COLUMN_NAME}_seq"

cd /home/postgres-home

# Update sequence
log "Updating sequence $SEQUENCE_NAME to start with $formatted_date..."
if sudo -u postgres psql -d $DB_NAME -c "ALTER SEQUENCE $SEQUENCE_NAME RESTART WITH $formatted_date;" >> $LOG_FILE 2>&1; then
    log "Sequence updated successfully."
else
    log "Error updating sequence."
    exit 1
fi

# Check the update
log "Verifying sequence update..."
if sudo -u postgres psql -d $DB_NAME -c "SELECT nextval('$SEQUENCE_NAME');" >> $LOG_FILE 2>&1; then
    log "Sequence verification successful."
else
    log "Error verifying sequence."
    exit 1
fi

log "Script finished."
1 Like