Table of Contents

Database Upgrade Scripts Process

Colm Carew 2018/05/17 08:11

The scripts are located in each project that the scripts need to be run for i.e. Snmp Manager upgrade scripts are in the Snmp Manager codebase

Presentation on this : https://docs.google.com/presentation/d/1Yv7KJqZBW4f90VWs9tpXdU1xn6j4hwy9cGZzeNlGnWY/edit#slide=id.p4

SQL File Checklist

Naming Convention

Where are they located

It depends on the project for Grails 2 and 3 they are in :

grails-app/conf/db/migration

For Spring Boot apps they are in :

src/main/resources/db/migration

How do I know what number to take

Dos and Don'ts

Stored Procedure Example

/* For Customer Specific Inserts/Updates we need to use a Stored Procedure*/
DELIMITER //
DROP PROCEDURE IF EXISTS load_of_dummy_inserts //
CREATE PROCEDURE load_of_dummy_inserts()

BEGIN
 SELECT @customerName := name from errigal_customer limit 1;
 IF @customerName = 'EXT' THEN
 CREATE TABLE if not exists colm_test (
   id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
   firstname VARCHAR(30) NOT NULL,
   lastname VARCHAR(30) NOT NULL
);
   INSERT INTO colm_test(firstname, lastname) values('Colm', 'Carew');
 END IF;
END; //

CALL load_of_dummy_inserts() //
DROP PROCEDURE IF EXISTS load_of_dummy_inserts //
DELIMITER ;

Create Customer Table

CREATE TABLE IF NOT EXISTS errigal_customer (
  name VARCHAR(255) NOT NULL
);

INSERT INTO errigal_customer(name) values(CUST_ABBREVIATION);

Current Customer's list per server environment

ServerEnv Customer (errigal_customer.name)
atcqa ATC
atcprod ATC
nvqa ATC
ccprod CC
errigalqa CC
qa CC
idmsqa EXT
sfqa EXT
extqa EXT
extprod EXT
scottypro SCOTTYPRO
  1. run the app with ddl_auto set to update
  2. export the DDL for newly created tables (or extract the new columns for ALTER commands)
  3. make sure the tables/constraints order is ok in the script (e.g. not having the constraint for a table before creating it)
  4. for any apps that are using table based sequences (e.g. fiber, the tables usually have the _SEQ sufix), provide inserts for the sequence table (look at V4__IDMS-2805_Demarcation.sql for more inspiration)
  5. prepare your Flyway script (it is also recommended to create a rollback script for further steps)
  6. stop the application
  7. rollback the changes (possibly using the rollback script created above)
  8. put your Flyway script into the right location of the project (documented in the sections above)
  9. run the app again, verify logs for Flyway messages (upgrading the schema) and possibly test the new tables via application
  10. repeat until it all works

Rules for the scripts

These rules are elaboration of what has been mentioned Dos and Don'ts above. The main goal of this is to make sure the changes are backwards compatible so we can rollback the application without having to rollback the DB.

Deprecating the fields or tables

If you think some of the fields/tables has to be changed or dropped, they should be handled us mentioned above. Additionally we would need to mark these fields as deprecated somewhere (there is no current example of that yet, but the best place seems to be the README.md in each project). The record could have following structure:

Add a column to a table

Always make sure the column does not already exists first

DELIMITER //
DROP PROCEDURE IF EXISTS add_column //
CREATE PROCEDURE add_column() 

BEGIN
	DECLARE colName TEXT;
	SELECT column_name INTO colName
	FROM information_schema.columns 
	WHERE table_schema = 'the_database'
	    AND table_name = 'the_table'
	AND column_name = 'column_to_add';

	IF colName is null THEN 
	    ALTER TABLE the_table ADD COLUMN the_column THE_TYPE;
	END IF; 
END//

CALL add_column() //

DROP PROCEDURE IF EXISTS add_column //
DELIMITER ;

Drop a column from a table

Only do this if the field has not been used for 2 or more releases ago

DELIMITER //
DROP PROCEDURE IF EXISTS remove_column //
CREATE PROCEDURE remove_column() 

BEGIN
	DECLARE colName TEXT;
	SELECT column_name INTO colName
	FROM information_schema.columns 
	WHERE table_schema = 'the_database'
	    AND table_name = 'the_table'
	AND column_name = 'column_to_drop';

	IF colName is not null THEN 
	    ALTER TABLE the_table DROP COLUMN the_column;
	END IF; 
END//

CALL remove_column() //

DROP PROCEDURE IF EXISTS remove_column //
DELIMITER ;

Generate INSERTS from Workbench

In MySQL workbench you can export an insert statement, which gives you the full column order for that table if you are unsure of your insert statement. To get this select a row from a table for example:

select * from ticketer.ticket where id = 10;

In the results you will see above the row a save icon with the name Export/Import as show below: Click this and select the Format of SQL INSERT statements to save the sql export as:

:!: Open the exported file and remove the smart quotes around column names and also remove the id column and its associated value. Set the version number to 0 for the insert you are creating and you should have a correctly ordered and formatted insert statement to use for the table you exported.:!:

Troubleshooting

To troubleshoot the Flyway scripts - make sure to understand how it works by reading How Flyway works - it will great help you fix the problems. Flyway logs it's initialisation with following logs, so that is the first place to look at when something doesn't work as expected:

2018-09-14 11:07:42.427  INFO 74355 --- [  restartedMain] o.f.core.internal.util.VersionPrinter    : Flyway 4.2.0 by Boxfuse
2018-09-14 11:07:42.782  INFO 74355 --- [  restartedMain] o.f.c.i.dbsupport.DbSupportFactory       : Database: jdbc:mysql://localhost:3306/fiber?useUnicode=yes&characterEncoding=UTF-8 (MySQL 5.1)
2018-09-14 11:07:42.861  INFO 74355 --- [  restartedMain] o.f.core.internal.command.DbValidate     : Successfully validated 4 migrations (execution time 00:00.023s)
2018-09-14 11:07:42.881  INFO 74355 --- [  restartedMain] o.f.core.internal.command.DbMigrate      : Current version of schema `fiber`: 4
2018-09-14 11:07:42.882  INFO 74355 --- [  restartedMain] o.f.core.internal.command.DbMigrate      : Schema `fiber` is up to date. No migration necessary.

Flyway allows the application to run in the rollback scenario - e.g. if the DB is version 4, but the app has just version 3 it will run anyway. Flyway will fail to start in following situations: