28
#sqlinthecity David Atkinson Product Manager, Red Gate [email protected] Automated Build and Test (or continuous integration) (for your database)

Database automated build and test - SQL In The City Cambridge

Embed Size (px)

DESCRIPTION

Automated Database Build and Test. Presentation from David Atkinson at SQL In The City Cambridge.

Citation preview

Page 1: Database automated build and test - SQL In The City Cambridge

#sqlinthecity

David AtkinsonProduct Manager, Red Gate

[email protected]

Automated Build and Test(or continuous integration)

(for your database)

Page 2: Database automated build and test - SQL In The City Cambridge

What will we cover today?

Database build automation:What, Why, How

Practical Demo

Questions encouraged!

#sqlinthecity

Page 3: Database automated build and test - SQL In The City Cambridge

Show of hands: How agile is your database development?

1. Database objects/scripts aren’t in version control

2. Database objects/scripts are in version control

3. An automated build system exists to build and test the database

4. An automated release system is used to promote your database to your environments

#sqlinthecity

Page 4: Database automated build and test - SQL In The City Cambridge

1. Database objects/scripts aren’t in version control

2. Database objects/scripts are in version control

3. An automated build system exists to build and test the database

4. An automated release system is used to promote your database to your environments

How agile is your database development?

#sqlinthecity

Page 5: Database automated build and test - SQL In The City Cambridge

“Continuous Integration is a practice designed to ensure that your software is

always working, and that you get comprehensive feedback in a few minutes as to whether any given change to your system

has broken it.”Jez Humble, ThoughtWorks,

author of “Continuous Delivery”

#sqlinthecity

What is continuous integration?

Page 6: Database automated build and test - SQL In The City Cambridge

“Database Continuous Integration is a practice designed to ensure that your

database software is always working, and that you get comprehensive feedback in a

few minutes as to whether any given change to your system has broken it.”

What is continuous integration?

#sqlinthecitydatabase

^

Page 7: Database automated build and test - SQL In The City Cambridge

Database Build Automation#sqlinthecity

Page 8: Database automated build and test - SQL In The City Cambridge

What is build?

• For application code = compile• For database code = database creation

script – But only for a new installation!

• Upgrade scripts required for existing installations– Need to preserve the state of the data

#sqlinthecity

Page 9: Database automated build and test - SQL In The City Cambridge

What is test?

• For .NET code, NUnit.– Runs on a developer’s machine and

build server• What about the database?– tSQLt is an open source framework for

testing SQL Server databases– SQL Test provides SSMS integration

#sqlinthecity

Page 10: Database automated build and test - SQL In The City Cambridge

Demo background

Two fictional developers, David and Grant, are working on the website, www.simple-talk.com, a community website for .NET and SQL Server developers

The application comprises• An ASP.Net web application• A SQL Server database

#sqlinthecity

Page 11: Database automated build and test - SQL In The City Cambridge

Demo background

Continuous integration has been configured to:

1. Keep a test database up to date with the latest changes

2. Run automated tSQLt tests(on realistic amounts of data)

3. Generate up-to-date documentation4. Generate deployment scripts

#sqlinthecity

Page 12: Database automated build and test - SQL In The City Cambridge

Demo background

A few improvements are being made to Simple Talk.

These changes will be deployed to production.

But… only when the dev team has demonstrated that the changes work!

#sqlinthecity

Page 13: Database automated build and test - SQL In The City Cambridge

Tools used• TeamCity (CI tool from JetBrains)• SQL Automation Pack (includes TeamCity

plugin)

• Glimpse (open source)

From the SQL Developer Bundle:• SQL Source Control• SQL Test (tSQLt unit testing framework)• SQL Data Generator• SQL Doc

#sqlinthecity

Page 14: Database automated build and test - SQL In The City Cambridge

Demo

#sqlinthecity

Page 15: Database automated build and test - SQL In The City Cambridge

Custom Migration ScriptsAn introduction

#sqlinthecity

Page 16: Database automated build and test - SQL In The City Cambridge

Generating a deployment script

1

12

#sqlinthecity

Page 17: Database automated build and test - SQL In The City Cambridge

How changes are made

1 2 3 4 5 6

modify procedure

create table

drop view

create trigger

delete column

#sqlinthecity

Page 18: Database automated build and test - SQL In The City Cambridge

1 2 3 4 5 6

modify procedure

create table

drop view

create trigger

delete column

7 8 9 10 11 12

modify trigger

create view

create procedure

create function

drop procedure

modify function

How changes are made#sqlinthecity

Page 19: Database automated build and test - SQL In The City Cambridge

Infer deployment script

How SQL Compare sees the world

1

12

#sqlinthecity

Page 20: Database automated build and test - SQL In The City Cambridge

But not all changes can be inferred

1 2 3 4 5 6

7 8 9 10 11 12

table rename

column rename add NOT NULL

column without DEFAULT

split table

merge table

data transformation

data motion

merge columnsplit column

#sqlinthecity

Page 21: Database automated build and test - SQL In The City Cambridge

SQL Compare and SQL Source Control support custom migration scripts

1 2 3 4 5 6

7 8 9 10 11 12

Custom migration script

#sqlinthecity

Page 22: Database automated build and test - SQL In The City Cambridge

Deployment script with migration scripts

1

12

#sqlinthecity

Page 23: Database automated build and test - SQL In The City Cambridge

SQL Compare

1

12

1’

Migration scripts

#sqlinthecity

Page 24: Database automated build and test - SQL In The City Cambridge

Demo

#sqlinthecity

Page 25: Database automated build and test - SQL In The City Cambridge

Recap: database CI

1. Incrementally keeps a test database up to date

2. Runs tSQLt tests on a database with realistic amounts of data

3. Generates always-up-to-date documentation

4. Generates and validates deployment scripts

5. Publishes a Deployment Manager database package

#sqlinthecity

Page 26: Database automated build and test - SQL In The City Cambridge

Recap of demo: writing a test

1. A new test is added2. Continuous Integration identifies a failure3. We reproduce the bug locally4. We fix the bug and test it5. We commit the fix to source control

#sqlinthecity

Page 27: Database automated build and test - SQL In The City Cambridge

Recap: migration scripts

1. The fix sets the [PublishDate] column from NULL to NOT NULL

2. Grant’s “get latest” fails because existing data that has NULL values

3. The continuous integration build alerts us of the same issue

4. A migration script is added to UPDATE existing data prior to changing the column to NOT NULL

5. Get Latest becomes possible and the continuous integration build once again indicates working software

#sqlinthecity

Page 28: Database automated build and test - SQL In The City Cambridge

Latest version at :www.red-gate.com/CI

Questions?

#sqlinthecity

[email protected]