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

[feature] Programmatic migrations and/or documentation for it #155

Open
cromefire opened this issue Dec 4, 2020 · 6 comments
Open

[feature] Programmatic migrations and/or documentation for it #155

cromefire opened this issue Dec 4, 2020 · 6 comments

Comments

@cromefire
Copy link
Contributor

Is your feature request related to a problem? Please describe.
There doesn't seem to be an "easy" way to programmatically migrate (At least not documented).

Describe the solution you'd like
Something like:

val config = DbConfig()
config.migrate()

(probably a bit more complex, but at least an easy and documented API)

In that case spitting the core/API from the plugin would probably be nice too.

Describe alternatives you've considered
I've tried to look at the plugin and reproduce it but it looks quite complicated.

Additional context
I'm trying to use it in spring but I'd like to make migrations automatically, because (semi-)manually might not be feasible in many contexts.

@cromefire cromefire changed the title Provide programmatic migrations and/or provide Documentation Provide programmatic migrations and/or provide Documentation for it Dec 4, 2020
@cromefire cromefire changed the title Provide programmatic migrations and/or provide Documentation for it [feature] Programmatic migrations and/or documentation for it Dec 4, 2020
@KenjiOhtsuka
Copy link
Owner

@cromefire
I was so sorry. Too busy to develop harmonica.

@KenjiOhtsuka
Copy link
Owner

@cromefire
Hello, I'm so sorry for the inconvenience.
Are the following documents helpful?
https://improve-future.com/en/spring-boot-with-db-migration.html
https://qiita.com/KenjiOtsuka/items/ff703715bc318509caec
Please feel free to tell me your thoughts. Thank you.

(In addition, I understood what you want.)

@cromefire
Copy link
Contributor Author

I was so sorry. Too busy to develop harmonica.

Well it's OSS, I didn't pay for anything, so there's no need to apologise.

Are the following documents helpful?

I've seen them and it works so far, but because I'm not building an internal application, but a (soon to be) OSS application/"product", I'd like it to be as easy as possible for the user, so if a user just get's the jar or the docker container, he shouldn't have to clone the source code and run the DB upgrade manually. I'd rather like to build something like liquibase provides and automatically migrate at application launch. I can easily do that myself, if I have a way to kick off migrations from within the app.

@KenjiOhtsuka
Copy link
Owner

@cromefire
OK. I will consider it.

Maybe, JarmonicaUpMain.main() would do it as for now.

Arguments

0: migration package. package text ("com.sample.package") which contains your migration class
2: migration directory. migration file directory which contains the migration classes.
3: environment. if you don't set it, "Default" is used.

So, the following new function may meet your needs.

fun migrate() {
    JarmonicaUpMain.main(
        "com.package", "", "directory/path", "Env"
    )
}

@cromefire
Copy link
Contributor Author

I'll have a look at it

@cromefire
Copy link
Contributor Author

So what I came up with as probably working is something like this:

fun migrate(migrations: List<AbstractMigration>, dbConfig: DbConfig, migrationTableName: String = "harmonica_migration") {
    val connection = Connection(dbConfig)
    
    val versionService = VersionService(migrationTableName)
    
    try {
        connection.transaction {
            versionService.setupHarmonicaMigrationTable(connection)
        }
        for (migration in migrations) {
            val migrationVersion =
                versionService.pickUpVersionFromClassName(migration.javaClass.name)
            if (versionService.isVersionMigrated(connection, migrationVersion)) continue

            connection.transaction {
                migration.connection = connection
                migration.up()
                versionService.saveVersion(connection, migrationVersion)
            }
        }
        connection.close()
    } catch (e: Exception) {
        connection.close()
        throw e
    }
}

You can't implement it externally, because everything on the VersionService and AbstractMigration.connection is internal, but it should work if added inside the library.

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

No branches or pull requests

2 participants