1Copyright © 2012, Oracle and/or its affiliates. All...

88
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 1

Transcript of 1Copyright © 2012, Oracle and/or its affiliates. All...

Page 1: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.1

Page 2: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Patterns of SaaS: Database refactoringLeonid Igolnik, Marcin Burlinski

Insert Picture Here

Page 3: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.3

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Page 4: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.4

Agenda

Introductions

Fundamentals of Database Refactoring

“Putting it all together” - a case study

Q&A

Page 5: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.5

About Us Based in California

Java Developer for over 10 years

Over 13 years of SaaS experience

Avid traveller and photographer

Page 6: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.6

About Us - Marcin

Based in Poland

Java Developer for over 10 years

Over 10 years of SaaS experience

Not a big fan of SQL

Avid traveller

Page 7: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.7

Are you a ...

Developer DBA Some who likes writing complex SQL Someone who maintains their own schema and writes change scripts Someone who has a tool do it for you (Play!, Spring Roo, etc..)

Page 8: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.8

Database refactoring fundamentals

Insert Picture Here

Page 9: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.9

Page 10: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.10

I live in the world of enterprise applications, and a big part of enterprise application development is working with databases. In my original book on refactoring, I picked out databases as a major problem area in refactoring because refactoring databases introduces a new set of problems. These problems are exacerbated by the sad division that’s developed in the enterprise software world where database professionals and software developers are separated by a wall of mutual incomprehension and contempt.

Martin FowlerChief Scientist, ThoughtWorks.

Page 11: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.11

I live in the world of enterprise applications, and a big part of enterprise application development is working with databases. In my original book on refactoring, I picked out databases as a major problem area in refactoring because refactoring databases introduces a new set of problems. These problems are exacerbated by the sad division that’s developed in the enterprise software world where database professionals and software developers are separated by a wall of mutual incomprehension and contempt.

Martin FowlerChief Scientist, ThoughtWorks.

Page 12: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.12

Database refactoring stakeholders

Developers DBAs QA Business Analysts

Page 13: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.13

A new scientific truth does not triumph by convincing its opponents and making them see the light, but rather because its opponents eventually die, and a new generation grows up that is familiar with it.

Max Planck

Page 14: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.14

But there are great benefits to be gained

Designing data model up front is hard At times upfront data modeling is impossible Knowing how to evolve your data model efficiently improves your

system architecture Much like getting rid of “Code Smells” using code refactoring , you

can get rid of “Database Smells”

Page 15: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.15

Database smells

Multipurpose column Multipurpose table Redundant data Too many columns Fear of change

Page 16: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.16

Mechanics of database refactoring

Page 17: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.17

A database refactoring is a simple change to a database schema that improves its design while retaining both its behavioral and informational semantics.

Ambler, Scott W.; Sadalage, Pramod J. (2006-03-03). Refactoring Databases: Evolutionary Database Design (Addison-Wesley Signature Series) (Kindle Locations 637-638). Addison-Wesley Professional. Kindle Edition.

Page 18: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.18

A database refactoring is a simple change to a database schema that improves its design while retaining both its behavioral and informational semantics.

Ambler, Scott W.; Sadalage, Pramod J. (2006-03-03). Refactoring Databases: Evolutionary Database Design (Addison-Wesley Signature Series) (Kindle Locations 637-638). Addison-Wesley Professional. Kindle Edition.

Page 19: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.19

Application architecture types

Database

Application

Database

ApplicationAnother

Application

Another Applicationyou don’t

know about

OtherDatabases

Test Scripts

Test Code BI Tools

Page 20: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.20

Key steps of refactoring process

1. Agree on the changes with all stakeholders

2. Refactor the schema

3. Migrate the data as needed

4. Refactor external access programs

5. Run your tests

6. Version control the change

7. Announce the change to other consumers

8. Deploy to production

9. Deprecate the old schema

Page 21: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.21

Refactoring types

Insert Picture Here

Page 22: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.22

Database refactoring categories

