Skip to content

Commit 582c06a

Browse files
committed
add fitur migrations
1 parent 122f152 commit 582c06a

10 files changed

+565
-3
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
# Overview
22
libpostgres is a collection of postgresql connections from various sources such as making connections with amp, elastic, etc.
33

4+
# How to use
5+
6+
## Connect to database
7+
```go
8+
dbOptions := libpostgres.Options{
9+
Host: "db_host",
10+
Port: "db_port",
11+
Username: "db_username",
12+
Password: "db_password",
13+
DBName: "db_name",
14+
SSLMode: "db_sslmode",
15+
}
16+
db, err := libpostgres.Connect(dbOptions)
17+
if err != nil {
18+
fmt.Println("Error conn to DB", err)
19+
panic(err)
20+
}
21+
```
22+
23+
## Migrate database
24+
- Create directory `db/migrations` on yout project.
25+
- Create your file `sql` with format name
26+
- 000001_create_users_table.down.sql
27+
- 000001_create_users_table.up.sql
28+
In the `.up.sql` file let's create the table:
29+
```sql
30+
CREATE TABLE IF NOT EXISTS users(
31+
user_id serial PRIMARY KEY,
32+
username VARCHAR (50) UNIQUE NOT NULL,
33+
password VARCHAR (50) NOT NULL,
34+
email VARCHAR (300) UNIQUE NOT NULL
35+
);
36+
```
37+
And in the .down.sql let's delete it:
38+
```sql
39+
DROP TABLE IF EXISTS users;
40+
```
41+
42+
Read more [here](https://github.com/golang-migrate/migrate/blob/master/database/postgres/TUTORIAL.md)
43+
44+
```go
45+
err := Migrate(db, "file://./migrations")
46+
if err != nil {
47+
fmt.Println("Error migrate DB", err)
48+
panic(err)
49+
}
50+
```
51+
452
# Constribute
553
Please welcome .

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module github.com/jiharal/libpostgres
33
go 1.14
44

55
require (
6-
github.com/lib/pq v1.0.0
6+
github.com/golang-migrate/migrate/v4 v4.11.0
7+
github.com/lib/pq v1.3.0
78
github.com/stretchr/testify v1.5.1
89
go.elastic.co/apm/module/apmsql v1.7.2
910
)

go.sum

Lines changed: 460 additions & 0 deletions
Large diffs are not rendered by default.

libpostgres.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type Options struct {
1919
Password string
2020
DBName string
2121
ConnectTimeout int
22+
MaxOpenConn int
23+
MaxIdleConn int
2224
SSLCert string
2325
SSLKey string
2426
SSLRootCert string
@@ -70,7 +72,8 @@ func Connect(options Options) (*sql.DB, error) {
7072
if err != nil {
7173
return nil, err
7274
}
73-
75+
db.SetMaxOpenConns(options.MaxOpenConn)
76+
db.SetMaxIdleConns(options.MaxIdleConn)
7477
return db, nil
7578
}
7679

@@ -107,6 +110,7 @@ func ConnectWithAMP(options Options) (*sql.DB, error) {
107110
if err != nil {
108111
return nil, err
109112
}
110-
113+
db.SetMaxOpenConns(options.MaxOpenConn)
114+
db.SetMaxIdleConns(options.MaxIdleConn)
111115
return db, nil
112116
}

libpostgres_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ func TestLibpostgres(t *testing.T) {
3535
require.NoError(t, err)
3636
t.Log(name1)
3737

38+
errM := Migrate(db, "file://./migrations")
39+
require.NoError(t, errM)
40+
3841
}

migration.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package libpostgres
2+
3+
import (
4+
"database/sql"
5+
6+
"github.com/golang-migrate/migrate/v4"
7+
"github.com/golang-migrate/migrate/v4/database/postgres"
8+
_ "github.com/golang-migrate/migrate/v4/source/file"
9+
)
10+
11+
// Migrate is used to migrate table
12+
// Ex: Migrate(opt, "file://db/migrations")
13+
func Migrate(db *sql.DB, path string) error {
14+
driver, err := postgres.WithInstance(db, &postgres.Config{})
15+
if err != nil {
16+
return err
17+
}
18+
m, err := migrate.NewWithDatabaseInstance(path, "postgres", driver)
19+
err = m.Up()
20+
if err != nil && err.Error() != "no change" {
21+
return err
22+
}
23+
return nil
24+
}
25+
26+
func DownOneStep(db *sql.DB, path string) error {
27+
driver, err := postgres.WithInstance(db, &postgres.Config{})
28+
if err != nil {
29+
return err
30+
}
31+
m, err := migrate.NewWithDatabaseInstance(path, "postgres", driver)
32+
err = m.Steps(-1)
33+
if err != nil {
34+
return err
35+
}
36+
return nil
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS kurir;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE IF NOT EXISTS kurir(
2+
user_id serial PRIMARY KEY,
3+
username VARCHAR (50) UNIQUE NOT NULL,
4+
password VARCHAR (50) NOT NULL,
5+
email VARCHAR (300) UNIQUE NOT NULL
6+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alter table kurir drop column if exists address;

migrations/2_add_colum_address.up.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alter table kurir add column if exists address varying(255);

0 commit comments

Comments
 (0)