-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ShwetaGhenand/feature/add-migration
feat: postgres database migration
- Loading branch information
Showing
11 changed files
with
1,442 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package server | ||
|
||
import ( | ||
"database/sql" | ||
"embed" | ||
|
||
"github.com/golang-migrate/migrate/v4" | ||
"github.com/golang-migrate/migrate/v4/database/postgres" | ||
"github.com/golang-migrate/migrate/v4/source/iofs" | ||
) | ||
|
||
//go:embed migrator/migrations/*.sql | ||
var fs embed.FS | ||
|
||
const version = 1 | ||
|
||
func validateSchema(db *sql.DB) error { | ||
sourceInstance, err := iofs.New(fs, "migrator/migrations") | ||
if err != nil { | ||
return err | ||
} | ||
targetInstance, err := postgres.WithInstance(db, new(postgres.Config)) | ||
if err != nil { | ||
return err | ||
} | ||
m, err := migrate.NewWithInstance("iofs", sourceInstance, "postgres", targetInstance) | ||
if err != nil { | ||
return err | ||
} | ||
err = m.Migrate(version) // current version | ||
if err != nil && err != migrate.ErrNoChange { | ||
return err | ||
} | ||
return sourceInstance.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FROM migrate/migrate | ||
|
||
COPY /migrations /migrations |
4 changes: 4 additions & 0 deletions
4
cmds/server/migrator/migrations/001_create_users_table.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
BEGIN; | ||
|
||
DROP TABLE users; | ||
COMMIT; |
12 changes: 12 additions & 0 deletions
12
cmds/server/migrator/migrations/001_create_users_table.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
BEGIN; | ||
|
||
CREATE TABLE IF NOT EXISTS users( | ||
id INT PRIMARY KEY NOT NULL, | ||
name TEXT NOT NULL, | ||
email TEXT NOT NULL, | ||
phone TEXT, | ||
age INT, | ||
address TEXT | ||
); | ||
|
||
COMMIT; |
5 changes: 5 additions & 0 deletions
5
cmds/server/migrator/migrations/002_alter_users_table.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE users DROP COLUMN personal_number; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE users | ||
ADD COLUMN personal_number TEXT; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters