elegant database modifications

43
Elegant database modifications Edmund von der Burg [email protected]

Upload: evdb

Post on 29-Jun-2015

1.474 views

Category:

Technology


0 download

DESCRIPTION

Discussion of the issues involved in changing database schemas and data.

TRANSCRIPT

Page 1: Elegant Database Modifications

Elegant database modifications

Edmund von der [email protected]

Page 2: Elegant Database Modifications

Database changes are hard

We need to make them all the time

Let's make them easy

Page 3: Elegant Database Modifications

Disclaimer

The opinions presented in this talk are those of the presenter only. They are based on his limited experience. There are no absolutes in this talk.

Your own experiences may differ.

Please interrupt with questions,corrections and suggestions.

Page 4: Elegant Database Modifications

For far too long we have been throwing SQL scripts at standalone binary clients,

it's about time we had some way to throw them at the DBI instead.

Adam Kennedy - SQL::Script

Page 5: Elegant Database Modifications

Problems

Page 6: Elegant Database Modifications

The database is at the bottom of the stack

Page 7: Elegant Database Modifications

View

Legacy codeController Optimisations

ORM

Database & other persistent storage

Web App Hierarchy

Page 8: Elegant Database Modifications

Keeping track of changes during

development is hard

Page 9: Elegant Database Modifications

No way to refer to schema versions

• SQL standard is not helpful

• Need a standard place to store metadata

• Needs to be easy to query

Page 10: Elegant Database Modifications

Can't specify the schema needed

in the code

Page 11: Elegant Database Modifications

The data needs to change too

Page 12: Elegant Database Modifications

create table locations ( postcode varchar(8) not null

);

,latitude float ,longitude float

"ALTER TABLE"to add the

lat & long fields

run code to populate lat& long from postcode

not nullnot null

"ALTER TABLE" to make lat & long"NOT NULL"

Page 13: Elegant Database Modifications

All databasesare different

Page 14: Elegant Database Modifications

• PostgreSQL can do 'alter table' in a transaction.

• MySQL can alter several columns in one statement.

• etc, etc...

Page 15: Elegant Database Modifications

I don't know why thischange was made

• The schema in the database does not contain a history

• No clear place to put documentation on the schema

• Changes to the schema are not stored in version control - so don't appear alongside code changes in diffs

Page 16: Elegant Database Modifications

Developers arenot sys-admins

Page 17: Elegant Database Modifications

Changing the schema might lock the database

Page 18: Elegant Database Modifications

Deploying changes to live systems involves

other steps

Page 19: Elegant Database Modifications

• Display a "down for maintenance" message or switch to "read only" mode.

• Backup everything before starting

• Switch off import & backup systems

• Disable alerting

• Upgrade codebase / commision new virtual servers.

• Switch to new systems when complete

Page 20: Elegant Database Modifications

FEAR"Don't change the database"

"I think that the import system needs

that column"

"Just put a hack in the code to fix that"

"Email that change to Bob and he'll apply it during the maintenance window"

"Did that change get applied?"

"We can't really test it without going live"

Page 21: Elegant Database Modifications

Solutions!

Page 22: Elegant Database Modifications

Surely the answer is on CPAN?

Page 23: Elegant Database Modifications

DBIx::Class::Schema::Versioned

DBIx::Migration

DBIx::Migration::Directories

DBIx::SchemaView

DBIx::VersionedSchema

Jifty::DBI::Schema

Yes!

Page 24: Elegant Database Modifications

No!

None deal with modifying data in code

Can't specify schema version in the code

Too smart - try do work out the SQL for you

Too dumb - don't give you the control you need

ORMs can generate the SQL statements, but should not be applying them

Page 25: Elegant Database Modifications

Add metadata to the schema

Page 26: Elegant Database Modifications

create table __metadata ( name varchar(100) primary key, value text not null);

insert into __metadata (name, value) values ( 'schema_version', 0 );

insert into __metadata (name, value) values ( 'your_key', 'your_value');

Page 27: Elegant Database Modifications

Specify the schema version in the code

Page 28: Elegant Database Modifications

use MyApp::Schemas Main => 72, TheSchwartz => 3, OtherSchema => 12;

A module in your namespace that contains all the schema info

The various schemas you've defined

The versions you require for this code

Page 29: Elegant Database Modifications

Create a framework for applying changes

Page 30: Elegant Database Modifications

F*******k !

Page 31: Elegant Database Modifications

Sample code

Page 32: Elegant Database Modifications

Sample code

Page 33: Elegant Database Modifications

Sample code

Page 34: Elegant Database Modifications

Sample code

Page 35: Elegant Database Modifications

Sample code

Page 36: Elegant Database Modifications

HierarchyMyApp::SchemasMyApp::Schemas::MainMyApp::Schemas::Main::v001MyApp::Schemas::Main::V002

• Not happy with the v00x naming

• Each level does inherits from the level above

• Bottom level inherits from module on CPAN

• Might call this DBIx::Schemas ?????

Page 37: Elegant Database Modifications

Hooks / Handlers:

• start / end of upgrade

• start / end of version jump

• start /end of each step in a version jump

• .... lots more .....

Page 38: Elegant Database Modifications

And also:

• Tests - embed them next to the changes they relate to

• Specify the steps to take, not make them

• Opens up potential for other tools to apply the steps

Page 39: Elegant Database Modifications

Make it easy for developers

Page 40: Elegant Database Modifications

• One clear place to put the schema/data changes.

• Enforce schema changes in code (could autoupgrade when developing)

• Put schema changes in version control

• Know what the schema should look like

Page 41: Elegant Database Modifications

Make it easy forsys-admins

Page 42: Elegant Database Modifications

• Changes to schema/data clearly defined

• Intention of each step is documented

• Can run the steps (on stage server) to see what happens on real data

• Can add in hooks to ease the upgrade

• Whatever they want.......

Page 43: Elegant Database Modifications

Questions?Opinions?

Suggestions?