#sqlinthecity
Automated database build and
test with TeamCity
SITC Workshop Cambridge, UK
What is CI?
• Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early.
#sqlinthecity
What is database CI?
• It’s the same!
• Applying the same approach to the database as we do for the application
#sqlinthecity
The road to database CI
1. Source control
2. Automated builds
3. Automated tests
4. Frequent check ins of small changes
#sqlinthecity
The road to database CI
1. Source control
2. Automated builds
3. Automated tests
4. Frequent check ins of small changes
#sqlinthecity
Advantages of Automated Builds
• Early warning of broken code
• Constant availability of a "current" build
• Don’t continue developing on a broken foundation
• Many more….
#sqlinthecity
Goals of today’s workshop
1. Link a source control repository to a build system
2. Use the SQLCI resources to implement build steps that:• Verify that your database code has built successfully• Verify an upgrade to an existing database • Run database unit tests
3. Trigger builds automatically on changes being checked in to source control
4. Introduce artifacts as the basis for future repeatable deployments
#sqlinthecity
• Plugin for TeamCity
• Part of the Red Gate SQL Automation Pack
• Includes 3 build runners:
• Build
• Sync
• Test
#sqlinthecity
SQLCI Plugin – What is it?
SQLCI
#sqlinthecity
• Subversion(SVN)
• SQL Server 2012
• SQL Source Control
• TeamCity
#sqlinthecity
Current setup
Creating a project
• Creating a new TeamCity Project
• Adding a VCS Root
#sqlinthecity
The Build Step…
#sqlinthecity
CREATE
TABLE
foo…
Does creation
script run?
?
1. Convert scripts folder into package
2. Generate the creation script
3. Validate creation script
The Build Step…
#sqlinthecity
Triggering…
#sqlinthecity
Triggering…
#sqlinthecity
• Check it works so far.
Break time!
#sqlinthecity
The Sync Step…
#sqlinthecity
1. Apply package version to target database
2. Deployment validation (is the target the same as the package?)
V1.1
V1.2
compare
The Sync Step…
#sqlinthecity
The Sync Step…
• Check it works so far.
#sqlinthecity
Capturing an upgrade script…
/scriptFile:“<FilePath>“
We can use %build.number% to grab the current build number.
#sqlinthecity
Review…
#sqlinthecity
Try it for yourself….
• SQLServerCentral
#sqlinthecity
Lunch time!
#sqlinthecity
The road to database CI
1. Source control
2. Automated builds
3. Automated tests
4. Frequent check ins of small changes
#sqlinthecity
The road to database CI
1. Source control
2. Automated builds
3. Automated tests
4. Frequent check ins of small changes
#sqlinthecity
Advantages of Automated Tests
• Early warning of broken code
• Constant validation of existing functionality
• Detect when new changes introduce errors
• Feedback on the current state of the code
• Many more….
#sqlinthecity
SQL Test – What is it?
• Add-in for SQL Server Management Studio
• Builds upon tSQLt framework
• Adds a simple UI for creating, managing and running database unit tests.
• All tests are written in T-SQL
www.tsqlt.org
#sqlinthecity
Add database to SQL Test
#sqlinthecity
Add a test
Manually running the tests…
• Check it works so far.
#sqlinthecity
Automating tests
#sqlinthecity
The Test Step…
#sqlinthecity
1. Build empty test database from package
2. Populate a new database with test data (optional)
3. Run tSQLt unit tests(report in JUnit format)
The Test Step…
• Practical
– sqlCI.exe Sync
/package= <values5,2,3>
/databaseServer=<value6>
/databaseName=<value7>
Optionally supply SQL Server Credentials
#sqlinthecity
The Test Step…
• Run the build
#sqlinthecity
The Test Step…
• Break the build
#sqlinthecity
Review…
#sqlinthecity
Break time!
#sqlinthecity
Try it for yourself….
• SQLServerCentral
– How about a test to check the name added by the sproc?
#sqlinthecity
Contact info
• Tom Austin
– Pre Sales Engineer at Red Gate
– Email: [email protected]
www.red-gate.com/ci
#sqlinthecity
Top Related