I'm working on a project using Scala which uses Slick.io for handling database interactions. I have the database schema implemented via Slick using its DDL.
This all works fine when I was starting out but now that I am some way into the project, making changes to the database schema is becoming a pain. I have to drop all the tables, manage data, re-create the schema etc...
I've previously used the PHP framework Laravel which had a nice way of dealing with database migrations & making changes to databases. This was done via a set of classes that were controlled by a timestamp and the "most recent" migrations could be run to make any changes to a database schema.
Does Slick have any equivalent?
Thanks
No, slick itself does not have an equivalent, but you can use something like:
Flyway:
https://github.com/flyway/flyway
http://flywaydb.org/
As you have not given any additional info:
for the play-framework there is an own module, which can be found here: https://github.com/flyway/flyway-play
Related
I designed a database in MYSQL. Now I want to connect that with my Laravel application. But after researching, I understand that I need to migrate the database into Laravel by creating migration tables. But doing the same thing that I did in MySQL is tedious. I think there is another way to do the stuff. After searching I got this article from stackoverflow, but this is related to yii framework. I think someone knows here the solution of my problem.
I don't see a problem here, you can just connect Laravel to your DB (by editing your .env file) without any problems, migration files are just an easier way to design and implement your tables (also altering your tables' scheme on production is a very useful usage for migrations), but with that being done then no further actions are required, and you are good to go!
it's pretty obvious, you can update the database setting in the .env file and use DB::table('your_table_name') and get whatever query you want.
The upside of using migrations is that the exact same database can be created on different systems (ex: co-workers or acceptance/production servers).
Laravel migrations have an up and a down function so you can rollback to a specific version if something went wrong, very usefull when publishing your code.
That being said, co-workers could also review the database changes and for example hint at using more indexes and foreign keys.
As the other comments said, it's not a requirement you use migrations but it has considerable advantages versus creating and updating the database manually.
Is there any way to create schema through liquibase script ?
After some research I've found out that we need to include a create schema in change log file.
I'm thinking on similar terms for question asked previously for postgres
Any pointers or help is welcomed.
Found an answer that was mentioned already on stackoverflow.
We can create a fresh database using this URL.
jdbc:mysql://localhost:3306/database_name?createDatabaseIfNotExist=true
createDatabaseIfNotExist this keyword create a fresh new database in your system. If the database does not exist. if exist, skip executing.
Your proposed solution to your own question is to separate stuff by database, while your original question was about how to separate stuff using schema. Those are not the same.
If you are using Liquibase in "embedded mode", meaning it is your application code which controls Liquibase execution, not some Maven cmd or Liquibase CLI, then you can have a look at Pre-Liquibase.
Pre-Liquibase attempts to solve the chicken-and-egg problem inherent to Liquibase: It cannot be used to setup its own "home" (Liquibase needs two tables of its own that has to live somewhere), nor can Liquibase ChangeSets be used to create databases or schemas.
This is the problem which Pre-Liquibase solves. It executes some SQL prior to Liquibase itself. For example, you can use it with MySQL to make sure the database exists without fiddling with URL. In such case your Pre-Liquibase SQL script file would look like this:
CREATE DATABASE IF NOT EXISTS my_database_name
You can use Pre-Liquibase if you are using Spring Boot or just Spring Framework without Spring Boot. However, feel free to steal the ideas if you have another tech stack.
(full disclosure: I'm the author of Pre-Libuibase)
I created some tables using rails. Now I want to modify the structure of a few of them. I know it can be done using rails migration. But i was wondering if it would cause any anomaly in the rails app if I modify the schemas using mysql rdbms?
Doing such changes through a migration has the advantage of not losing the changes if you decide to recreate/remigrate the database.
Also it serves as documentation. Imagine if your coworker altered some tables sneakily (and then you both forgot about it).
Technically, updating schemas directly in the database should work, but don't do it.
To add to Sergio's point, you're missing a simple fact - Rails' migrations create the famous db/schema.rb file - from which your migrations pull all their data.
The importance of schema.rb is overlooked - it is one of the most crucial aspects of your application.
db/schema.rb
The schema gives all your migrations a version of your DB to change / add to. Each time you perform a migration, Rails changes the schema file to ensure it has a "blueprint" of your db stored on file.
The schema is then able to rebuild the database using such methods as rake db:schema:load (ONLY RECOMMENDED FOR NEW INSTALLS -- DELETES PREVIOUS DATA)
So whilst there's no problem setting up the db using the db's own native tools, I recommend against it. You need to keep your migrations up to speed so that Rails can build the appropriate tables from its schema.
I tried using https://github.com/building5/sails-db-migrate module for creating migrations but it did not run correctly. Table used to get generated but few columns were missing.
Is there a better way to create and run migrations? Sorry i am new to sails, recently migrated from Php.
So, it turn out i was stupid to think we need a module to write migration in sails. It turn out on sails lift the application automatically creates the table from attributes of the model.
But on trying that i realized the waterline module which takes care of this does not create for foreign-key constraints as of now.They are still working on it.
If you are using a nosql db then you can use the associations
http://sailsjs.org/#!/documentation/concepts/ORM
Time is pass, but nothing changes with sails. Beware use it.
If you already in this trap - try https://www.npmjs.com/package/sails-migrations
So people will get a clean and correct answer: The sails ORM (waterline) already supports auto migration, you can change this in the config file (/config/models.js):
module.exports.models = {
migrate: 'alter'
};
It accepts the following options:
safe - never auto-migrate my database(s). I will do it myself (by hand)
alter - auto-migrate, but attempt to keep my existing data (experimental)
drop - wipe/drop ALL my data and rebuild models every time I lift Sails
http://sailsjs.org/#!/documentation/concepts/ORM/model-settings.html
I learned play by following the tutorial on their website for building a little blogging engine.
It uses JPA and in it's bootstrap calls Fixtures.Deletemodels(), (or something like that).
It basically nukes all the tables every time it runs and I lose all the data.
I've deployed a production system like (sans the nuke statement).
Now I need to deploy a large update to the production system. Lots of classes have changed, been added, and been removed. In my testing locally, without nuking the tables on every run I ran into sync issues. When I would try to write or read from tables play would throw errors. I opened up mysql and sure enough the tables had only been partially modified and modified incorrectly in some cases. Even if I have the DDL mode set to "create" in my config JPA can't seem to "figure out" how to reconcile the changes and modify my schema accordingly.
So I have to put back in the bootstrap statement that nukes all my tables.
So I started looking into database evolutions within Play and read an article on the play framework website about database evolutions. The article talked about version scripts, but it said, "If you work with JPA, Hibernate can handle database evolutions for you automatically. Evolutions are useful if you don’t use JPA".
So if JPA is supposed to be taking care of this for me, how do I deploy large updates to a large Play app? So far JPA has not been able to make the schema changes correctly and the app will throw errors. I'm not interested in losing all my data so the fix on dev "Fixtures.deleteModels()" can't really be used in prod.
Thanks in advance,
Josh
No, JPA should not take care of it for you. It's not a magic tool. If you decide to rename the table "client" to "customer", the column "street" to "line1" and to switch the values of the customer type column from 1, 2, 3 to "bronze", "silver", "gold", there is no way for JPA to read in your mind and figure all the changes to do automagically.
To migrate from one schema to another, you use the same tools as if you didn't use JPA: SQL scripts, or more adavanced schema and data migration tools, or even custom migration JDBC code.
Have a look at flyway. You may trigger database migrations from your code or maven.
There is a property called hbm2ddl.auto=update which will update your schema.
I would STRONGLY suggest to not use this setting in production as it introduces a whole new level of problems if something goes wrong.
It's perfectly fine for development though.
When a JPA container starts (say, EclipseLink or any other), it expects to find a database which matches #Entity classes you've defined in your code. If the database has been migrated already, everything will work smoothly; otherwise: probably it will fail.
So, long story short, you need to perform database migrations (or evolutions, if you prefer) before the JPA container starts. Apparently, Play performs migrations for you, before Play kicks off the database manager you configured. So, in theory, regardless the ORM you are using, Play decides when it's time for the ORM to start its work. So, conceptually it should work.
For a good presentation about this subject, please have a look at the second video at: http://flywaydb.org/documentation/videos.html