packagepostgresimport ("context""database/sql""fmt""os""strconv")// Open database commectionfuncOpen() (*sql.DB, error) {var db *sql.DB port, err := strconv.Atoi(os.Getenv("POSTGRES_PORT"))if err !=nil {return db, err }return sql.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", os.Getenv("POSTGRES_HOST"), port, os.Getenv("POSTGRES_USER"), os.Getenv("POSTGRES_PASSWORD"), os.Getenv("POSTGRES_DB"), ), )}// StatusCheck returns nil if it can successfully talk to the database. It// returns a non-nil error otherwise.funcStatusCheck(ctx context.Context, db *sql.DB) error {// Run a simple query to determine connectivity. The db has a "Ping" method// but it can false-positive when it was previously able to talk to the// database but the database has since gone away. Running this query forces a// round trip to the database.constq=`SELECT true`var tmp boolreturn db.QueryRowContext(ctx, q).Scan(&tmp)}
Buat file schema/migrate.go
Buat file schema/seed.go
Buat file cmd/cli.go
Buat database drivers
Jalankan go run cmd/cli.go migrate
Update file server.go untuk membuat koneksi database
Update server.go untuk mengaupdate roting dengan menginject db ke service
Update server.go service handler agar mempunyai proprety db
Update file server.go untuk membuat fungsi logError
Update file server.go untuk mengupdate fungsi List
Update file server.go untuk mengupdate fungsi Create
Update file server.go untuk mengupdate fungsi Update
Update file server.go untuk mengupdate fungsi Delete
package schema
import (
"database/sql"
"github.com/GuiaBolso/darwin"
)
var migrations = []darwin.Migration{
{
Version: 1,
Description: "Create drivers Table",
Script: `
CREATE TABLE public.drivers (
id uuid NOT NULL,
name varchar NOT NULL,
phone varchar NOT NULL,
licence_number varchar NOT NULL,
company_id varchar NOT NULL,
company_name varchar NOT NULL,
is_deleted bool NOT NULL DEFAULT false,
created timestamp(0) NOT NULL,
created_by varchar NOT NULL,
updated timestamp(0) NOT NULL,
updated_by varchar NOT NULL,
CONSTRAINT drivers_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX drivers_phone ON public.drivers USING btree (phone);
`,
},
}
// Migrate attempts to bring the schema for db up to date with the migrations
// defined in this package.
func Migrate(db *sql.DB) error {
driver := darwin.NewGenericDriver(db, darwin.PostgresDialect{})
d := darwin.New(driver, migrations, nil)
return d.Migrate()
}
package schema
import (
"database/sql"
"fmt"
)
// seeds is a string constant containing all of the queries needed to get the
// db seeded to a useful state for development.
//
// Using a constant in a .go file is an easy way to ensure the queries are part
// of the compiled executable and avoids pathing issues with the working
// directory. It has the downside that it lacks syntax highlighting and may be
// harder to read for some cases compared to using .sql files. You may also
// consider a combined approach using a tool like packr or go-bindata.
//
// Note that database servers besides PostgreSQL may not support running
// multiple queries as part of the same execution so this single large constant
// may need to be broken up.
// Seed runs the set of seed-data queries against db. The queries are ran in a
// transaction and rolled back if any fail.
func Seed(db *sql.DB, seeds ...string) error {
tx, err := db.Begin()
if err != nil {
return err
}
for _, seed := range seeds {
_, err = tx.Exec(seed)
if err != nil {
tx.Rollback()
fmt.Println("error execute seed")
return err
}
}
return tx.Commit()
}