-
Notifications
You must be signed in to change notification settings - Fork 694
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
Database Migrations #165
Comments
I guess it's a little bit out of the scope of Exposed as orm/dao framework. We have functionality to automatically add columns and indices (and log differences which can't be resolved). If you have a vision how we can integrate Flyaway (sorry, but I never used it) please share or PR. |
Yes, I'm agree database migration tool is really important to have. I have developing notification service which is separate from website (not jvm and kotlin) and I want to use Exposed with existing database, so how I suppose to mange my model? Create classes manually? Not efficient. We need such a tool which is automatically generate table classes code from existing database. |
@Tapac This is what I had in mind The basic version would do the following given the list of models to be managed, while at the same time storing the current version identifier in the database:
This way, at some point in future if one desires to go to a particular database state, one can do so by just providing an identifier of the revision or by calling downgrade until one gets to the desired state |
I agree that database migration and version control tools is very important, especially when the java options are really old-school style, needing you to write SQL, XML, etc. If you guys were thinking on developing something like Django migrations inside Exposed, you can count on me for any help. |
@shrpereira I am really interested in developing something like this. I am currently working with Jooq and Flyway. It is working, but as you said it is old-school. The only thing stopping me from using Exposed in production is the lack of db migration tool. |
Even though I really would not know where to begin (except for checking out the Django library), I would love to help out with this. Sounds awesome and a great addition to the library. Please let me know if I can help. ps. I am a former Django dev. |
I've been developing such a migration tool. https://github.com/KenjiOhtsuka/harmonica
|
If you guys are going to develop such a tool, please take a look at other frameworks to see how other people have implemented this, a good example is Laravel Migration. @KenjiOhtsuka keep up the good work, it's really cool. other JVM based migration tools |
I'd much prefer the way that Android does migrations with Rooms to Laravel's migrations. Laravel relies upon file names following a specific format so that they can iterate over migrations files, derive class names from the file names, and then dynamically call methods they expect to be there with no interface contract (I had to basically re-implement Laravel migrations on a project using Eloquent without Laravel). Rooms, on the other hand, is very safe, highly testable, and extremely easy to follow and implement. |
@Tapac , Are there any updates on this topic? Also, How can I alter-tables? I have defined a table
Right know I am using @KenjiOhtsuka 's Harmonica to write a migration file. Now I want to alter the table User, for example by adding a few columns and dropping some others. How can I achieve this? |
@Wnzebkhan , there is no updates or plans to implement it in the near future, only if someone will provide a PR. |
I'm Symfony developer and I'd like to write in Kotlin/Java. But realy, php frameworks has perfect migration scheme. For Example, DoctrineMigration. Its very reliable and convenient tool for migrations. And we have DoctrineFixture for data filling. And we have Doctrine - it UnitOfWork/DataMapper ORM like Hibernate. Java/Kotlin - where are you? Unfortunately. |
@Maximilian73 , there are DB migration tools in Java too, e.g. https://flywaydb.org/ , https://www.liquibase.org/ . But Exposed doesn't cover that part of a development process only DSL/ORM part. |
I know about flyway and liquibase, but even liquibase (on my opinion) much weaker than doctrine migrations. And I need in EntityManager for my project. That's why I will have to use Jooby and Hibernate... And probably Liquibase and Guice... |
@Tapac I might create this and PR it, from a functionality perspective I suppose we would simply need:
Any other ideas? |
@CharlieTap , it would be great if you could make a PR with a migration feature. |
@CharlieTap , exactly as @Tapac said " The main problem is how to track DSL versioning and build diff between versions." Currently, exposed has |
@Tapac @Kabbura By versioning I guess you mean, if a new version of the library comes out then its important old migrations work? But I'm not sure how that's different from any piece of code? My current plan is to:
Some clever ideas for potential future extensions:
|
Any updates on this? What's the recommended way to handle migrations with Exposed? |
I got some way into a POC but ultimately had to stop due to other commitments :( |
Any thoughts @Tapac? How do you recommend doing migrations? This is a critical feature for putting Exposed in production. |
This blog post demonstrates how to use Flyway as migration tool with Exposed. Basically you use Flyway for creating tables and migrations, don't use the one by Exposed. No integration, you have to manually make sure the Exposed table definition matches the one created by Flyway. FYI |
Hey @yannikyeo which blog post are you talking about? |
Sorry here's the link https://www.thebookofjoel.com/kotlin-ktor-exposed-postgres |
I couldn't get harmonica running without gradle (I use maven), so I started out to develop a very basic migration tool just for my personal needs. |
Just dropping a note here as I was discussing this with some co-workers. What may be a better use of time is to have a compile step that generates table classes based on liquibase/flyway changesets. This is done with other ORMs via maven/gradle plugins. I agree with the sentiment that having to manually line up your migration schema and exposed is error prone. But adding migration functionality to exposed is major scope creep. I'd rather have some glue plugins to bridge the gap between a mature migration library and exposed. |
@cha55son, we are working on gradle-plugin which will at least generate Table/Entity classes from an existing database scheme and I thinkg it will be possible to run it against SQL scripts. |
@Tapac Is there any update on the state of the plugin? |
@DuchGhast , yes, the 0.1 version is almost ready to go public. Need to fix documentation and a plugin publication flow. I will share the project link in that issue. |
@Tapac do you mean this plugin? |
Conceptually related question: since Exposed does not maintain the schema, why are there so many functions to describe it? How does it actually benefit from knowing that a column is indexed by an index of a certain name for example? |
So how exactly do I make sure my flyway migrations match Exposed mappings? Hibernate has I'm in for a workaround like writing insert+fetch unit tests to make sure columns are in sync, but maybe someone has a better idea |
This works for me as a workaround for schema validation: I run this test before each application run (I run backend app by shell script) and as a pre-commit hook, so I can never commit non-matching schema, if my flyway migrations do not match exposed tables. Just make sure to create a throwaway database when in unit test mode, as I did. |
@Tapac is the plugin still coming? |
@Tapac also interested in the update here, would love to use Exposed but db migrations are a hard requirement for me |
I have written a tutorial on how to set up Exposed with Flyway and how to use the Gradle plugin to generate Exposed table definitions: https://bettercoding.dev/kotlin/tutorial-exposed-generation-flyway/ Hope this helps some of you :) |
maybe for someone this repo will be of some use https://github.com/raharrison/kotlin-ktor-exposed-starter |
I added some features and updated and improved the code a little bit here: https://github.com/Suwayomi/exposed-migrations The library is used in my Javalin web app My opinion might be biased but this library is much more flexible and lighter than harmonica as it doesn't use Exposed internals. |
One of the ways that has worked for me is using a combination of Exposed and Flyway by utilizing the
|
Yes! I would love to see a light-weight migration tool build-in. I would love to write migrations in Kotlin. Please make it possible without workarounds and 3rd-party libs. |
For anyone coming across this in 2024, it looks like this exists now: https://github.com/JetBrains/Exposed/tree/main/exposed-migration |
Is there plan to have database migrations in this great tool?
It can either be inbuilt or integrate with a tool like Flyway.
The text was updated successfully, but these errors were encountered: