SlideShare a Scribd company logo
1 of 40
Download to read offline
Database
Refactoring
keeping up with evolution

                     Anton Keks
A few words of warning...
Avoid overspecialization



                             --- BARRIER ---
  Application Developer                        Database Developer




                        Communication
                         Collaboration
                         Understanding
                      Knowledge-exchange
                           New skills
       Developer                                  Developer

                                                                    3
Refactoring
In Maths, to “factor” is to reduce an expression
to it's simplest form


In CS, is the disciplined way to restructure code
  ●   Without adding new features
  ●   Improving the design
  ●
      Often making the code simpler, more readable



                                                     4
Definition: Code Refactoring
●   A small change to the code to improve design
    that retains the behavioural semantics of the
    code
●   Code refactoring allows you to evolve the code
    slowly over time, to take evolutionary
    approach to programming




                                                     5
Definition: Database Refactoring
●   A simple change to a schema that improves its
    design while retaining behavioural and
    informational semantics
●   A database includes both structural aspects as
    well as functional aspects




                                                     6
It's
Refactoring
     not
Refucktoring

               7
Why refactor?
●   To safely fix existing legacy databases
      ●   They are here to stay
      ●
          They are not going to fix themselves!
●
    To support evolutionary development
      ●
          Because our business, our customers are changing
      ●   The world around our software is evolving
●
    Prevent over-design
      ●
          Simple, maintainable code and data model


                                                             8
Leads to Evolutionary Design
●   Small steps
●   The simplest design first
●   Unit tests of stored code (or avoid it!)
●
    Design is final only when the code is ready




                                                  9
Otherwise, it can happen that
Database is not under control
   It lives its own life and is controlling us




       The DB



                                                 10
Database Smells




     The DB




                  11
Database Smells
●
    All known code smells also apply to stored code
    as well, including:
      ●   Monster procedures
      ●
          Spaghetti code
      ●   Code duplication
      ●   IF-ELSE overuse
      ●   Code ladder
      ●
          Low cohesion
      ●   etc

                                                      12
Database Smells
●
    Database schema can add to the musty odour
      ●
          Multi-purpose table / column
      ●   Redundant data
      ●
          Tables with many columns / rows
      ●
          “Smart” columns
      ●
          Lack of constraints
      ●   Fear of change




                                                 13
Fear of change
●   The strongest of all smells
●
    Prevents innovation
●
    Reduces effectiveness
●   Produces even more mess
●
    Over time, situation gets only worse




                                           14
How to do it right?
●   Start in your development sandbox
●
    Apply to the integration sandbox(es)
●
    Install into production




        “Keep out of my unstable development DB!”   15
Sandboxes (1)
      Project A


                             Project A
          Development       Integration
Development Sandbox           Sandbox
  Sandbox                                           Pre-production
                                                       Sandbox



                                                                                    Production
      Project B

                                                           Demo
                             Project B                  Sandbox(es)
          Development       Integration
Development Sandbox           Sandbox
  Sandbox



                                                                         highly
                    frequent               controlled
                                                                       controlled
                   deployment             deployment
                                                                      deployment                 16
Best case scenario (easiest)

          The Application




              The DB




                               17
Worst case scenario (hardest)

                      The Application
 Other applications
Other applications                       Other applications
                                        Other applications
  we know about
 we know about                            we know about
                                          we don't know




Persistence                                   Other
frameworks                The DB
                                               DBs




           Data                          Data
          imports                       exports
                         Test code
                                                              18
Trivial case
●   Can we rename a column in our DB?
      ●   Without breaking 100 applications?
●
    If we can't do something trivial, how can we
    do something important?
      ●   If we can't evolve the schema, we are most likely
          not very good at developing applications




                                                              19
Testing (2)
●   Do we have code in the DB that implements
    critical business functionality?
●   Do we consider data an important asset?
●
    … and it's all not tested?

●
    Automatic regression tests would help
●
    Proper refactoring cannot happen without
    them

                                                20
Database Unit Tests
●   Too complex?
●
    No good framework?
create or replace package dbunit
is
   procedure assert_equals(expected number, actual number);
   procedure assert_equals(expected varchar2, actual varchar2);
   procedure assert_null(actual varchar2);
   procedure assert_not_null(actual varchar2);
   ...
end;

create or replace public synonym dbunit for dbunit;
grant execute on dbunit to public;
                                                                  21
Running Unit Tests
●   Anonymous PL/SQL code
●
    No need to change the DB
●
    Assertions raise_application_error with
    specific message if tests fail
●   Rollback at the end
●
    Runnable with any SQL tool
●
    Or with ant


                                              22
PL/SQL Unit Test example
declare
  xml XmlType;
begin
--@Test no messages in case of no changes
  xml := hub.next_message(0);
  dbunit.assert_null(xml);

--@Test identification number change message
  hub_api.ident_number_changed('123', '007', 'PERSONAL_CODE',
                                 'LV', '888', current_timestamp);
  xml := hub.next_message(1);
  dbunit.assert_xpath('123', '/hub/party/@source_ref', xml);
end;


                                                                    23
How to deal with coupling?
●   Big-Bang approach
      ●   Usually, you can't fix all 100 apps at once
●
    Give up
      ●
          And afford even more technical debt?
●
    Transition Window approach
      ●   Can be a viable solution




                                                        24
Transition Window (3)
●   Deprecate the old schema
      ●   Write tests if not present
      ●
          Decide on the removal date, communicate it out
●
    Create the change
      ●
          Make the old schema work (scaffolding code)
●   Run the tests

      Implement the           Transition period         Refactoring
        refactoring       (old schema deprecated)       completed

             Deploy new schema, migrate        Remove old schema
             data, add scaffolding code        and scaffolding code
                                                                      25
Dealing with unknown applications
●   It's easy to eliminate all usages in
      ●   the DB itself
      ●
          the application you are developing
●
    Log accesses to the deprecated schema
      ●
          Helps to find these 'unknown' applications




                                                       26
Changelog (4)
●   Doing all this needs proper tracking of changes
●
    Write delta-scripts (aka migrations)
      ●
          To start the transition period
      ●
          To end the transition period (these will be
          applied on a later date/release)
●
    Same scripts for
      ●   Updating sandboxes
      ●   Deployment to production


                                                        27
What to refactor in a DB?
●   Databases usually contain
      ●   Data (stored according to a schema)
      ●
          Stored code
●
    Stored code is no different from any other code
      ●
          except that it runs inside of a database
●   Database schema
      ●   Data is the state of a database
      ●
          Maintaining the state needs a different approach
          from refactoring the code
                                                             28
Upgrade/Downgrade Tool
●   Upgrade tool will track/update the changelog
    table automatically
      ●
          Each DB will know it's state (version)
      ●
          It will be easy to upgrade any sandbox
●   Downgrading possibility is also important
      ●   Delta scripts need to be two-way, i.e. include
          undo statements
      ●
          It will be easy to switch to any other state
           –   e.g. in order to reproduce a production bug

                                                             29
Sample refactoring script
-- rename KLK to CUSTOMER_ID
ALTER TABLE CUSTOMER ADD COLUMN CUSTOMER_ID NUMBER;
UPDATE CUSTOMER SET CUSTOMER_ID = KLK;
-- keep KLK and CUSTOMER_ID in sync
CREATE TRIGGER ...;

--//@UNDO
DROP TRIGGER ...;
ALTER TABLE CUSTOMER DROP COLUMN CUSTOMER_ID;

-- this will go to another script for later deployment
-- finish rename column refactoring
DROP TRIGGER ...;
ALTER TABLE CUSTOMER DROP COLUMN KLK;
--//@UNDO
...                                                      30
dbdeploy
●   http://dbdeploy.com
●
    Very simple
●
    Runnable from ant or command-line
●   Delta scripts
      ●   Numbered standard .sql files
      ●   Unapplied yet delta scripts run sequentially
      ●   Nothing is done if the DB is already up-to-date



                                                            31
liquibase
●   http://liquibase.org
●
    More features, more complex
●
    Runnable from ant or command-line
●   Delta scripts
      ●   In XML format (either custom tags or inline SQL)
      ●   Many changes per file
      ●   Identifies changes with Change ID, Author, File
      ●
          Records MD5 for detecting of changed scripts

                                                             32
Versioning

         Development
              DB
            v47.29                             Pre-production
Development                                          DB
     DB                                            v46.45
   v47.34
                             Integration                            Production
                                 DB                                   v45.82
                               v47.29
         Development
              DB                                  Demo
            v46.50                                  DB
                                                  v46.13
Development
     DB
   v45.82



              Each DB knows its release/version number and can be
                    upgraded/downgraded to any other state
                                                                                 33
Proper Versioning
●   Baseline (aka skin)
      ●   Delta scripts (migrations)
      ●
          Code changes
●
    Branch for a release
●
    New baseline after going to production
●   The goal of versioning a database is to push
    out changes in a consistent, controlled manner
    and to build reproducible software

                                                     34
Continuous Integration (5)
●   CI server will verify each commit to the VCS
      ●   By deploying it into an integration sandbox
      ●
          And running regression tests
      ●   Fully automatically
●   All the usual benefits
      ●   Better quality, Quick feedback
      ●
          Build is always ready and deployable
      ●
          Developers are independent
      ●   No locking, no overwriting changes!!!
                                                        35
Teamwork (6)
●   Developers
      ●   Must work closely with Agile DBAs
      ●
          Must gain basic data skills
●
    Agile DBAs/DB developers
      ●
          Must be embedded into the development team
      ●   Must gain basic application skills




                                                       36
Tools
●   Delta scripts
      ●
          dbdeploy, liquibase, deltasql
      ●
          Easy to write our own!
●   PL/SQL code


                                 vs
                                              Intellij IDEA (Java)
                       Oracle SQL Developer

                                                                     37
Enabling database refactoring
(1) Development Sandboxes
(2) Regression Testing
(3) Transition Window approach
(4) Versioning with Changelog & Delta scripts
(5) Continuous integration
(6) Teamwork & Cultural Changes



                                                38
The Catalog
●   Scott Ambler and Pramod Sadalage have
    created a nice catalog of DB refactorings
●   http://www.ambysoft.com/books/refactoringDatabases.html
●
    Classification
      ●
          Structural
      ●
          Data Quality
      ●
          Referential Integrity
      ●
          Architectural
      ●   Method (Stored code)
      ●
          Transformations
          (non-refactorings)
                                                              39
