-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: Oracle db migration (#2015) #2197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Samar1110
wants to merge
13
commits into
gofr-dev:development
Choose a base branch
from
Samar1110:oracleDb_migration
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
555d828
oracle db migration completed
Samar1110 37310af
oracle db migration completed
Samar1110 f6195eb
formated oracle_test.go in migration
Samar1110 89dea5b
Fixed linter issue
Samar1110 871df9d
Fixed linter issue
Samar1110 e40bf12
Fixed code quality issue
Samar1110 bd9687c
Merge branch 'gofr-dev:development' into oracleDb_migration
Samar1110 df77d06
fixed code issues
Samar1110 0b4bab6
Merge branch 'oracleDb_migration' of https://github.com/Samar1110/gof…
Samar1110 de141d8
Merge branch 'development' into oracleDb_migration
Umang01-hash b5435ee
Fix the conflict error
Samar1110 823be58
Fix the conflict error
Samar1110 2a19715
Merge branch 'development' into oracleDb_migration
Samar1110 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,110 @@ | ||
package migration | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"gofr.dev/pkg/gofr/container" | ||
) | ||
|
||
type oracleDS struct { | ||
Oracle | ||
} | ||
|
||
type oracleMigrator struct { | ||
Oracle | ||
migrator | ||
} | ||
|
||
// Provides a wrapper to apply the oracle migrator logic. | ||
func (od oracleDS) apply(m migrator) migrator { | ||
return oracleMigrator{ | ||
Oracle: od.Oracle, | ||
migrator: m, | ||
} | ||
} | ||
|
||
const ( | ||
checkAndCreateOracleMigrationTable = ` | ||
BEGIN | ||
EXECUTE IMMEDIATE 'CREATE TABLE gofr_migrations ( | ||
version NUMBER NOT NULL, | ||
method VARCHAR2(64) NOT NULL, | ||
start_time TIMESTAMP NOT NULL, | ||
duration NUMBER NULL, | ||
PRIMARY KEY (version, method) | ||
)'; | ||
EXCEPTION | ||
WHEN OTHERS THEN | ||
IF SQLCODE != -955 THEN RAISE; END IF; | ||
END; | ||
` | ||
getLastOracleGoFrMigration = ` | ||
SELECT NVL(MAX(version), 0) AS last_migration | ||
FROM gofr_migrations | ||
` | ||
insertOracleGoFrMigrationRow = ` | ||
INSERT INTO gofr_migrations (version, method, start_time, duration) | ||
VALUES (:1, :2, :3, :4) | ||
` | ||
) | ||
|
||
// Create migration table if it doesn't exist. | ||
func (om oracleMigrator) checkAndCreateMigrationTable(_ *container.Container) error { | ||
return om.Oracle.Exec(context.Background(), checkAndCreateOracleMigrationTable) | ||
} | ||
|
||
// Get the last applied migration version. | ||
func (om oracleMigrator) getLastMigration(c *container.Container) int64 { | ||
type LastMigration struct { | ||
LastMigration int64 `db:"last_migration"` | ||
} | ||
|
||
var lastMigrations []LastMigration | ||
|
||
var lastMigration int64 | ||
|
||
err := om.Oracle.Select(context.Background(), &lastMigrations, getLastOracleGoFrMigration) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
if len(lastMigrations) != 0 { | ||
lastMigration = lastMigrations[0].LastMigration | ||
} | ||
|
||
lm2 := om.migrator.getLastMigration(c) | ||
|
||
if lm2 > lastMigration { | ||
return lm2 | ||
} | ||
|
||
return lastMigration | ||
} | ||
|
||
// Begin a new migration transaction. | ||
func (om oracleMigrator) beginTransaction(c *container.Container) transactionData { | ||
td := om.migrator.beginTransaction(c) | ||
c.Debug("OracleDB Migrator begin successfully") | ||
|
||
return td | ||
} | ||
|
||
// Commit the migration. | ||
func (om oracleMigrator) commitMigration(c *container.Container, data transactionData) error { | ||
err := om.Oracle.Exec(context.Background(), insertOracleGoFrMigrationRow, data.MigrationNumber, | ||
"UP", data.StartTime, time.Since(data.StartTime).Milliseconds()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.Debugf("inserted record for migration %v in oracle gofr_migrations table", data.MigrationNumber) | ||
|
||
return om.migrator.commitMigration(c, data) | ||
} | ||
|
||
// Rollback the migration. | ||
func (om oracleMigrator) rollback(c *container.Container, data transactionData) { | ||
om.migrator.rollback(c, data) | ||
c.Fatalf("migration %v failed and rolled back", data.MigrationNumber) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the method
initializeClickHouseRunMocks
we need to mark Oracle as nil else it's tests will fail.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure , I will mark it as nil