Skip to content

Commit

Permalink
Merge pull request #307 from civo/postgres-db
Browse files Browse the repository at this point in the history
Add support for PostgreSQL in DB
  • Loading branch information
vishalanarase authored Apr 18, 2023
2 parents 066e853 + 6afbf23 commit 26bed9a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cmd/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

var firewallID, networkID, size, updatedName string
var firewallID, networkID, size, updatedName, software, softwareVersion string
var nodes int

// DBCmd is the root command for the db subcommand
Expand Down Expand Up @@ -40,6 +40,8 @@ func init() {
dbCreateCmd.Flags().StringVarP(&networkID, "network", "n", "", "the network to use for the database")
dbCreateCmd.Flags().StringVarP(&rulesFirewall, "firewall-rules", "u", "", "the firewall rules to use for the database")
dbCreateCmd.Flags().StringVarP(&size, "size", "s", "g3.db.small", "the size of the database. You can list available DB sizes by `civo size list -s database`")
dbCreateCmd.Flags().StringVarP(&software, "software", "m", "MySQL", "the software to use for the database. One of: MySQL, PostgreSQL. Please make sure you use the correct capitalisation.")
dbCreateCmd.Flags().StringVarP(&softwareVersion, "version", "v", "8.0", "the version of the software to use for the database.")

dbUpdateCmd.Flags().IntVarP(&nodes, "nodes", "", 0, "the number of nodes for the database")
dbUpdateCmd.Flags().StringVarP(&firewallID, "firewall", "", "", "the firewall to use for the database")
Expand Down
57 changes: 49 additions & 8 deletions cmd/database/database_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var rulesFirewall string
var dbCreateCmd = &cobra.Command{
Use: "create",
Aliases: []string{"new", "add"},
Example: "civo db create <DATABASE-NAME> --size <SIZE>",
Example: "civo db create <DATABASE-NAME> --size <SIZE> --software <SOFTWARE_NAME> --version <SOFTWARE_VERSION>",
Short: "Create a new database",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -73,14 +73,55 @@ var dbCreateCmd = &cobra.Command{
}
}

dbVersions, err := client.ListDBVersions()
if err != nil {
utility.Error("Unable to list database versions %s", err)
os.Exit(1)
}

if software == "" {
software = "MySQL"
}

if softwareVersion == "" && software == "MySQL" {
softwareVersion = "8.0"
}

softwareIsValid := false
softwareVersionIsValid := false
if software != "" {
for swName, version := range dbVersions {
if swName == software {
softwareIsValid = true
}
for i, v := range version {
if v.SoftwareVersion == version[i].SoftwareVersion {
softwareVersionIsValid = true
}
}
}
}

if !softwareIsValid {
utility.Error("The provided software name is not valid. Make sure you use correct capitalization (e.g. MySQL, PostgreSQL)")
os.Exit(1)
}

if !softwareVersionIsValid {
utility.Error("The provided software version is not valid")
os.Exit(1)
}

configDB := civogo.CreateDatabaseRequest{
Name: args[0],
Size: size,
NetworkID: network.ID,
Nodes: nodes,
FirewallID: firewallID,
FirewallRules: rulesFirewall,
Region: client.Region,
Name: args[0],
Size: size,
NetworkID: network.ID,
Nodes: nodes,
FirewallID: firewallID,
FirewallRules: rulesFirewall,
Software: software,
SoftwareVersion: softwareVersion,
Region: client.Region,
}

db, err := client.NewDatabase(&configDB)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
github.com/briandowns/spinner v1.11.1
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c // indirect
github.com/civo/civogo v0.3.24
github.com/civo/civogo v0.3.26
github.com/dsnet/compress v0.0.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/google/go-github v17.0.0+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/civo/civogo v0.3.24 h1:7kXEhtVUFkWIunNePqPCcIUilOktnXLWrKfi5sDqBNQ=
github.com/civo/civogo v0.3.24/go.mod h1:SbS06e0JPgIF27r1sLC97gjU1xWmONQeHgzF1hfLpak=
github.com/civo/civogo v0.3.26 h1:vAg9nrzVSceUCdM0ACEZzM5khl8tnddwXFXMkGCQy94=
github.com/civo/civogo v0.3.26/go.mod h1:SbS06e0JPgIF27r1sLC97gjU1xWmONQeHgzF1hfLpak=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down

0 comments on commit 26bed9a

Please sign in to comment.