Best practices
●   Refactor to ease additions to your schema
●
    Ensure the test suite is in place
●
    Take small steps
●   Program for people
●
    Don’t publish data models prematurely
●   The need to document reflects a need to
    refactor
●   Test frequently
                              (according to Scott Ambler)
                                                            40

More Related Content

What's hot

The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practicesBill Buchan
 
The Power Of Refactoring (php|tek 09)
The Power Of Refactoring (php|tek 09)The Power Of Refactoring (php|tek 09)
The Power Of Refactoring (php|tek 09)Stefan Koopmanschap
 
Webapp acceptance testing a case study
Webapp acceptance testing   a case studyWebapp acceptance testing   a case study
Webapp acceptance testing a case studyekantola
 
The Power Of Refactoring (PHPCon Italia)
The Power Of Refactoring (PHPCon Italia)The Power Of Refactoring (PHPCon Italia)
The Power Of Refactoring (PHPCon Italia)Stefan Koopmanschap
 
Creating a reasonable project boilerplate
Creating a reasonable project boilerplateCreating a reasonable project boilerplate
Creating a reasonable project boilerplateStanislav Petrov
 
Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy CodeAdam Culp
 
Real developers-dont-need-unit-tests
Real developers-dont-need-unit-testsReal developers-dont-need-unit-tests
Real developers-dont-need-unit-testsSkills Matter
 
XPDays Ukraine: Legacy
XPDays Ukraine: LegacyXPDays Ukraine: Legacy
XPDays Ukraine: LegacyVictor_Cr
 
程序员实践之路
程序员实践之路程序员实践之路
程序员实践之路Horky Chen
 
invokedynamic: Evolution of a Language Feature
invokedynamic: Evolution of a Language Featureinvokedynamic: Evolution of a Language Feature
invokedynamic: Evolution of a Language FeatureDanHeidinga
 
How we tested our code "Google way"
How we tested our code "Google way"How we tested our code "Google way"
How we tested our code "Google way"Oleksiy Rezchykov
 
Shirly Ronen - User story testing activities
Shirly Ronen - User story testing activitiesShirly Ronen - User story testing activities
Shirly Ronen - User story testing activitiesAgileSparks
 
Test Driven Development Powered by LEGO
Test Driven Development Powered by LEGOTest Driven Development Powered by LEGO
Test Driven Development Powered by LEGOAgile Montréal
 
It's all about behaviour, also in php - phpspec
It's all about behaviour, also in php - phpspecIt's all about behaviour, also in php - phpspec
It's all about behaviour, also in php - phpspecGiulio De Donato
 
Automating MySQL operations with Puppet
Automating MySQL operations with PuppetAutomating MySQL operations with Puppet
Automating MySQL operations with PuppetKris Buytaert
 
Shirly Ronen - Documenting an agile defect
Shirly Ronen - Documenting an agile defectShirly Ronen - Documenting an agile defect
Shirly Ronen - Documenting an agile defectAgileSparks
 

What's hot (19)

The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practices
 
Tdd
TddTdd
Tdd
 
The Power Of Refactoring (php|tek 09)
The Power Of Refactoring (php|tek 09)The Power Of Refactoring (php|tek 09)
The Power Of Refactoring (php|tek 09)
 
Webapp acceptance testing a case study
Webapp acceptance testing   a case studyWebapp acceptance testing   a case study
Webapp acceptance testing a case study
 
The Power Of Refactoring (PHPCon Italia)
The Power Of Refactoring (PHPCon Italia)The Power Of Refactoring (PHPCon Italia)
The Power Of Refactoring (PHPCon Italia)
 
TDD anche su iOS
TDD anche su iOSTDD anche su iOS
TDD anche su iOS
 
Creating a reasonable project boilerplate
Creating a reasonable project boilerplateCreating a reasonable project boilerplate
Creating a reasonable project boilerplate
 
Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy Code
 
Making Strongly-typed NETCONF Usable
Making Strongly-typed NETCONF UsableMaking Strongly-typed NETCONF Usable
Making Strongly-typed NETCONF Usable
 
Real developers-dont-need-unit-tests
Real developers-dont-need-unit-testsReal developers-dont-need-unit-tests
Real developers-dont-need-unit-tests
 
XPDays Ukraine: Legacy
XPDays Ukraine: LegacyXPDays Ukraine: Legacy
XPDays Ukraine: Legacy
 
程序员实践之路
程序员实践之路程序员实践之路
程序员实践之路
 
invokedynamic: Evolution of a Language Feature
invokedynamic: Evolution of a Language Featureinvokedynamic: Evolution of a Language Feature
invokedynamic: Evolution of a Language Feature
 
How we tested our code "Google way"
How we tested our code "Google way"How we tested our code "Google way"
How we tested our code "Google way"
 
Shirly Ronen - User story testing activities
Shirly Ronen - User story testing activitiesShirly Ronen - User story testing activities
Shirly Ronen - User story testing activities
 
Test Driven Development Powered by LEGO
Test Driven Development Powered by LEGOTest Driven Development Powered by LEGO
Test Driven Development Powered by LEGO
 
