Skip to content

Conversation

jjchen01
Copy link
Collaborator

  • Explicit sql files for migration
  • Upgrade sequentially
  • Select for update to ensure only one instance is updating the schema.

func NewSchemaVersion() SchemaVersion {
sv := SchemaVersion{
Migrations: []int64{
2,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to update the sequence file manually.

}

if err := tx.WithContext(ctx).
Raw("SELECT id, version FROM database_migration WHERE id = ? FOR UPDATE", m.ID).
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Select for update to acquire a lock among instances.


if reset {
// Still experiencing a race condition here, need to consult with DevOps regarding deployment strategy.
if err := db.Exec("DROP SCHEMA IF EXISTS public CASCADE;").Error; err != nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

race condition

continue
}
// get version sql file
sqlFile := filepath.Join(migrationSqlFolder, fmt.Sprintf("%d.sql", migrationVersion))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If migrationVersion is 1, retrieve 1.sql.

}

if reset {
// Still experiencing a race condition here, need to consult with DevOps regarding deployment strategy.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove the logic there -))
...
we can run it manually on first steps
Don't risk our prod data

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reset will be removed in the next release.

fileContentAsString := string(content)
sqlCommands := strings.Split(fileContentAsString, ";")
for _, command := range sqlCommands {
db.Exec(command)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if one command failure, chains of errors happen.
can we log and throw error immediately.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, there is no error returned in the exec function, and we should test it before releasing the new schema.

0,
},
}
slices.Sort(sv.Migrations)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just add the name like
"0001_v01.0.sql"
"0002.sql"
and use it without sorting
men -)) we hard code but we sort it again make me feel unsafe

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorting ensures the correct sequence, in case code conflicts prevent us from adding elements back in order. If you're uncomfortable with it, we can remove it and hope we add them correctly each time.

Copy link
Contributor

@locnguyen1986 locnguyen1986 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) few comments, feel free to merge after address that

@jjchen01 jjchen01 merged commit 357a046 into main Sep 22, 2025
1 check passed
@jjchen01 jjchen01 deleted the feat/migration-version branch September 22, 2025 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants