Written by: Juan Carlos Olamendy Turruellas Introduction In this article, I want to talk about versioning Oracle database schemas to be agile in this new age of continuous integration and DevOps . In this fast pace age where we have teams collaborating, we need tools that help us to work fast without risking the governance of our database. For example, we need to track the changes, solve any code conflict as well as to have a central repository as the true source for reproducing changes to production environments. It’s common today to see developers creating the database schema but sometimes they forgot the naming conventions to comply with standards and the best practices such as creating a primary key for each table, create the appropriate indexes (neither too much nor too few), partition the data when required, etc. So we need a mechanism and a tool to validate each change before going into production. In this article, I want to show how to solve the problems above using git/GitHub and Toad for Oracle as the key pieces for a version control system. Toad for Oracle has a feature named Team Coding for helping the developers to manage code and object (tables, views, materialized views, indexes, etc) changes in the database. It uses the Editor to control access to the database objects. This feature is a type of centralized version control system, which is installed on the database as the central server-based repository, so no configuration is required for each developer. It enables check-in and check-out status visibility to track the changes to the database objects and resolve any conflict. Toad for Oracle has source control integration for git by providing check-in-in/check-out features onto a local git repository. Git is a popular distributed revision control system. Because it’s distributed, every local git repository has a complete history of changes independently of the network access to any central repository. In short, each developer has a copy of the repository and every commit is done locally until the developer decides pushing the changes to a remote repository (for example GitHub ) to share the code with the rest of the team. Then changes in the remote repository are synchronized with the local repository by pulling the remote changes. A common workflow with a git repository is as shown below: Init: Create a local repository. Clone : Get a local copy of a remote repository. Edit : Make changes to local repository. Add : Add changes of local repository to the staging area. Commit : Commit the changes of the local repository. Push : Push local changes/merges to remote repository. Pull : Fetch merges/changes from remote repository into the local one. When integrating git and Team Coding in Toad for Oracle we need to consider the following points: Git is a distributed version control while Team Coding is a centralized one, so Team Coding locks database objects even when this operation doesn’t exist in git (we see later an example of this). Team Coding check-in and check-out database objects onto the local repository, so the commit, push and pull operations are executed outside Toad for Oracle using the appropriate git commands Demo Now let’s go the practical part of the article, where we’re going to apply the learned principles and concepts with a real-word example. In order to execute correctly this demo, we need the following artifacts: Git installed Toad for Oracle installed Access to GitHub An instance of Oracle database with some schema for versioning Step01. Create a GitHub remote repository and its counterpart locally First of all, we need to create a remote repository in GitHub and the counterpart locally as shown below in the listing 01. echo "# db_repository" >> README.md git init git add README.md git commit -m "My first commit" git remote add origin https://github.com/my_git_account/db_repository.git git push -u origin master Listing 01 Step02. Install the Team Coding objects Next step is to install the Team Coding objects in the target database and create a TC_ADMIN_ROLE role. In order to execute this step correctly, we need to create a session with DBA role in Toad for Oracle because we’re creating a new schema in the database. We select the menu option “ Utilities|Team Coding|Administer” to go to the “Team Coding Administration” dialog. Then click on the “ Install Team Coding … ” button to launch the “Team Coding Installation” dialog as shown in the figure 01 and figure 02. Figure 01 Figure 02 Now we need to associate Team Coding with git as shown in the figure 03. Figure 03 Now, Team Coding is ready to be used by any developer/database user. In order to to use the Team Coding , we need to grant the TC_ADMIN_ROLE privilege for each developer as shown in the listing 02. SQL> GRANT TC_ADMIN_ROLE to test_user; Listing 02 Step03. Add a team project for versioning the schema Now log-on using Toad for Oracle as the schema/user that we want to version control. In our example, the user is test_user . Then, we select the menu option “ Utilities|Team Coding|Administer” to go to the “Team Coding Administration” dialog and create a new team project by pointing to the local repository as well as to provide information about the GitHub user as shown in the listing 04. Figure 04 Then, create a project representing the schema code to version as shown in the listing 05. Figure 05 And finally, link the database objects/schemas that we want to version control using the newly created project as shown in the figure 06. Figure 06 Using the “Team Coding Manager” window, we can see the list of database objects that we’re able to version control as shown in the figure 07. Check in green color, we can see the objects ready to be check-out. Figure 07 Step04. Check-out a database object Right-click on the EMPLOYEE table object and select the “Check-out” option to get the latest version of the database object as shown in the figure 08. With this action, we’ve locked this object only for us. This is a feature of Team Coding not of git . It’s also worth noting that, when objects are controlled using Team Coding, they cannot be altered in Toad unless they are first checked-out. Step05. Make some changes to a database object Now it’s time for us to make some changes to the EMPLOYEE table, for example, add a new column such as ADDRESS using “Alter Table” window as shown below in the figure 08. Figure 08 We can also validate that the changes are applied directly to the database as shown in the figure 09. Figure 09 This confirms that this tool is for working on the development environment where we can play with the database schema as we like, and this is another hard reason why there exists a version control system in order to commit the changes to the repository in the form of SQL scripts to be validated and tested before going into production. Production environment is a very secure place not to be used for development purpose. The process for moving schema changes in the form of SQL scripts from a development environment to a production one in a secure and fast way is the theme of continuous integration and delivery and it’s a topic for future articles. Step06. Check-in a database object locally When everything is OK, then we proceed to check-in the changes of the schemas in the local repository by right-click on the EMPLOYEE table and selecting the “Check-in” option. We can validate the changes in the form of SQL scripts in the local repository as shown in the figure 10. Figure 10 This SQL scripts are the source of the continuous integration for validating, unit testing and finally deploying into production environment. Step07. Submit the changes to the remote repository Now it’s the time to submit the changes to the remote repository (for our example is GitHub ) to share the code with the rest of the team as shown in the figure 11. Figure 11 If we go to GitHub , we can confirm that everything was pushed correctly over there as shown in the figure 12. Figure 12 Conclusion In this first part, I've showed how to create a version control system using git/GitHub and Toad for Oracle tools. Now you can use the ideas and real-world demo as the starting point for designing your continuous integration and DevOps environment for your Oracle databases.
↧