Run Evolutions in Command Line

2019-04-22 08:11发布

问题:

It's my first day tinkering with the Play framework, and I'm having a hard time with evolutions. I'm using Play 2.4.

I picked a sample app from the many that come up in activator ui, it uses play-slick and play-slick-evolutions for DB connection and evolutions.

I've perused the docs, but I can't seem to find a way to run the evolutions from the command line. When I run activator on bash I get thrown into a shell, and the help doesn't bring up anything about running evolutions, or slick.

I've been doing PHP for a while, so I'm used to being able to run these up/down from the command line. I can drop the tables from the database client and do activator run which should prompt me to run the evolutions, but I'm looking for the right, manual way to do this. I imagine it's possible since it would need to be done on deploy.

回答1:

As far as I know, they are not designed to be run manually. In development, Play asks you to run them:

In Production I trigger the evolutions by starting my server with the parameter -DapplyEvolutions.default=true. You can write this (without -D of course) also to the application.conf-file to run them always. You can also use only the down- or up-evolutions with -DapplyUpEvolutions.default=true or -DapplyDownEvolutions.default=true.

And of course there is always the option to just copy the part of the script you need manually and apply it with your favorite database tool. However, you then need to tell Play what you did by manually altering the table play_evolutions. An easier solution then would be to not use the evolutions mechanism provided by the Play Framework at all.



回答2:

A quick hack that works for me and doesn't involve bringing up a play instance:

export PGPASSWORD=<password> for SQL_FILE in $(ls <path-to-evolution-files>); do psql -h localhost -U <username> -c "$(sed '1,/Ups/d' <path-to-evolution-files>/$SQL_FILE | sed -n '/Downs/q;p')"; done

Removes everything up to and include Ups and everything after and including Downs, and runs the commands on the Postgres instance.