Last active
March 28, 2024 12:59
-
-
Save Tug/ac21a96502facb752d25ac1c206b5e4a to your computer and use it in GitHub Desktop.
Convert Cockroach DDL export to Postgres
This file contains hidden or 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
#!/usr/bin/env bash | |
# Adapted from https://dev.to/franckpachot/moving-data-from-cockroachdb-to-postgresql-or-yugabytedb-462h | |
input_sql=$1 | |
gawk ' | |
# Create extensions that are default in CR | |
NR==1 { | |
print "create extension if not exists pgcrypto;" > ("tab-" FILENAME) | |
} | |
# Remove any cast to any type | |
{ | |
gsub(/:::?[^, )]*/, "") | |
} | |
# Replace STRING with TEXT and STRING[] with TEXT[]... | |
{ | |
gsub(/ STRING/, " TEXT") | |
} | |
# Replace current_timestamp() with NOW() | |
{ | |
gsub(/current_timestamp\(\)/, "NOW()") | |
} | |
# Remove ASC from key definitions | |
{ | |
gsub(/ ASC/, "") | |
} | |
# Replace any defaultdb. with an empty string | |
{ | |
gsub(/defaultdb\./, "") | |
} | |
# Skip lines containing unique_rowid() | |
/unique_rowid\(\)/{ | |
next | |
} | |
# Skip constraints for the rowid field | |
/PRIMARY KEY \(rowid\)/{ | |
next | |
} | |
# Move commas at the end of lines to the beginning of the next line | |
nextline!="" { | |
$0=gensub(/(^\t*)(.*)$/, "\\1"nextline"\\2", 1) | |
nextline="" | |
} | |
/,$/ { | |
sub(/,$/, "") | |
nextline="," | |
} | |
# INDEX clause in the CREATE TABLE is not a SQL syntax | |
/^CREATE TABLE/ { | |
table=gensub(/^CREATE TABLE (.*) \(/,"\\1",1) | |
} | |
/^\t*,(UNIQUE )?INDEX/ { | |
indexes=indexes"\n"gensub(/ STORING /," INCLUDE ",1,gensub(/^\t*,(UNIQUE )?(INDEX)([^(]+)(.*)( STORING)?(.*)$/, "create \\1\\2 \\3 on "table" \\4 \\5 \\6;",1)) | |
$0=gensub(/(^\t*),(.*)$/, "\\1--\\2", 1) | |
} | |
# Validate constraints at creation | |
/^ALTER TABLE.*ADD CONSTRAINT.*/ { | |
$0=gensub(/(.*)(NOT VALID)?(;)$/, "\\1\\3", 1) | |
{print > "ref-"FILENAME} | |
$0="--"$0 | |
} | |
/^ALTER TABLE.*VALIDATE CONSTRAINT.*;$/ { | |
$0="--"$0 | |
} | |
# Print that to the create table file | |
{print > ("tab-" FILENAME)} | |
END { | |
if(indexes != "") print indexes > ("ind-"FILENAME) | |
} | |
' "$input_sql" | |
cat "tab-$input_sql" "ind-$input_sql" > "out-$input_sql" | |
rm "tab-$input_sql" "ind-$input_sql" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment