Skip to content

Commit

Permalink
feat(sql): add support for postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Dec 12, 2022
1 parent ccd9ff9 commit 6c7ccbc
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 7 deletions.
25 changes: 19 additions & 6 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ tasks:
mongo
- name: PostgreSQL
env:
DB_CONNECTION: "user=postgres password=password host=localhost port=5432 dbname=gobblr"
DB_TYPE: postgres
command: |
docker run \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=gobblr \
--rm \
-p 5432:5432 \
--name pgsql \
postgres
postgres &
./examples/simple/migrations/simple
- before: |
curl -sfL gpm.simonemms.com | bash
Expand Down Expand Up @@ -70,13 +76,20 @@ tasks:
pre-commit install --install-hooks
ports:
- port: 3306
name: MySQL
visibility: private

- port: 5432
name: PostgreSQL
visibility: private

- port: 27017
visibility: private

vscode:
extensions:
- donjayamanne.git-extension-pack
- EditorConfig.EditorConfig
- golang.go

ports:
- port: 3306
name: MySQL
visibility: private
28 changes: 28 additions & 0 deletions cmd/db_sql_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,43 @@ limitations under the License.
package cmd

import (
"github.com/mrsimonemms/gobblr/pkg/drivers/sql"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var dbSqlPostgresOpts struct {
Database string
Host string
Password string
Port int
User string
}

// dbSqlPostgresCmd represents the postgres command
var dbSqlPostgresCmd = &cobra.Command{
Use: "postgres",
Short: "PostgreSQL ingestion commands",
Run: func(cmd *cobra.Command, args []string) {
dbOpts.Driver = sql.PostgreSQL(
dbSqlPostgresOpts.Database,
dbSqlPostgresOpts.Host,
dbSqlPostgresOpts.Password,
dbSqlPostgresOpts.Port,
dbSqlPostgresOpts.User,
)
},
}

func init() {
dbSqlCmd.AddCommand(dbSqlPostgresCmd)

viper.SetDefault("host", "localhost")
viper.SetDefault("port", 5432)
viper.SetDefault("username", "postgres")
dbSqlPostgresCmd.Flags().StringVarP(&dbSqlPostgresOpts.Database, "database", "d", viper.GetString("database"), "name of the database to use")
dbSqlPostgresCmd.Flags().StringVarP(&dbSqlPostgresOpts.Host, "host", "H", viper.GetString("host"), "database host name")
dbSqlPostgresCmd.Flags().StringVarP(&dbSqlPostgresOpts.Password, "password", "p", viper.GetString("password"), "database password")
dbSqlPostgresCmd.Flags().IntVarP(&dbSqlPostgresOpts.Port, "port", "P", viper.GetInt("port"), "database port")
dbSqlPostgresCmd.Flags().StringVarP(&dbSqlPostgresOpts.User, "username", "u", viper.GetString("username"), "database username")
}
13 changes: 13 additions & 0 deletions examples/simple/migrations/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ go 1.19
require (
github.com/cenkalti/backoff/v4 v4.2.0
gorm.io/driver/mysql v1.4.4
gorm.io/driver/postgres v1.4.5
gorm.io/gorm v1.24.2
)

require (
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.8.1 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/text v0.4.0 // indirect
)
191 changes: 191 additions & 0 deletions examples/simple/migrations/go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions examples/simple/migrations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/cenkalti/backoff/v4"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -45,6 +46,8 @@ func main() {
switch dbType {
case "mysql":
dialector = mysql.Open(connection)
case "postgres":
dialector = postgres.Open(connection)
default:
panic(fmt.Errorf("unknown db type: %s", dbType))
}
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
gorm.io/driver/mysql v1.4.4
gorm.io/driver/postgres v1.4.5
gorm.io/gorm v1.24.2
)

Expand All @@ -14,6 +15,14 @@ require (
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/magiconair/properties v1.8.6 // indirect
Expand All @@ -25,6 +34,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
golang.org/x/text v0.4.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
Loading

0 comments on commit 6c7ccbc

Please sign in to comment.