Skip to content

Instantly share code, notes, and snippets.

@davydmaker
Created April 22, 2026 02:25
Show Gist options
  • Select an option

  • Save davydmaker/6862e121d0b7ce7713b7d5dd30d654d3 to your computer and use it in GitHub Desktop.

Select an option

Save davydmaker/6862e121d0b7ce7713b7d5dd30d654d3 to your computer and use it in GitHub Desktop.
generate-sql-migration: timestamped SQL migration file generator

generate-sql-migration

Shell script that generates SQL migration files with a timestamp prefix and a formatted name based on input arguments. Useful when Docker Compose runs SQL files at database boot in alphabetical order and you need guaranteed execution ordering.

Usage

chmod +x generate-sql-migration.sh
./generate-sql-migration.sh [names...]
Example
./generate-sql-migration.sh sample iNit Database
# → creates sql/1703395808-SampleInitDatabase.sql

Configuration

The default output path is sql/. To change, edit default_path at the top of the script.

What the script does

  1. Capitalizes each input word.
  2. Removes accents.
  3. Strips spaces and punctuation.
  4. Prepends a Unix timestamp.
  5. Writes the resulting file with a default header (generation date, author, description slot, rollback comment block).

License

MIT.

#!/bin/bash
# generate-sql-migration — creates a SQL migration file with timestamp prefix
# and formatted name based on input arguments.
#
# Use case: Docker Compose running SQL files at DB boot in alphabetical order.
# Timestamped prefix guarantees execution order.
#
# Author: Davyd Maker
# License: MIT
# Usage: ./generate-sql-migration.sh [names...]
# Example: ./generate-sql-migration.sh sample iNit Database
# Output: sql/1703395808-SampleInitDatabase.sql
# Set the default path for the migration files
default_path="sql/"
# Get the current timestamp
timestamp=$(date +%s)
# Capitalize the input arguments
string_capitalized=$(echo "$@" | awk '{for (i=1; i<=NF; i++) $i=toupper(substr($i, 1, 1)) tolower(substr($i, 2));}1')
# Remove accents from the capitalized string
string_without_accents=$(echo "$string_capitalized" | iconv -f UTF-8 -t ASCII//TRANSLIT)
# Remove spaces and punctuation from the string
string_concatenated=$(echo "$string_without_accents" | tr -d '[:space:][:punct:]')
# Generate the migration name with timestamp and formatted string
migration_name="${timestamp}-${string_concatenated}.sql"
# Concatenate the default path with the migration name
migration_path="${default_path}${migration_name}"
# Create the migration file
touch "$migration_path"
# Get the current user
current_user=$USER
# Write header to the migration file
{
echo "-- Generation Date: $(date)"
echo "-- Author: $current_user"
echo "-- Description: <describe the purpose of this SQL script here>"
echo ""
echo ""
echo ""
echo "-- Fill in the rollback script in the comment for registration and eventual need"
echo "/*"
echo ""
echo "*/"
} > "$migration_path"
# Print the migration name with success message
echo "Migration $string_concatenated created successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment