Compare commits

...

9 commits
v1 ... main

Author SHA1 Message Date
Magnus Åhall
aa1ead78e1 Bumped to v5 2024-04-24 16:32:51 +02:00
Magnus Åhall
9367bab359 Better descriptions when extracting 2024-04-24 16:32:41 +02:00
2b240d873b Update README.md 2024-04-23 19:08:44 +00:00
2e4b71e688 Update README.md 2024-04-23 19:08:14 +00:00
Magnus Åhall
45ccbe091c Added readme 2024-04-23 21:07:13 +02:00
Magnus Åhall
01e4588bf7 Better handling of list, bumped to v4 2024-04-23 20:41:39 +02:00
Magnus Åhall
77260743a0 Bumped to v3 2024-04-23 20:24:51 +02:00
Magnus Åhall
36d0eacf8f Added -list option 2024-04-23 20:24:40 +02:00
Magnus Åhall
0bfcd7a8a9 Flag version, bumped to v2 2024-04-23 20:21:08 +02:00
3 changed files with 38 additions and 6 deletions

11
README.md Normal file
View file

@ -0,0 +1,11 @@
# Usage
pgsplit takes a PostgreSQL dump with multiple databases through STDIN and extracts them to the current directory, named with the database name and an .sql extension.
`pgsplit -list` prints the database names contained in the SQL dump.
`pgsplit` extracts all databases.
`pgsplit -include-all -except foo -except bar` extracts all databases except for foo and bar.
`pgsplit -exclude-all -except foo -except bar` extracts only databases foo and bar.

14
flag.go
View file

@ -19,17 +19,31 @@ func (i *arrayFlags) Set(value string) error {
} }
var ( var (
flagVersion bool
flagList bool
flagIncludeAll bool flagIncludeAll bool
flagExcludeAll bool flagExcludeAll bool
flagExcept arrayFlags flagExcept arrayFlags
) )
func init() { func init() {
flag.BoolVar(&flagList, "list", false, "Only list database names.")
flag.BoolVar(&flagVersion, "version", false, "Prints version and exits.")
flag.BoolVar(&flagIncludeAll, "include-all", false, "Split out all databases, with provided exceptions.") flag.BoolVar(&flagIncludeAll, "include-all", false, "Split out all databases, with provided exceptions.")
flag.BoolVar(&flagExcludeAll, "exclude-all", false, "Split out no databases, with provided exceptions.") flag.BoolVar(&flagExcludeAll, "exclude-all", false, "Split out no databases, with provided exceptions.")
flag.Var(&flagExcept, "except", "Exception to rule, can be used multiple times") flag.Var(&flagExcept, "except", "Exception to rule, can be used multiple times")
flag.Parse() flag.Parse()
if flagVersion {
fmt.Println(VERSION)
os.Exit(0)
}
if flagList && (flagIncludeAll || flagExcludeAll || len(flagExcept) > 0) {
fmt.Println("-list is not usable with any other flag.")
os.Exit(1)
}
if flagIncludeAll && flagExcludeAll { if flagIncludeAll && flagExcludeAll {
fmt.Println("-include-all and -exclude-all are mutually exclusive") fmt.Println("-include-all and -exclude-all are mutually exclusive")
os.Exit(1) os.Exit(1)

17
main.go
View file

@ -9,7 +9,7 @@ import (
"slices" "slices"
) )
const VERSION = "v1" const VERSION = "v5"
const MAXLINE = 1048576 const MAXLINE = 1048576
type Db struct { type Db struct {
@ -18,10 +18,12 @@ type Db struct {
file *os.File file *os.File
} }
func NewDb(name string) (db Db, err error) { func NewDb(name string) (db Db) {
db.Name = name db.Name = name
fmt.Printf("Database %s\n", db.Name) return
}
func (db *Db) Open() (err error) {
db.file, err = os.OpenFile( db.file, err = os.OpenFile(
db.Name+".sql", db.Name+".sql",
os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.O_CREATE|os.O_WRONLY|os.O_TRUNC,
@ -64,18 +66,23 @@ func main() {
prevDb.Close() prevDb.Close()
dbName := dbMatch[1] dbName := dbMatch[1]
db = NewDb(dbName)
if (flagIncludeAll && slices.Contains(flagExcept, dbName)) || (flagExcludeAll && !slices.Contains(flagExcept, dbName)) { if (flagIncludeAll && slices.Contains(flagExcept, dbName)) || (flagExcludeAll && !slices.Contains(flagExcept, dbName)) {
fmt.Printf("Skipping %s\n", dbName) fmt.Printf("Skipping %s\n", dbName)
continue continue
} }
if db, err = NewDb(dbName); err != nil { fmt.Printf("Extracting %s\n", db.Name)
if flagList {
continue
}
if err = db.Open(); err != nil {
fmt.Printf("%s: %s\n", dbName, err) fmt.Printf("%s: %s\n", dbName, err)
os.Exit(1) os.Exit(1)
} }
} }
if db.IsOpen() { if !flagList && db.IsOpen() {
db.Write(line) db.Write(line)
} }