It's all about behaviour, also in php - phpspec
It's all about behaviour, also in php - phpspecIt's all about behaviour, also in php - phpspec
It's all about behaviour, also in php - phpspec
 
Automating MySQL operations with Puppet
Automating MySQL operations with PuppetAutomating MySQL operations with Puppet
Automating MySQL operations with Puppet
 
Shirly Ronen - Documenting an agile defect
Shirly Ronen - Documenting an agile defectShirly Ronen - Documenting an agile defect
Shirly Ronen - Documenting an agile defect
 

Viewers also liked

LINSEN an efficient approach to split identifiers and expand abbreviations
LINSEN an efficient approach to split identifiers and expand abbreviationsLINSEN an efficient approach to split identifiers and expand abbreviations
LINSEN an efficient approach to split identifiers and expand abbreviationsValerio Maggio
 
Improving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniquesImproving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniquesValerio Maggio
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8Garth Gilmour
 
Publicidad y Redes Sociales. II Parte.
Publicidad y Redes Sociales. II Parte.Publicidad y Redes Sociales. II Parte.
Publicidad y Redes Sociales. II Parte.Bidane Galicia
 
Beacon Portuguese October 2011
Beacon Portuguese October 2011Beacon Portuguese October 2011
Beacon Portuguese October 2011npac75
 
Manoel luciene2007doutorado
Manoel luciene2007doutoradoManoel luciene2007doutorado
Manoel luciene2007doutoradoLuciene Gomes
 
Geneva crossing the lake - cool travel - around the world
Geneva   crossing the lake - cool travel - around the worldGeneva   crossing the lake - cool travel - around the world
Geneva crossing the lake - cool travel - around the worldMaarten Schäfer
 
HSW - Home Sweet Work
HSW - Home Sweet WorkHSW - Home Sweet Work
HSW - Home Sweet WorkMarco Coghi
 
Invitación a Ponencia sobre Desarrollo Comunitario DIF Nuevo León
Invitación a Ponencia sobre Desarrollo Comunitario DIF Nuevo LeónInvitación a Ponencia sobre Desarrollo Comunitario DIF Nuevo León
Invitación a Ponencia sobre Desarrollo Comunitario DIF Nuevo LeónNatura Equilibrium
 
Drupal Panopoly | Drupal Camp Vienna 2015
Drupal Panopoly | Drupal Camp Vienna 2015Drupal Panopoly | Drupal Camp Vienna 2015
Drupal Panopoly | Drupal Camp Vienna 2015Matthias Walti
 
Merkenbinding door Social Media (Poken uitgelegd)
Merkenbinding door Social Media (Poken uitgelegd)Merkenbinding door Social Media (Poken uitgelegd)
Merkenbinding door Social Media (Poken uitgelegd)Ayman van Bregt
 
Trabajo práctico nº3
Trabajo práctico nº3Trabajo práctico nº3
Trabajo práctico nº3Camiii07
 
Influencia de la estructura de los distritos en su capacidad de innovación
Influencia de la estructura de los distritos en su capacidad de innovaciónInfluencia de la estructura de los distritos en su capacidad de innovación
Influencia de la estructura de los distritos en su capacidad de innovaciónDaniel Gabadón-Estevan
 
Power mireia silvia
Power mireia silviaPower mireia silvia
Power mireia silviajdelga36
 

Viewers also liked (20)

LINSEN an efficient approach to split identifiers and expand abbreviations
LINSEN an efficient approach to split identifiers and expand abbreviationsLINSEN an efficient approach to split identifiers and expand abbreviations
LINSEN an efficient approach to split identifiers and expand abbreviations
 
JavaScript Refactoring
JavaScript RefactoringJavaScript Refactoring
JavaScript Refactoring
 
Improving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniquesImproving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniques
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
 
Publicidad y Redes Sociales. II Parte.
Publicidad y Redes Sociales. II Parte.Publicidad y Redes Sociales. II Parte.
Publicidad y Redes Sociales. II Parte.
 
Beacon Portuguese October 2011
Beacon Portuguese October 2011Beacon Portuguese October 2011
Beacon Portuguese October 2011
 
Manoel luciene2007doutorado
Manoel luciene2007doutoradoManoel luciene2007doutorado
Manoel luciene2007doutorado
 
Geneva crossing the lake - cool travel - around the world
Geneva   crossing the lake - cool travel - around the worldGeneva   crossing the lake - cool travel - around the world
Geneva crossing the lake - cool travel - around the world
 
HSW - Home Sweet Work
HSW - Home Sweet WorkHSW - Home Sweet Work
HSW - Home Sweet Work
 
Invitación a Ponencia sobre Desarrollo Comunitario DIF Nuevo León
Invitación a Ponencia sobre Desarrollo Comunitario DIF Nuevo LeónInvitación a Ponencia sobre Desarrollo Comunitario DIF Nuevo León
Invitación a Ponencia sobre Desarrollo Comunitario DIF Nuevo León
 
Drupal Panopoly | Drupal Camp Vienna 2015
Drupal Panopoly | Drupal Camp Vienna 2015Drupal Panopoly | Drupal Camp Vienna 2015
Drupal Panopoly | Drupal Camp Vienna 2015
 
Merkenbinding door Social Media (Poken uitgelegd)
Merkenbinding door Social Media (Poken uitgelegd)Merkenbinding door Social Media (Poken uitgelegd)
Merkenbinding door Social Media (Poken uitgelegd)
 
The Shipyard Email Statistics
The Shipyard Email StatisticsThe Shipyard Email Statistics
The Shipyard Email Statistics
 
Gorbeia Central Park
Gorbeia Central ParkGorbeia Central Park
Gorbeia Central Park
 
Fiji 2012
Fiji 2012Fiji 2012
Fiji 2012
 
Trabajo práctico nº3
Trabajo práctico nº3Trabajo práctico nº3
Trabajo práctico nº3
 
Hitex TexPrint
Hitex TexPrintHitex TexPrint
Hitex TexPrint
 
7 people
7 people7 people
7 people
 
Influencia de la estructura de los distritos en su capacidad de innovación
Influencia de la estructura de los distritos en su capacidad de innovaciónInfluencia de la estructura de los distritos en su capacidad de innovación
Influencia de la estructura de los distritos en su capacidad de innovación
 
Power mireia silvia
Power mireia silviaPower mireia silvia
Power mireia silvia
 

Similar to Database Refactoring

Reverse engineering
Reverse engineeringReverse engineering
Reverse engineeringSaswat Padhi
 
The View - The top 30 Development tips
The View - The top 30 Development tipsThe View - The top 30 Development tips
The View - The top 30 Development tipsBill Buchan
 
Pipeline as code for your infrastructure as Code
Pipeline as code for your infrastructure as CodePipeline as code for your infrastructure as Code
Pipeline as code for your infrastructure as CodeKris Buytaert
 
Continuous Integration for Oracle Database Development
Continuous Integration for Oracle Database DevelopmentContinuous Integration for Oracle Database Development
Continuous Integration for Oracle Database DevelopmentVladimir Bakhov
 
Achieving Full Stack DevOps at Colonial Life
Achieving Full Stack DevOps at Colonial Life Achieving Full Stack DevOps at Colonial Life
Achieving Full Stack DevOps at Colonial Life DevOps.com
 
Kelly potvin nosurprises_odtug_oow12
Kelly potvin nosurprises_odtug_oow12Kelly potvin nosurprises_odtug_oow12
Kelly potvin nosurprises_odtug_oow12Enkitec
 
Extensible dev secops pipelines with Jenkins, Docker, Terraform, and a kitche...
Extensible dev secops pipelines with Jenkins, Docker, Terraform, and a kitche...Extensible dev secops pipelines with Jenkins, Docker, Terraform, and a kitche...
Extensible dev secops pipelines with Jenkins, Docker, Terraform, and a kitche...Richard Bullington-McGuire
 
Moby is killing your devops efforts
Moby is killing your devops effortsMoby is killing your devops efforts
Moby is killing your devops effortsKris Buytaert
 
Taking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and DecideTaking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and DecideBret Fisher
 
Taking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and DecideTaking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and DecideDocker, Inc.
 
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...Dan Allen
 
Deploying your SaaS stack OnPrem
Deploying your SaaS stack OnPremDeploying your SaaS stack OnPrem
Deploying your SaaS stack OnPremKris Buytaert
 
[KubeCon NA 2018] Telepresence Deep Dive Session - Rafael Schloming & Luke Sh...
[KubeCon NA 2018] Telepresence Deep Dive Session - Rafael Schloming & Luke Sh...[KubeCon NA 2018] Telepresence Deep Dive Session - Rafael Schloming & Luke Sh...
[KubeCon NA 2018] Telepresence Deep Dive Session - Rafael Schloming & Luke Sh...Ambassador Labs
 
Preparing for Neo - Singapore OutSystems User Group October 2022 Meetup
Preparing for Neo - Singapore OutSystems User Group October 2022 MeetupPreparing for Neo - Singapore OutSystems User Group October 2022 Meetup
Preparing for Neo - Singapore OutSystems User Group October 2022 MeetupYashrajNayak4
 
Building FoundationDB
Building FoundationDBBuilding FoundationDB
Building FoundationDBFoundationDB
 
Elephant grooming: quality with Hadoop
Elephant grooming: quality with HadoopElephant grooming: quality with Hadoop
Elephant grooming: quality with HadoopRoman Nikitchenko
 
Discover MongoDB - Israel
Discover MongoDB - IsraelDiscover MongoDB - Israel
Discover MongoDB - IsraelMichael Fiedler
 
UKLUG 2012 - XPages Extensibility API - going deep!
UKLUG 2012 - XPages Extensibility API - going deep!UKLUG 2012 - XPages Extensibility API - going deep!
UKLUG 2012 - XPages Extensibility API - going deep!René Winkelmeyer
 

Similar to Database Refactoring (20)

Reverse engineering
Reverse engineeringReverse engineering
Reverse engineering
 
The View - The top 30 Development tips
The View - The top 30 Development tipsThe View - The top 30 Development tips
The View - The top 30 Development tips
 
Pipeline as code for your infrastructure as Code
Pipeline as code for your infrastructure as CodePipeline as code for your infrastructure as Code
Pipeline as code for your infrastructure as Code
 
Continuous Integration for Oracle Database Development
Continuous Integration for Oracle Database DevelopmentContinuous Integration for Oracle Database Development
Continuous Integration for Oracle Database Development
 
Achieving Full Stack DevOps at Colonial Life
Achieving Full Stack DevOps at Colonial Life Achieving Full Stack DevOps at Colonial Life
Achieving Full Stack DevOps at Colonial Life
 
Kelly potvin nosurprises_odtug_oow12
Kelly potvin nosurprises_odtug_oow12Kelly potvin nosurprises_odtug_oow12
Kelly potvin nosurprises_odtug_oow12
 
Extensible dev secops pipelines with Jenkins, Docker, Terraform, and a kitche...
Extensible dev secops pipelines with Jenkins, Docker, Terraform, and a kitche...Extensible dev secops pipelines with Jenkins, Docker, Terraform, and a kitche...
Extensible dev secops pipelines with Jenkins, Docker, Terraform, and a kitche...
 
Moby is killing your devops efforts
Moby is killing your devops effortsMoby is killing your devops efforts
Moby is killing your devops efforts
 
Taking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and DecideTaking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and Decide
 
Taking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and DecideTaking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and Decide
 
Debugging ZFS: From Illumos to Linux
Debugging ZFS: From Illumos to LinuxDebugging ZFS: From Illumos to Linux
Debugging ZFS: From Illumos to Linux
 
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
 
Deploying your SaaS stack OnPrem
Deploying your SaaS stack OnPremDeploying your SaaS stack OnPrem
Deploying your SaaS stack OnPrem
 
[KubeCon NA 2018] Telepresence Deep Dive Session - Rafael Schloming & Luke Sh...
[KubeCon NA 2018] Telepresence Deep Dive Session - Rafael Schloming & Luke Sh...[KubeCon NA 2018] Telepresence Deep Dive Session - Rafael Schloming & Luke Sh...
[KubeCon NA 2018] Telepresence Deep Dive Session - Rafael Schloming & Luke Sh...
 
Preparing for Neo - Singapore OutSystems User Group October 2022 Meetup
Preparing for Neo - Singapore OutSystems User Group October 2022 MeetupPreparing for Neo - Singapore OutSystems User Group October 2022 Meetup
Preparing for Neo - Singapore OutSystems User Group October 2022 Meetup
 
Django in the Real World
Django in the Real WorldDjango in the Real World
Django in the Real World
 
Building FoundationDB
Building FoundationDBBuilding FoundationDB
Building FoundationDB
 
Elephant grooming: quality with Hadoop
Elephant grooming: quality with HadoopElephant grooming: quality with Hadoop
Elephant grooming: quality with Hadoop
 
Discover MongoDB - Israel
Discover MongoDB - IsraelDiscover MongoDB - Israel
Discover MongoDB - Israel
 
UKLUG 2012 - XPages Extensibility API - going deep!
UKLUG 2012 - XPages Extensibility API - going deep!UKLUG 2012 - XPages Extensibility API - going deep!
UKLUG 2012 - XPages Extensibility API - going deep!
 

More from Anton Keks

Java Course 14: Beans, Applets, GUI
Java Course 14: Beans, Applets, GUIJava Course 14: Beans, Applets, GUI
Java Course 14: Beans, Applets, GUIAnton Keks
 
Java Course 13: JDBC & Logging
Java Course 13: JDBC & LoggingJava Course 13: JDBC & Logging
Java Course 13: JDBC & LoggingAnton Keks
 
Java Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsJava Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsAnton Keks
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design PatternsAnton Keks
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyAnton Keks
 
Java Course 9: Networking and Reflection
Java Course 9: Networking and ReflectionJava Course 9: Networking and Reflection
Java Course 9: Networking and ReflectionAnton Keks
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsAnton Keks
 
Java Course 7: Text processing, Charsets & Encodings
Java Course 7: Text processing, Charsets & EncodingsJava Course 7: Text processing, Charsets & Encodings
Java Course 7: Text processing, Charsets & EncodingsAnton Keks
 
Java Course 6: Introduction to Agile
Java Course 6: Introduction to AgileJava Course 6: Introduction to Agile
Java Course 6: Introduction to AgileAnton Keks
 
Java Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, AssertionsJava Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, AssertionsAnton Keks
 
Java Course 4: Exceptions & Collections
Java Course 4: Exceptions & CollectionsJava Course 4: Exceptions & Collections
Java Course 4: Exceptions & CollectionsAnton Keks
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOPAnton Keks
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: BasicsAnton Keks
 
Java Course 1: Introduction
Java Course 1: IntroductionJava Course 1: Introduction
Java Course 1: IntroductionAnton Keks
 
Java Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, HibernateJava Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, HibernateAnton Keks
 
Being a Professional Software Developer
Being a Professional Software DeveloperBeing a Professional Software Developer
Being a Professional Software DeveloperAnton Keks
 

More from Anton Keks (16)

Java Course 14: Beans, Applets, GUI
Java Course 14: Beans, Applets, GUIJava Course 14: Beans, Applets, GUI
Java Course 14: Beans, Applets, GUI
 
Java Course 13: JDBC & Logging
Java Course 13: JDBC & LoggingJava Course 13: JDBC & Logging
Java Course 13: JDBC & Logging
 
Java Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsJava Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & Servlets
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design Patterns
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and Concurrency
 
Java Course 9: Networking and Reflection
Java Course 9: Networking and ReflectionJava Course 9: Networking and Reflection
Java Course 9: Networking and Reflection
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Java Course 7: Text processing, Charsets & Encodings
Java Course 7: Text processing, Charsets & EncodingsJava Course 7: Text processing, Charsets & Encodings
Java Course 7: Text processing, Charsets & Encodings
 
Java Course 6: Introduction to Agile
Java Course 6: Introduction to AgileJava Course 6: Introduction to Agile
Java Course 6: Introduction to Agile
 
Java Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, AssertionsJava Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, Assertions
 
Java Course 4: Exceptions & Collections
Java Course 4: Exceptions & CollectionsJava Course 4: Exceptions & Collections
Java Course 4: Exceptions & Collections
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOP
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: Basics
 
Java Course 1: Introduction
Java Course 1: IntroductionJava Course 1: Introduction
Java Course 1: Introduction
 
Java Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, HibernateJava Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, Hibernate
 
Being a Professional Software Developer
Being a Professional Software DeveloperBeing a Professional Software Developer
Being a Professional Software Developer
 

Recently uploaded

The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sectoritnewsafrica
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfAarwolf Industries LLC
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialJoão Esperancinha
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessWSO2
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 

Recently uploaded (20)

The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdf
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorial
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with Platformless
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 

