|
1 | 1 | # Welcome to Database Refactor Workshop
|
| 2 | + |
| 3 | +This workshop is intended to show one more practice for CI and CD, so, this is the chance for learning a new tecnhique for new and legacy systems with a relational database. |
| 4 | + |
| 5 | +## Requeriments |
| 6 | + |
| 7 | +- JDK 1.8+ |
| 8 | +- MySQL 5.5+ |
| 9 | +- A text editor |
| 10 | + |
| 11 | +Simple!!! Right!!! |
| 12 | + |
| 13 | +## Optional requirements |
| 14 | + |
| 15 | +- Git for your convenience and control |
| 16 | +- Groovy for an optimization |
| 17 | +- Gradle 5.x+ for final integration with your systems |
| 18 | + |
| 19 | +## Environment setup |
| 20 | + |
| 21 | +- Download **liquibase** from the [website](https://www.liquibase.org/download). |
| 22 | +- Add to your **PATH**, wherever you want Güin2, Linux, Mac, but only in the terminal |
| 23 | +- Verify your installation: |
| 24 | + |
| 25 | +```shell |
| 26 | +_> liquibase --version |
| 27 | +Starting Liquibase at vie, 12 jun 2020 20:12:54 CDT (version 3.10.0 #10 built at Thu Jun 11 09:47:49 UTC 2020) |
| 28 | +Liquibase Version: 3.10.0 |
| 29 | +Liquibase Community 3.10.0 by Datical |
| 30 | +Running Java under /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre (Version 1.8.0_181) |
| 31 | +``` |
| 32 | +
|
| 33 | +## Database setup |
| 34 | +
|
| 35 | +Well, it's easy: |
| 36 | +
|
| 37 | +- Create a database `makingdevs_demo_db` |
| 38 | +
|
| 39 | +```shell |
| 40 | + mysql -u makingdevs -pmakingdevs -h 0.0.0.0 makingdevs_demo_db < create-schema-mysql.sql |
| 41 | +``` |
| 42 | +
|
| 43 | +## Exercises in this workshop |
| 44 | +
|
| 45 | +1. So we're going to rename a table |
| 46 | + * Rename the `inv` table to `invoice` |
| 47 | + * Rename the `lineitem` table to `line\_item` |
| 48 | + * Rename the `lidetail` table to `line\_item\_detail` |
| 49 | +1. Rename some columns in the `invoice` table |
| 50 | + * Rename `invid` to `id` |
| 51 | + * Rename `invnumber` to `invoice\_number` |
| 52 | + * Rename `datetimecreated` to `date\_created` |
| 53 | +1. We're going to combine two columns using data transformation |
| 54 | + * `invoice.udtime` and `invoice.uddate` should be combined into `invoice.date\_updated` |
| 55 | + * First populate the `date\_created` new column with an `UPDATE` query that merges the `udtime` and `uddate` values |
| 56 | + * HINT: `udtime` + `uddate` |
| 57 | + * What do you think about this refactoring? |
| 58 | + * drop or kept them? |
| 59 | +1. Create tables |
| 60 | + * `contact\_ball\_of\_mud` is too ambitious of a table (or insufficiently coherent). Let's begin splitting it up. |
| 61 | + * The **contact** table should contain name fields, gender, email address, street address, birthday, occupation, and national ID |
| 62 | + * The **security\_info** table should contain password and mother's maiden name |
| 63 | + * The **credit\_card** table should contain credit card type, number, expiration and CVV |
| 64 | + * Choice of data type for each column is left as an exercise for the student. |
| 65 | + * Don't run this refactoring yet! |
| 66 | +1. Tagging and rolling back |
| 67 | + * Tag the database, then run the table rename refactoring written in the previous step |
| 68 | + * Now roll back to continue development on the refactoring |
| 69 | +1. Finish refactoring of `contact\_ball\_of\_mud` |
| 70 | + * Write data transformation code to populate the three tables from their source |
| 71 | + * Remember that `security\_info` and `credit\_card` should have foreign keys to contact. Be sure to add these constraints with the appropriate refactorings |
| 72 | +1. Add a column |
| 73 | + * Add a `full\_name` column to contact |
| 74 | + * Write data transformation SQL to populate it with the three existing name fields combined |
| 75 | + * HINT: `CONCAT_WS()` |
| 76 | + * Don't drop of the source name columns. |
| 77 | +1. Create a trigger |
| 78 | + * Create a file called __contact\_insert.sql__ |
| 79 | + * Write trigger logic to keep `full\_name` up to date with the fields for first name, middle initial, and last name every time a new record is inserted |
| 80 | + * Create a file called *contact\_update.sql* |
| 81 | + * Same logic as the insert trigger |
| 82 | + * Write changeSets that use the sqlFile refactoring to install these triggers. |
| 83 | + * Remember the runOnChange attribute. |
| 84 | + * HINT: be sure the changeSet is idempotent! |
| 85 | +1. Use **gradle**, this is the thing that you should to do... |
| 86 | + * Restart the steps but now with gradle |
| 87 | + * Use the Groovy DSL for the new migrations |
| 88 | +1. Introduce surrogate key |
| 89 | + * Add an auto\-incrementing column to line\_item called id. |
| 90 | + * Add a primary key constraint on id. |
0 commit comments