Skip to content

Instantly share code, notes, and snippets.

@zw963
Created May 26, 2021 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zw963/c37f21b8a5605a253558d711f2ad6718 to your computer and use it in GitHub Desktop.
Save zw963/c37f21b8a5605a253558d711f2ad6718 to your computer and use it in GitHub Desktop.
#!/bin/bash
sec=$(date '+%Y%m%d%H%M%S')
mkdir -p db/migrations
set -u
new_migration=db/migrations/${sec}_$1.rb
if [[ "$1" =~ ^create_join_table_for_(.+)_and_(.+) ]]; then
content="create_join_table"
elif [[ "$1" =~ ^create_(.+) ]]; then
content="create_table(:${BASH_REMATCH[1]}) do
primary_key :id
end"
elif [[ "$1" =~ ^add_(.+_id)_to_(.+) ]]; then
content="alter_table(:${BASH_REMATCH[2]}) do
add_foreign_key :${BASH_REMATCH[1]},
end"
elif [[ "$1" =~ ^(add|drop)_(.+)_to_(.+) ]]; then
content="alter_table(:${BASH_REMATCH[3]}) do
${BASH_}_column :${BASH_REMATCH[2]}, String
end"
elif [[ "$1" =~ ^rename_(.+)_to_(.+)_for_(.+) ]]; then
content="alter_table(:${BASH_REMATCH[2]}) do
rename_column :${BASH_REMATCH[1]}, :${BASH_REMATCH[2]}
end"
elif [[ "$1" =~ ^rename_(.+)_to_(.+)$ ]]; then
content="rename_table :${BASH_REMATCH[1]} :${BASH_REMATCH[2]}"
fi
cat <<HEREDOC > $new_migration
Sequel.migration do
change do
$content
end
end
HEREDOC
echo "Created $new_migration"
# if [[ "$1" =~ [0-9]+_create_(.+) ]]; then
# export table_name="${BASH_REMATCH[1]}"
# content=$(cat <<"HEREDOC"
# create_table($table_name) do
# end
# HEREDOC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment