generate_db.sh 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/bash
  2. #
  3. # Copyright (C) 2024 The LineageOS Project
  4. #
  5. # SPDX-License-Identifier: Apache-2.0
  6. #
  7. set -e
  8. if [[ $# -le 2 ]]; then
  9. echo "syntax: generate_db.sh sqlite3 target_db sql_file0 sql_file1..."
  10. exit 1
  11. fi
  12. SQLITE=$1
  13. if [[ ! -x "$SQLITE" ]]; then
  14. echo "sqlite binary not found or not executable: $SQLITE"
  15. exit 1
  16. fi
  17. TARGET_DB=$2
  18. shift 2
  19. # Split the config sql and ecc sql files
  20. for file in "$@"; do
  21. if [[ $file == *_config.sql ]]; then
  22. CONFIG_SQL_FILES+=("$file")
  23. else
  24. ECC_SQL_FILES+=("$file")
  25. fi
  26. done
  27. # Sort the files
  28. IFS=$'\n' CONFIG_SQL_FILES=($(sort -V <<< "${CONFIG_SQL_FILES[*]}"))
  29. IFS=$'\n' ECC_SQL_FILES=($(sort -V <<< "${ECC_SQL_FILES[*]}"))
  30. unset IFS
  31. # Config migrations should be applied after ecc migrations
  32. ORDERED_MIGRATIONS=("${ECC_SQL_FILES[@]}" "${CONFIG_SQL_FILES[@]}")
  33. rm -f "$TARGET_DB"
  34. {
  35. echo "BEGIN TRANSACTION;"
  36. for file in "${ORDERED_MIGRATIONS[@]}"; do
  37. cat "$file"
  38. done
  39. echo "COMMIT TRANSACTION;"
  40. } | $SQLITE "$TARGET_DB"