2019-12-11 18:18:50 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2022-03-21 14:48:03 +00:00
|
|
|
//go:build ignore
|
2019-12-11 18:18:50 +00:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// pg-to-crdb converts a Postgres plaintext sql backup generated by pg_dump
|
|
|
|
// to a compatible plaintext sql backup that only has SQL statements compatible with CockroachDB.
|
2020-07-16 16:27:24 +01:00
|
|
|
//
|
2019-12-11 18:18:50 +00:00
|
|
|
// Usage:
|
2022-10-11 12:39:08 +01:00
|
|
|
//
|
|
|
|
// cat postgres_backup.sql | go run pg-to-crdb.go > cockroach_backup.sql
|
2019-12-11 18:18:50 +00:00
|
|
|
func main() {
|
|
|
|
print := false
|
|
|
|
printOnce := false
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
scanner.Buffer(nil, 5242880)
|
|
|
|
for scanner.Scan() {
|
|
|
|
text := scanner.Text()
|
|
|
|
|
|
|
|
if strings.HasPrefix(text, "CREATE TABLE ") ||
|
|
|
|
strings.HasPrefix(text, "CREATE SEQUENCE ") ||
|
|
|
|
strings.HasPrefix(text, "COPY ") {
|
|
|
|
print = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(text, "ALTER TABLE ") ||
|
|
|
|
strings.HasPrefix(text, "CREATE INDEX ") ||
|
|
|
|
strings.HasPrefix(text, "GRANT ALL ON TABLE ") ||
|
|
|
|
strings.HasPrefix(text, "GRANT ALL ON SEQUENCE ") ||
|
|
|
|
strings.HasPrefix(text, "GRANT SELECT ON TABLE ") ||
|
|
|
|
strings.HasPrefix(text, "GRANT SELECT ON SEQUENCE ") ||
|
|
|
|
strings.HasPrefix(text, "ALTER SEQUENCE ") ||
|
|
|
|
(strings.HasPrefix(text, "CREATE UNIQUE INDEX ") && !strings.Contains(text, " WHERE ")) {
|
|
|
|
|
|
|
|
if text[len(text)-1] == ';' {
|
|
|
|
printOnce = true
|
|
|
|
} else {
|
|
|
|
print = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !print && !printOnce {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(text)
|
|
|
|
|
|
|
|
if text == ");" || text == "\\." || text == "" {
|
|
|
|
print = false
|
|
|
|
}
|
|
|
|
printOnce = false
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|