elegant database modifications
DESCRIPTION
Discussion of the issues involved in changing database schemas and data.TRANSCRIPT
![Page 2: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/2.jpg)
Database changes are hard
We need to make them all the time
Let's make them easy
![Page 3: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/5.jpg)
Problems
![Page 6: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/6.jpg)
The database is at the bottom of the stack
![Page 7: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/7.jpg)
View
Legacy codeController Optimisations
ORM
Database & other persistent storage
Web App Hierarchy
![Page 8: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/8.jpg)
Keeping track of changes during
development is hard
![Page 9: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/10.jpg)
Can't specify the schema needed
in the code
![Page 11: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/11.jpg)
The data needs to change too
![Page 12: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/13.jpg)
All databasesare different
![Page 14: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/14.jpg)
• PostgreSQL can do 'alter table' in a transaction.
• MySQL can alter several columns in one statement.
• etc, etc...
![Page 15: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/16.jpg)
Developers arenot sys-admins
![Page 17: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/17.jpg)
Changing the schema might lock the database
![Page 18: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/18.jpg)
Deploying changes to live systems involves
other steps
![Page 19: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/19.jpg)
• 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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/21.jpg)
Solutions!
![Page 22: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/22.jpg)
Surely the answer is on CPAN?
![Page 23: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/23.jpg)
DBIx::Class::Schema::Versioned
DBIx::Migration
DBIx::Migration::Directories
DBIx::SchemaView
DBIx::VersionedSchema
Jifty::DBI::Schema
Yes!
![Page 24: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/25.jpg)
Add metadata to the schema
![Page 26: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/27.jpg)
Specify the schema version in the code
![Page 28: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/29.jpg)
Create a framework for applying changes
![Page 30: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/30.jpg)
F*******k !
![Page 31: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/31.jpg)
Sample code
![Page 32: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/32.jpg)
Sample code
![Page 33: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/33.jpg)
Sample code
![Page 34: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/34.jpg)
Sample code
![Page 35: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/35.jpg)
Sample code
![Page 36: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/38.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/39.jpg)
Make it easy for developers
![Page 40: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/40.jpg)
• 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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/41.jpg)
Make it easy forsys-admins
![Page 42: Elegant Database Modifications](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/42.jpg)
• 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](https://reader036.vdocuments.us/reader036/viewer/2022081401/559075211a28abcf118b4639/html5/thumbnails/43.jpg)
Questions?Opinions?
Suggestions?