Structural Data Quality Referential Integrity Functional Non-refactoring transformations

Page 23: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.23

Structural refactoring types

Replace Column Drop Column Rename Table Rename Column Introduce Calculated Column Introduce Surrogate Key Replace One-to-Many With

Associative Table Move Column Merge Columns

Merge Tables Drop Table Drop View Rename View Replace Large Object (LOB)

With Table Replace Surrogate Key With

Natural Key Split Column Split Table

Page 24: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.24

Structural: Rename column

• Readability of schema• Database porting (e.g. to remove

reserved keywords)

Motivation

• Cost of application change vs. readability

Tradeoffs

• Introduce new column• Add sync trigger (if multi-app access)• Consider renaming referencing

columns

Update Mechanics Data Migration

• Copy data

Page 25: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.25

Example

Page 26: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.26

Example

Page 27: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.27

Data Quality refactoring types

Make Column Non-Nullable Make Column Nullable Drop Default Value Add Default Value Introduce Column Constraint Drop Column Constraint

Move Data Replace Type Code with

Property Flags Introduce Column Format Consolidate Key Strategy Apply Standard Type Apply Standard Codes Add Lookup Table

Page 28: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.28

Referential Quality refactoring types

Add Foreign Key Constraint Introduce Soft Delete

Add Trigger for Calculated Column

Drop Foreign Key Constraint Introduce Cascading Delete Introduce Hard Delete Introduce Trigger for History

Page 29: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.29

Key Considerations: Testing

Stored procedures Referential integrity View definitions Default values Data invariants (constraints) Data migration testing

Page 30: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.30

Putting it all together – or how to apply 650,000 changes in a few hours without loosing sleep

Insert Picture Here

Page 31: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.31

Approaches to Managing Multi-Tenant Data

Page 32: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.32

Approaches to Managing Multi-Tenant DataSeparate Database

Separate Schema

Shared Schema

Page 33: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.33

Approaches to Managing Multi-Tenant DataSeparate Database

Separate Schema

Shared Schema

Pros

• Database level data isolation

• Out of the box per tenant recovery

Cons

• Can be expensive to operate

• Inherit limitations of number of databases per instance

• Schema sync

Page 34: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.34

Approaches to Managing Multi-Tenant DataSeparate Database

Separate Schema

Shared Schema

Pros

• Database level data isolation

• Out of the box per tenant recovery

• Guaranteed schema sync

Cons

• Can be expensive to operate

• Inherit limitations of number of databases per instance

• Schema sync

• Per tenant operations are more difficult

• Index balancing issues

Page 35: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.35

Approaches to Managing Multi-Tenant DataSeparate Database

Separate Schema

Shared Schema

Pros

• Database level data isolation

• Out of the box per tenant recovery

• Database level data isolation

• Guaranteed schema sync

Cons

• Can be expensive to operate

• Inherit limitations of number of databases per instance

• Schema sync

• Inherit limitations of number of databases per instance

• Schema sync• No simple per

tenant recovery

• Per tenant operations are more difficult

• Index balancing issues

Page 36: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.36

The application

~6,000 tenants Over 70,000,000 transactions a week Average response time ~170ms Peak transaction volume ~20,000/min Average transaction volume ~4,000-7,000/min

Page 37: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.37

Our approach: Release model

Single code base for all customers (configuration over customization) Single release date for all customers every 8-10 weeks Nearly zero downtime during release No rollback past defined point of no-return Releases are done under average transaction workloads Typical release contains about 70-200 individual changes to the

database (DML + DDL)

Page 38: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.38

Management of database artifacts

Data Definition Language (DDL) scripts for the schema– Table definitions

– View definitions

– Referential and other constraints

– Indexes

– Misc objects such as sequences etc

O/R mapping configs Stored procedures/triggers etc. Data load/extract and migration scripts Test data generation scripts and test scripts

Page 39: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.39

Per release script types

DDL – Data Definition Language– CREATE

– ALTER

– DROP

– TRUNCATE

– RENAME

DML – Data Manipulation Language– INSERT

– DELETE

– UPDATE

Page 40: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.40

All changes are split into small incremental changes

Page 41: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.41

Script naming

<file_sequence>_<release_number>_ <project_name>_<brief_description>_

[ddl|dml].sql

Page 42: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.42

Script naming

<file_sequence>_<release_number>_

<project_name>_<brief_description>_ [ddl|dml].sql

Page 43: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.43

Script naming

<file_sequence>_<release_number>_

<project_name>_<brief_description>_ [ddl|dml].sql

Page 44: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.44

Script naming

<file_sequence>_<release_number>_ <project_name>_<brief_description>

_ [ddl|dml].sql

Page 45: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.45

Script naming

<file_sequence>_<release_number>_ <project_name>_<brief_description>

_ [ddl|dml].sql

Page 46: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.46

Script naming

<file_sequence>_<release_number>_ <project_name>_<brief_description>_

[ddl|dml].sql

Page 47: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.47

File naming example

110_9.5_create_we_pm_review_competency_section_table.sql 120_9.5_add_goal_section_weight_into_we_pm_review.sql 130_9.5_add_mandatory_fields_to_list_view.sql 140_9.5_COMP_add_comp_category.sql 150_9.5_COMP_add_pay_type.sql 160_9.5_COMP_add_behavior.sql 170_9.5_COMP_add_frequency.sql

Page 48: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.48

Script template

Author - where author name is listed Project - name of the project for which the script is Description - purpose of the script

Page 49: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.49

Make scripts safely re-runnable if you can

Provide standard pattern for DML and DDL Scripts

Page 50: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.50

Common DML patterns

Avoid bulk updates – use DB SQL and cursors with periodic commits Create and use library of reusable templates for common tasks Backup the data you alter into a table with standard naming

convention:– BACKUP_<release_number>_<script_number>_<script_type>

Automatically delete backup tables after defined period of time Make your DBA a friend

Page 51: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.51

Artifact organization in source control

Releases– X.X

– Component Name

PRE

MAIN

POST

Page 52: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.52

PRE – MAIN - POSTPRE MAIN POST

Page 53: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.53

PRE – MAIN - POSTPRE MAIN POST

• Changes that keep schema backward compatible:

• New Nullable columns

• New Tables• Seeding for data

compatible with the old code version

Page 54: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.54

PRE – MAIN - POSTPRE MAIN POST

• Changes that keep schema backward compatible:

• New Nullable columns

• New Tables• Seeding for data

compatible with the old code version

• Minimal changes that are required to make the schema compatible with the new version of the app.

• Those changes brake backward compatibility

• Require application downtime

Page 55: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.55

PRE – MAIN - POSTPRE MAIN POST

• Changes that keep schema backward compatible:

• New Nullable columns

• New Tables• Seeding for data

compatible with the old code version

• Minimal changes that are required to make the schema compatible with the new version of the app.

• Those changes brake backward compatibility

• Require application downtime

• Changes requiring new application code:

• Not Nullable fields constraints

• New constraints• Seeding data

compatible only with the new code version

Page 56: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.56

Page 57: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.57

SQL Review

Every database change gets reviewed by multiple senior engineers SQL reviews start mid-release dev cycle and continue until code

freeze Tools:

– Crucible or your favorite code review tool

Page 58: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.58

Environments

Development(frequent

deployments)

• Development sandbox

• Project Integrationsandbox

Page 59: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.59

Environments

QA(controlled

deployments)

Development(frequent

deployments)

• Demo sandbox

• Pre-productionQA

• Development sandbox

• Project Integrationsandbox

Page 60: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.60

Environments

• Production

QA(controlled

deployments)

Development(frequent

deployments)

Production(highly controlled

deployments)

• Demo sandbox

• Pre-productionQA

• Development sandbox

• Project Integrationsandbox

Page 61: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.61

Environments

QA(controlled

deployments)

Development(frequent

deployments)

Production(highly controlled

deployments)

• Apply pre main post as developed at times out of sequence

Page 62: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.62

Applying DML and DDL to Dev/QA/Production

Page 63: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.63

Environments

QA(controlled

deployments)

Development(frequent

deployments)

Production(highly controlled

deployments)

• Apply pre main post as developed at times out of sequence

• Apply pre main post as developed at times out of sequence

• Perform final integration test

Page 64: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.64

Environments

• Apply in order

QA(controlled

deployments)

Development(frequent

deployments)

Production(highly controlled

deployments)

• Apply pre main post as developed at times out of sequence

• Apply pre main post as developed at times out of sequence

• Perform final integration test

Page 65: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.65

Create DML/DDL

Overall workflow

Page 66: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.66

Create DML/DDL Release to dev and QA

Overall workflow

Page 67: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.67

Create DML/DDL Release to dev and QA

Review SQL

Overall workflow

Page 68: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.68

Create DML/DDL Release to dev and QA

Review SQL

Final integration

test

Overall workflow

Page 69: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.69

Create DML/DDL Release to dev and QA

Review SQL

Final integration

testRelease

Overall workflow

Page 70: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.70

Create DML/DDL Release to dev and QA

Review SQL

Final integration

testRelease

Post release cleanup

Overall workflow

Page 71: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.71

PRE

010_pre

020_pre

MAIN

010_main

020_main

POST

010_post

020_post

PRE

210_pre

220_pre

MAIN

210_main

220_main

POST

210_post

220_post

Overall workflow

QA QA

Page 72: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.72

PRE

010_pre

020_pre

MAIN

010_main

020_main

POST

010_post

020_post

PRE

210_pre

220_pre

MAIN

210_main

220_main

POST

210_post

220_post

PRE

210_pre

220_pre

MAIN

210_main

220_main

POST

210_post

220_post

Overall workflow

QA

QA

QA

Page 73: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.73

Run PRE

Final integration test

Page 74: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.74

Run PRERegression

test previous build

Final integration test

Page 75: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.75

Run PRERegression

test previous build

Run Main and Deploy new build of the

app

Final integration test

Page 76: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.76

Run PRERegression

test previous build

Run Main and Deploy new build of the

app

Regression test new build

Final integration test

Page 77: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.77

Run PRERegression

test previous build

Run Main and Deploy new build of the

app

Regression test new build Run POST

Final integration test

Page 78: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.78

Run PRERegression

test previous build

Run Main and Deploy new build of the

app

Regression test new build Run POST Regression

test new build

Final integration test

Page 79: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.79

Run PRE Shutdown the app Run MAIN

Deploy new version of

the appRun POST Run

Cleanup

Day/Week of the release

Page 80: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.80

Run PRE Shutdown the app Run MAIN

Deploy new version of

the appRun POST Final restart

(optional)Run

Cleanup

Day/Week of the release

Page 81: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.81

Common issues

Schema inconsistencies Data inconsistencies Transaction log space Execution time

Page 82: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.82

Database Schema Audits

Periodic audit of schema structure Combination of manual and automated tools

Page 83: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.83

Lessons learned

Document your Schema change process Managed you database artifacts in source control Have your own version of an SQL Policeman Develop reusable templates for DML and DDL Have repeatable automated process to apply changes to all

environments: Dev, QA, Staging and production Make a friend out of your DBA

Page 84: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.84

Key Messages

Database refactoring is HARD Automate everything to ensure repeatable process Review everything Backup crucial data Test as much as you reasonably can Don’t reinvent the wheel – follow the pattern

Page 85: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.85

Further reading

Agile data: http://www.agiledata.org/ http://www.amazon.com/Refactoring-

Databases-Evolutionary-paperback-Addison-Wesley/dp/0321774515

DBUnit SQLUnit

Page 86: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.86

Q&A

Page 87: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.87

Page 88: 1Copyright © 2012, Oracle and/or its affiliates. All ...s3-eu-west-1.amazonaws.com/presentations2013/58_presentation.pdf · Backup the data you alter into a table with standard naming

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.88