Database migration tools can be very helpfull when dealing with the deployment of database schema changes. The best way to learn how to do schema migrations it to practice it!
In this task you will work with the same old ShopDB
database for online store. The development team started using the liquibase for database schema migrations allready. The database changelog is stored in the file task.sql
. The initial version of the database (labeled 0.0.1
) has the following tables:
Countries
, which has the following columns:ID
,Name
.Products
, which has the following columns:ID
,Name
.Warehouses
, which has the following columns:ID
,Name
,Address
,CountryID
ProductInventory
, which has the following columns:ID
,ProductID
,WarehouseAmount
andWarehouseID
.
- Install and configure a MySQL database server on a Virtual Machine, connect to it with the MySQL client.
- Fork this repository.
- Install Docker on your computer
In this task, you need to develop 2 new changesets for the ShopDB
database changelog:
- The first changeset should create a table, called
Users
, which has the following columns:ID
,FirstName
,LastName
,Email
. The changeset should have a new unique ID (just as the rest of the already existing changesets in the changelog), and it should have label0.0.2
. - The second changeset should create an index, called
Email
for theEmail
field in theUsers
table. This changeset should have label0.0.3
.
Both changesets should have rollback procedure defined.
Just in case you want to test your script on your database before submitting a pull request, you can do it by performing the following actions:
- Drop the
ShopDB
database using "DROP DATABASE ShopDB;" statement if you already have it on your database server. - Create an empty database, called
ShopDB
on your database server. - Run the intial schema migration for the
ShopDB
database with liquibase using docker on your computer:
docker run -v $(pwd):/repos --workdir /repos/ -e INSTALL_MYSQL=true \ -e LIQUIBASE_COMMAND_USERNAME=<db username> \ -e LIQUIBASE_COMMAND_PASSWORD=<db password> \ -e LIQUIBASE_COMMAND_URL=jdbc:mysql://<db host>:3306/ShopDB \ liquibase/liquibase liquibase update --labels="0.0.1
Make sure to run the command in the folder, where repository is clonned. If you are running the script on Windows - replace $(pwd)
with the full path of the clonned reposotory.
Replace , and with values for your database server before running the command.
4. Tag the database with the initial version, so you will be able to rollback any new changesets:
docker run -v $(pwd):/repos --workdir /repos/ -e INSTALL_MYSQL=true \ -e LIQUIBASE_COMMAND_USERNAME=<db username> \ -e LIQUIBASE_COMMAND_PASSWORD=<db password> \ -e LIQUIBASE_COMMAND_URL=jdbc:mysql://<db host>:3306/ShopDB \ liquibase/liquibase liquibase tag 0.0.1
5. Use commands, described in steps 3 and 4 to update the database to versions 0.0.2
and 0.0.3
you will create while workin on this task.
6. Test rollback of the changeset by reverting the state of the database to a tag:
docker run -v $(pwd):/repos --workdir /repos/ -e INSTALL_MYSQL=true \ -e LIQUIBASE_COMMAND_USERNAME=<db username> \ -e LIQUIBASE_COMMAND_PASSWORD=<db password> \ -e LIQUIBASE_COMMAND_URL=jdbc:mysql://<db host>:3306/ShopDB \ liquibase/liquibase liquibase rollback <tag name>
Replace with the version of tag you want to rollback to (ex. 0.0.2
or 0.0.1
)