Category Archives: Version Control

Tips to help PL/SQL developers get started with CI/CD

In most ways, PL/SQL development is just like working with any other language but, sometimes it can be a little different.  If you’d like to create a Continuous Deployment Pipeline for your PL/SQL application, here is a short list of tips to help you get started.

Work From Files

Do not use the Database as your source code repository.  If you are making changes to your application in a live database, stop right now and go read PL/SQL 101: Save your source code to files by Steven Feuerstein.

Now that you’re working from files, those files should be checked into…

Version Control

There are a lot of version control applications out there, the most popular right now is probably Git.  One advantage of using Git is, you work in your own local copy of the repository making frequent commits that only you see.  If you’re working in your own personal database you could compile your changes to the database at this point.

If you’re working in a database shared with other people and you’re ready to compile your code,   git pull from the central repository to get any changes since your last pull.  Handle any merge conflicts, then git push your changes back up to the shared repository.  After the push, you can compile your code to the database.  This helps ensure that people don’t overwrite each other’s changes.

Making frequent small commits will help keep everything running smoothly.

What about your schema objects?

Your schema objects, such as tables and views, are as much a part of your application as any other component and changes to these objects should be version controlled as well.  The process of keeping track of schema changes is called Schema Migration and there are open source tools you can use such as Flyway and Liquibase.

Unit Testing

In any application it’s smart to have good test coverage, If your goal is Continuous Deployment it’s critical.  A Unit Test is when you test a single unit of code, in PL/SQL that may be a function or procedure.

Unit tests typically follow these steps:

  1. Setup the environment to simulate how it will be when the application is deployed.  This might include changing configuration settings and loading test data.
  2. Execute the code unit.
  3. Validate the results.
  4. Clean up the environment, resetting it to the state it was in before running the tests.

utPLSQL is a great tool for unit testing your PL/SQL applications.  You will write a package of tests for each ‘unit’ of your application which should test all of the required functionality and potential errors.  utPLSQL is an active open source project with an impressive set of features.  Check out the docs to get started.

Build

Building a database application usually consists of running the change scripts in a specific order.  It’s common to create a master script that executes the others in order of how the objects depend on each other.  However, if the master script simply executes the other scripts, you will need to create additional scripts to track and verify changes, and more scripts to give you the ability to rollback the changes if/when there’s a problem.

There are build tools such as Gradle and Maven that can easily execute your scripts.  But you’ll still need to create the additional control scripts.  If you use a Schema Migration tool it should include a lot of these additional functions without having to write extra scripts.  For an example, check out Dino Date which has a Liquibase migration included.

How to handle the PL/SQL code

You could include your PL/SQL code in a Schema Migration changeset but adding schema migration notation to your PL/SQL introduces another layer of complexity and potential errors.

In the Dino Date runOnChange directory, you will find examples of setting up Liquibase changesets that watch for changes in the files of objects that you would rather keep ‘pure’.  When you run a migration, if the file has changed Liquibase will run the new version.

In a shared database environment, you should execute a schema migration after you pull/merge/push your changes into your version control system.

Automate!

All of these pieces can be tied together and automated with an automation server such as Hudson or Jenkins (both are open source) to create a build pipeline.

Original by Jez Humble

A simple (maybe too simple) build pipeline using the above tools could follow these steps:

  1. Developer makes a change and pushes it to the shared Git repository.
  2. Hudson notices the repository has changed and triggers the build pipeline.
  3. The project is pulled from Git.
  4. Liquibase deploys the changes to a test database.
  5. utPLSQL is trigged to run the unit tests.
  6. Liquibase deploys the changes to the production database.

Other Useful Tools

  • Edition Based Redefinition[pdf] can be used to deploy applications with little to no downtime.
  • Oracle Developer Cloud Service comes with a ton of pre-configured tools to help with almost every aspect of your development process.
  • Gitora can help you version control your application if you are not able to move out to files.

Intro to Git

During the lunch and learn at KScope15 we talked a little about using open source projects as a way to learn/teach programming concepts.  There was a little confusion about Git and GitHub so I thought I’d put up a short post with some resources.

I don’t claim to be a Git guru by any means, this is intended to be a high level introduction.  I have linked several resources at the end for those who want to dive deep.

Git, a Distributed Version Control System (VCS)

You may be familiar with the typical VCS which has a centralized shared repository.  You checkout a copy of the code, work on it, then check it back in.  Everyone works from the same central repo.

Git is different.  Think of it as having your own personal VCS built into your local work-space.  You still check code in and out, branch, merge and commit changes; but instead of locking files on a remote system somewhere, you merge your local VCS into a shared remote repo when ready.  You have the advantage of being able to make multiple frequent commits locally and only push to the central repo when you want your changes merged into the product.  Your work doesn’t impact others till you make the push.

There are many great features in Git, but for me it’s the distributed functionality that makes it awesome.

When your central repo is unreachable (Network/internet issues, server down or system patches;) using a traditional VCS your developers are not able to commit changes till the problem is resolved.  They can continue to work, but run the risk of not being able to roll back to a ‘working’ point in the code.

With Git your developers continue to work making frequent commits; they can rollback, branch, merge and shelf changes as needed.  When the issue is resolved they pull the current state down, merge their changes and push the merged changes back up.

As an added bonus, you can push your changes to multiple remote repos.  You can push and pull from an internal company repo as well as a remote hosting service such as GitHub or BitBucket.

Remote git hosting services

A remote git hosting service is basically a place where you host a Git repository.  Similar to how you would create a Git repository on an internal server, you can create the same repo on one of these services.  Once the repo is created at your chosen host, you pull and push to it the same as an other repo.

A common misconception is that GitHub is Git.  GitHub is probably the most popular of the many remote git hosting services but it is not Git.

The two I’ve used the most are GitHub and BitBucket.  Both offer a very comparable set of services; issue tracking, pull requests, team collaboration features and others.  GitHub tends to be more popular, especially with the open source projects; BitBucket gives you private repositories in the free level.

I would recommend either one, take your time to see which fits your needs better.  If you have a service you like better please leave a comment.

Want to learn more

There are a ton of resources to help you become an expert, here are a few I’ve found.  If you have a great one, leave a comment.

Git resources:

Remote git hosting services: