Skip to content
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

Add db migrations #217

Merged
merged 6 commits into from
Apr 14, 2023
Merged

Add db migrations #217

merged 6 commits into from
Apr 14, 2023

Conversation

vminkobin
Copy link
Contributor

@vminkobin vminkobin commented Apr 13, 2023

Library for migrations code from this pr https://github.com/trustwallet/backend/pull/2331

Additionally added method which reads migration params from env vars and runs migrations, so it can be used in services like

package main

import (

	"github.com/trustwallet/go-libs/database"

	"github.com/sirupsen/logrus"
)

func main() {
	log := logrus.New()
	if err := database.RunFromEnv(log); err != nil {
		log.Errorf("run migrations: %v", err)
	}
}

@vminkobin vminkobin marked this pull request as ready for review April 13, 2023 16:00
@vminkobin vminkobin requested a review from tw-dmy April 13, 2023 16:00
@vminkobin vminkobin requested review from covain and iCell April 13, 2023 16:07
iCell
iCell previously approved these changes Apr 14, 2023
runner.logger = &noopLogger{}
}

mgr, err := migrate.NewWithDatabaseInstance(
Copy link
Contributor

Choose a reason for hiding this comment

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

we can simplify this a bit, and use migrate.New("file://...", "connection string") instead of initializing db connection with sql.Open and then passing it no NewWithDatabaseInstance.

Comment on lines 81 to 87
if runner.filesDir == "" {
runner.filesDir = defaultFilesDir
}

if runner.logger == nil {
runner.logger = &noopLogger{}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

why not setting these fields when initializing MigrationRunner? i.e.

runner := &MigrationRunner{
    filesDir: defaultFilesDir,
    logger:  &noopLogger{},
}


// Constants representing environment variable keys for migration configuration.
const (
envKeyPrefix = "MIGRATION"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to have duplicated prefixes in the constants below ("MIGRATION_DSN", "MIGRATION_OPERATION"), rather than having a method to concatenate them. It will be much more readable, and easier to search where an env var is used, i.e. when you see an env definition in a deployment config, and would like to know in which package it's used. If env vars defined as a full name, you can just search across all repos and find it.

return fmt.Errorf("missing env: %s", envKey(envKeyOp))
}

forceVersion, err := readForceVersion()
Copy link
Contributor

Choose a reason for hiding this comment

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

[minor] this is required only for "force" operation, in other cases it's just ignored.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes and error will occur here only when related env var is set

@vminkobin
Copy link
Contributor Author

@tw-dmy thanks for the comments, all resolved

@vminkobin vminkobin requested review from tw-dmy and iCell April 14, 2023 14:42
@vminkobin vminkobin merged commit b8a3539 into master Apr 14, 2023
@vminkobin vminkobin deleted the db-migrations branch April 14, 2023 17:39
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.

None yet

3 participants