Skip to content

Commit

Permalink
Migrate mysql db to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sumi-k committed Oct 6, 2023
1 parent a0d4761 commit 90e2660
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 71 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ jobs:
run_tests:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
postgres:
image: postgres:15
ports:
- 3306:3306
- 5432:5432
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: website_admin_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: website_admin_test
steps:
- name: Checkout
uses: actions/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion blogs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -39,6 +38,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
1 change: 0 additions & 1 deletion blogs/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
Expand Down
2 changes: 1 addition & 1 deletion contact/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ require (
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -36,6 +35,7 @@ require (
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion db/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module db
go 1.20

require (
github.com/go-sql-driver/mysql v1.6.0
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.2.0
github.com/sirupsen/logrus v1.9.0
)

Expand Down
17 changes: 11 additions & 6 deletions db/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"strings"
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/reflectx"
_ "github.com/lib/pq"
log "github.com/sirupsen/logrus"
)

Expand All @@ -19,13 +19,13 @@ func NewSql() *sqlx.DB {
username := os.Getenv("DB_USERNAME")

if username == "" {
username = "root"
username = "postgres"
}

password := os.Getenv("DB_PASSWORD")

if password == "" {
password = "root"
password = "postgres"
}

host := os.Getenv("DB_HOST")
Expand All @@ -37,16 +37,21 @@ func NewSql() *sqlx.DB {
port := os.Getenv("DB_PORT")

if port == "" {
port = "3306"
port = "5432"
}

name := os.Getenv("DB_NAME")

if name == "" {
name = "website_admin"
name = "postgres"
}

db = sqlx.MustConnect("mysql", username+":"+password+"@("+host+":"+port+")/"+name)
sslmode := "require"
if os.Getenv("DB_ENV") == "test" {
sslmode = "disable"
}

db = sqlx.MustConnect("postgres", "postgres://"+username+":"+password+"@"+host+":"+port+"?sslmode="+sslmode)

db.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
db.SetConnMaxLifetime(time.Minute * 1)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.0 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand All @@ -65,6 +64,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ github.com/go-playground/validator/v10 v10.15.0 h1:nDU5XeOKtB3GEa+uB7GNYwhVKsgjA
github.com/go-playground/validator/v10 v10.15.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
Expand Down
10 changes: 5 additions & 5 deletions infrastructure/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ Parameters:
Description: aws-region.
DbUsername:
Type: String
Description: Mysql database username.
Description: Postgres database username.
DbPassword:
Type: String
Description: Mysql database password.
Description: Postgres database password.
DbHost:
Type: String
Description: Mysql database host.
Description: Postgres database host.
DbPort:
Type: String
Description: Mysql database port.
Description: Postgres database port.
DbName:
Type: String
Description: Mysql database name.
Description: Postgres database name.
RecaptchaConfigJSONBase64:
Type: String
Description: Recaptcha enterprise credentials in base64 format
Expand Down
2 changes: 1 addition & 1 deletion jobs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ require (
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -38,6 +37,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
51 changes: 21 additions & 30 deletions jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ func (repository *CareerRepository) GetCareers() ([]Career, error) {

careers := []Career{}

err := repository.Db.Select(&careers, "SELECT id, title, summary, description, button_name, qualification, employment_type,"+
"base_salary, experience, is_active, skills, total_openings,"+
"responsibilities, icon_name, unique_id, seo_title, seo_description,"+
"apply_seo_title, apply_seo_description, `index` "+
"FROM jobs WHERE is_active = 1 ORDER BY `index`")
err := repository.Db.Select(&careers, "SELECT j.id, j.title, j.summary, j.description, j.button_name, j.qualification, "+
"j.employment_type, j.base_salary, j.experience, j.is_active, j.skills, "+
"j.total_openings, j.responsibilities, i.value as icon_name, "+
"j.unique_id, j.seo_title, j.seo_description, "+
"j.apply_seo_title, j.apply_seo_description, j.index "+
"FROM jobs j JOIN job_icons i ON j.icon_name = i.id "+
"WHERE j.is_active = true "+
"ORDER BY j.index")

return careers, err
}
Expand All @@ -111,14 +114,14 @@ func (repository *CareerRepository) CareerById(c *gin.Context) {

id := c.Param("unique_id")

err := repository.Db.Get(&career, "SELECT id, title, summary, description, button_name, qualification, "+
"employment_type, base_salary, experience, is_active, skills, "+
"total_openings, responsibilities, icon_name, "+
"unique_id, seo_title, seo_description, "+
"apply_seo_title, apply_seo_description, `index` "+
"FROM jobs "+
"WHERE unique_id = ? AND is_active = 1 "+
"ORDER BY `index`", id)
err := repository.Db.Get(&career, "SELECT j.id, j.title, j.summary, j.description, j.button_name, j.qualification, "+
"j.employment_type, j.base_salary, j.experience, j.is_active, j.skills, "+
"j.total_openings, j.responsibilities, i.value as icon_name, "+
"j.unique_id, j.seo_title, j.seo_description, "+
"j.apply_seo_title, j.apply_seo_description, j.index "+
"FROM jobs j JOIN job_icons i ON j.icon_name = i.id "+
"WHERE j.unique_id = '"+id+"' AND j.is_active = true "+
"ORDER BY j.index")

if err != nil {
log.Error(err)
Expand Down Expand Up @@ -189,33 +192,21 @@ func (repository *CareerRepository) InsertJobApplication(input JobsApplicationsD
return err
}

stmt, err := repository.Db.Prepare(`INSERT INTO job_applications (name, email, phone, place, reference, resumeURL, position, message, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
var responseID int
err = repository.Db.QueryRow("INSERT INTO job_applications (name, email, phone, place, reference, resumeurl, position, message, status, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING id", input.Name, input.Email, input.Phone, input.Place, input.References, resumeURL, input.JobTitle, input.Message, 1, time.Now(), time.Now()).Scan(&responseID)
if err != nil {
log.Error(err)
log.Error("responseID: ", err)
return err
}

defer stmt.Close()
query, err := repository.Db.Prepare(`INSERT INTO job_applicant_statuses (applicant_id, status, ` + "`index`" + `, rejection_with_mail, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)`)
query, err := repository.Db.Prepare("INSERT INTO job_applicant_statuses (applicant_id, status, index, rejection_with_mail, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6)")

if err != nil {
log.Error(err)
log.Error("query: ", err)
return err
}
defer query.Close()

result, err := stmt.Exec(input.Name, input.Email, input.Phone, input.Place, input.References, resumeURL, input.JobTitle, input.Message, 1, time.Now(), time.Now())
if err != nil {
log.Error(err)
return err
}

responseID, err := result.LastInsertId()
if err != nil {
log.Error(err)
return err
}

_, err = query.Exec(responseID, 1, 0, false, time.Now(), time.Now())
if err != nil {
log.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion jobs/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func TestInit(t *testing.T) {
}

func TestGetAllJobs(t *testing.T) {
utils.TruncateTables(testDB)
err = utils.CreateTables(testDB)
if err != nil {
t.Errorf("Error in initializing test DB: %v", err)
}
utils.TruncateTables(testDB)
utils.PrepareTablesData(testDB)
engine := gin.New()
engine.GET("/api/careers", repo.Careers)
Expand Down
1 change: 1 addition & 0 deletions leave/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion notification/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -40,6 +39,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion sitemap/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -39,6 +38,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion utils/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ require (
require (
cloud.google.com/go/compute v1.7.0 // indirect
cloud.google.com/go/recaptchaenterprise v1.3.1 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.1.0 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/lib/pq v1.2.0 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
Expand Down
Loading

0 comments on commit 90e2660

Please sign in to comment.