Database Refactoring

  • 2. A few words of warning...
  • 3. Avoid overspecialization --- BARRIER --- Application Developer Database Developer Communication Collaboration Understanding Knowledge-exchange New skills Developer Developer 3
  • 4. Refactoring In Maths, to “factor” is to reduce an expression to it's simplest form In CS, is the disciplined way to restructure code ● Without adding new features ● Improving the design ● Often making the code simpler, more readable 4
  • 5. Definition: Code Refactoring ● A small change to the code to improve design that retains the behavioural semantics of the code ● Code refactoring allows you to evolve the code slowly over time, to take evolutionary approach to programming 5
  • 6. Definition: Database Refactoring ● A simple change to a schema that improves its design while retaining behavioural and informational semantics ● A database includes both structural aspects as well as functional aspects 6
  • 7. It's Refactoring not Refucktoring 7
  • 8. Why refactor? ● To safely fix existing legacy databases ● They are here to stay ● They are not going to fix themselves! ● To support evolutionary development ● Because our business, our customers are changing ● The world around our software is evolving ● Prevent over-design ● Simple, maintainable code and data model 8
  • 9. Leads to Evolutionary Design ● Small steps ● The simplest design first ● Unit tests of stored code (or avoid it!) ● Design is final only when the code is ready 9
  • 10. Otherwise, it can happen that Database is not under control It lives its own life and is controlling us The DB 10
  • 11. Database Smells The DB 11
  • 12. Database Smells ● All known code smells also apply to stored code as well, including: ● Monster procedures ● Spaghetti code ● Code duplication ● IF-ELSE overuse ● Code ladder ● Low cohesion ● etc 12
  • 13. Database Smells ● Database schema can add to the musty odour ● Multi-purpose table / column ● Redundant data ● Tables with many columns / rows ● “Smart” columns ● Lack of constraints ● Fear of change 13
  • 14. Fear of change ● The strongest of all smells ● Prevents innovation ● Reduces effectiveness ● Produces even more mess ● Over time, situation gets only worse 14
  • 15. How to do it right? ● Start in your development sandbox ● Apply to the integration sandbox(es) ● Install into production “Keep out of my unstable development DB!” 15
  • 16. Sandboxes (1) Project A Project A Development Integration Development Sandbox Sandbox Sandbox Pre-production Sandbox Production Project B Demo Project B Sandbox(es) Development Integration Development Sandbox Sandbox Sandbox highly frequent controlled controlled deployment deployment deployment 16
  • 17. Best case scenario (easiest) The Application The DB 17
  • 18. Worst case scenario (hardest) The Application Other applications Other applications Other applications Other applications we know about we know about we know about we don't know Persistence Other frameworks The DB DBs Data Data imports exports Test code 18
  • 19. Trivial case ● Can we rename a column in our DB? ● Without breaking 100 applications? ● If we can't do something trivial, how can we do something important? ● If we can't evolve the schema, we are most likely not very good at developing applications 19
  • 20. Testing (2) ● Do we have code in the DB that implements critical business functionality? ● Do we consider data an important asset? ● … and it's all not tested? ● Automatic regression tests would help ● Proper refactoring cannot happen without them 20
  • 21. Database Unit Tests ● Too complex? ● No good framework? create or replace package dbunit is procedure assert_equals(expected number, actual number); procedure assert_equals(expected varchar2, actual varchar2); procedure assert_null(actual varchar2); procedure assert_not_null(actual varchar2); ... end; create or replace public synonym dbunit for dbunit; grant execute on dbunit to public; 21
  • 22. Running Unit Tests ● Anonymous PL/SQL code ● No need to change the DB ● Assertions raise_application_error with specific message if tests fail ● Rollback at the end ● Runnable with any SQL tool ● Or with ant 22
  • 23. PL/SQL Unit Test example declare xml XmlType; begin --@Test no messages in case of no changes xml := hub.next_message(0); dbunit.assert_null(xml); --@Test identification number change message hub_api.ident_number_changed('123', '007', 'PERSONAL_CODE', 'LV', '888', current_timestamp); xml := hub.next_message(1); dbunit.assert_xpath('123', '/hub/party/@source_ref', xml); end; 23
  • 24. How to deal with coupling? ● Big-Bang approach ● Usually, you can't fix all 100 apps at once ● Give up ● And afford even more technical debt? ● Transition Window approach ● Can be a viable solution 24
  • 25. Transition Window (3) ● Deprecate the old schema ● Write tests if not present ● Decide on the removal date, communicate it out ● Create the change ● Make the old schema work (scaffolding code) ● Run the tests Implement the Transition period Refactoring refactoring (old schema deprecated) completed Deploy new schema, migrate Remove old schema data, add scaffolding code and scaffolding code 25
  • 26. Dealing with unknown applications ● It's easy to eliminate all usages in ● the DB itself ● the application you are developing ● Log accesses to the deprecated schema ● Helps to find these 'unknown' applications 26
  • 27. Changelog (4) ● Doing all this needs proper tracking of changes ● Write delta-scripts (aka migrations) ● To start the transition period ● To end the transition period (these will be applied on a later date/release) ● Same scripts for ● Updating sandboxes ● Deployment to production 27
  • 28. What to refactor in a DB? ● Databases usually contain ● Data (stored according to a schema) ● Stored code ● Stored code is no different from any other code ● except that it runs inside of a database ● Database schema ● Data is the state of a database ● Maintaining the state needs a different approach from refactoring the code 28
  • 29. Upgrade/Downgrade Tool ● Upgrade tool will track/update the changelog table automatically ● Each DB will know it's state (version) ● It will be easy to upgrade any sandbox ● Downgrading possibility is also important ● Delta scripts need to be two-way, i.e. include undo statements ● It will be easy to switch to any other state – e.g. in order to reproduce a production bug 29
  • 30. Sample refactoring script -- rename KLK to CUSTOMER_ID ALTER TABLE CUSTOMER ADD COLUMN CUSTOMER_ID NUMBER; UPDATE CUSTOMER SET CUSTOMER_ID = KLK; -- keep KLK and CUSTOMER_ID in sync CREATE TRIGGER ...; --//@UNDO DROP TRIGGER ...; ALTER TABLE CUSTOMER DROP COLUMN CUSTOMER_ID; -- this will go to another script for later deployment -- finish rename column refactoring DROP TRIGGER ...; ALTER TABLE CUSTOMER DROP COLUMN KLK; --//@UNDO ... 30
  • 31. dbdeploy ● http://dbdeploy.com ● Very simple ● Runnable from ant or command-line ● Delta scripts ● Numbered standard .sql files ● Unapplied yet delta scripts run sequentially ● Nothing is done if the DB is already up-to-date 31
  • 32. liquibase ● http://liquibase.org ● More features, more complex ● Runnable from ant or command-line ● Delta scripts ● In XML format (either custom tags or inline SQL) ● Many changes per file ● Identifies changes with Change ID, Author, File ● Records MD5 for detecting of changed scripts 32
  • 33. Versioning Development DB v47.29 Pre-production Development DB DB v46.45 v47.34 Integration Production DB v45.82 v47.29 Development DB Demo v46.50 DB v46.13 Development DB v45.82 Each DB knows its release/version number and can be upgraded/downgraded to any other state 33
  • 34. Proper Versioning ● Baseline (aka skin) ● Delta scripts (migrations) ● Code changes ● Branch for a release ● New baseline after going to production ● The goal of versioning a database is to push out changes in a consistent, controlled manner and to build reproducible software 34
  • 35. Continuous Integration (5) ● CI server will verify each commit to the VCS ● By deploying it into an integration sandbox ● And running regression tests ● Fully automatically ● All the usual benefits ● Better quality, Quick feedback ● Build is always ready and deployable ● Developers are independent ● No locking, no overwriting changes!!! 35
  • 36. Teamwork (6) ● Developers ● Must work closely with Agile DBAs ● Must gain basic data skills ● Agile DBAs/DB developers ● Must be embedded into the development team ● Must gain basic application skills 36
  • 37. Tools ● Delta scripts ● dbdeploy, liquibase, deltasql ● Easy to write our own! ● PL/SQL code vs Intellij IDEA (Java) Oracle SQL Developer 37
  • 38. Enabling database refactoring (1) Development Sandboxes (2) Regression Testing (3) Transition Window approach (4) Versioning with Changelog & Delta scripts (5) Continuous integration (6) Teamwork & Cultural Changes 38
  • 39. The Catalog ● Scott Ambler and Pramod Sadalage have created a nice catalog of DB refactorings ● http://www.ambysoft.com/books/refactoringDatabases.html ● Classification ● Structural ● Data Quality ● Referential Integrity ● Architectural ● Method (Stored code) ● Transformations (non-refactorings) 39
  • 40. Best practices ● Refactor to ease additions to your schema ● Ensure the test suite is in place ● Take small steps ● Program for people ● Don’t publish data models prematurely ● The need to document reflects a need to refactor ● Test frequently (according to Scott Ambler) 40