-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace python with a bash script for postgres to sqlite conversion (…
…PROJQUAY-7613) (#158) * Write bash script for postgres to sqlite sql conversion (PROJQUAY-7613) Removes the python dependency since some of the host machine don't provide python3 out of the box * Remove python sql version script * Fix regex --------- Signed-off-by: harishsurf <hgovinda@redhat.com>
- Loading branch information
1 parent
08b3899
commit 6cd20c3
Showing
4 changed files
with
60 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 0 additions & 47 deletions
47
ansible-runner/context/app/project/roles/mirror_appliance/templates/pg_to_sqlite.py
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
ansible-runner/context/app/project/roles/mirror_appliance/templates/pg_to_sqlite.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/bin/bash | ||
|
||
# Function that takes postgres data-only dump as "input_file" arg and converts it into a sqlite compatible .sql file | ||
pg_to_sqlite() { | ||
local input_file="$1" | ||
local output_file="$2" | ||
|
||
# Read the input file | ||
sql=$(<"$input_file") | ||
|
||
# Replace PostgreSQL-specific data types with SQLite equivalents | ||
sql=$(echo "$sql" | sed "s/'true'/1/g") | ||
sql=$(echo "$sql" | sed "s/'false'/0/g") | ||
|
||
# Remove PostgreSQL-specific commands not supported by SQLite | ||
sql=$(echo "$sql" | sed -E '/SET\s+\w+\s*=\s*[^;]+;/d') | ||
sql=$(echo "$sql" | sed -E '/^\s*--.*$/d') | ||
sql=$(echo "$sql" | sed -E '/ALTER TABLE .*? DISABLE TRIGGER ALL;/d') | ||
sql=$(echo "$sql" | sed -E '/ALTER TABLE .*? ENABLE TRIGGER ALL;/d') | ||
sql=$(echo "$sql" | sed -E "s/SELECT pg_catalog\.set_config\('search_path', '', false\);//g") | ||
sql=$(echo "$sql" | sed -E '/SET SESSION AUTHORIZATION DEFAULT;/d') | ||
sql=$(echo "$sql" | sed -E '/SET client_encoding = '\''UTF8'\'';/d') | ||
|
||
# Remove lines starting with "pg_dump:" | ||
sql=$(echo "$sql" | sed -E '/^pg_dump:.*$/d') | ||
|
||
# Remove original PostgreSQL sequence set statements | ||
sql=$(echo "$sql" | sed -E "s/SELECT pg_catalog\.setval\('public\..*?', [0-9]+, (true|false)\);//g") | ||
|
||
# Remove the `public.` schema prefix from table names | ||
sql=$(echo "$sql" | sed "s/INSERT INTO public\./INSERT INTO /g") | ||
|
||
# Remove empty lines | ||
sql=$(echo "$sql" | sed "/^\s*$/d") | ||
|
||
# Trim whitespace from the beginning and end | ||
sql=$(echo "$sql" | sed 's/^[ \t]*//;s/[ \t]*$//') | ||
|
||
# Write the output to the specified file | ||
echo "$sql" > "$output_file" | ||
} | ||
|
||
# Check if the script receives two arguments | ||
if [ "$#" -ne 2 ]; then | ||
echo "Usage: $0 <input_file> <output_file>" | ||
exit 1 | ||
fi | ||
|
||
input_file="$1" | ||
output_file="$2" | ||
|
||
# Call the function | ||
pg_to_sqlite "$input_file" "$output_file" |