The Schema Project: Database abstraction, reflection, and migration
Almost a year after first discussing the idea on the Drupal developer’s mailing list I am finally ready to announce the Drupal Schema project. The goal of the project is to provide a database schema abstraction layer for Drupal. The benefits of the project will include more reliable compatibility with a greater range of database systems, reduced install-related code size, centralization of the growing schema information required by or for other modules (such as Views and CCK), and a wide variety of other advanced capabilities that will no doubt arise once the system is in place.
The killer application of the Schema Project’s capabilities, I believe, will be “incremental data migration”: the ability to move almost any subset of a Drupal system (nodes, Views, Workflows, CCK types, configuration settings, etc.) from one live, running system to another. Incremental data migration will make it possible to develop, test, stage, and promote site changes without having to manually re-create configuration and data at each step along the way.
I’ll be at the 2007 OSCMS Summit and hope to gather some momentum for this project while there.
What is database schema abstraction?
A database schema is a description of the structure of a database. For a relational database, the schema specifies the tables, columns, and relationships among them. In Drupal’s case, the schema specifies things like: “A node is identified by a nid and has a title, author, creation time, body, and …”.
A database schema abstraction is some kind of language or representation of a database schema that allows specifying, studying, or manipulating a database schema. Database schemas and their abstractions are a large topic in computer science and software development. There are schema modelling languages, entity relationship diagrams, and all manner of approaches and tools for working with them.
I am not a database systems expert and know very little about the science of database schema abstractions in general (though I imagine I’ll learn more than I wanted to know through this project). I do not want to and cannot move Drupal to the cutting edge database modelling. Instead, my goal is to take an incremental step forward that I believe will provide substantial benefit for relatively minor effort.
What is database schema reflection?
It would be possible (and quite useful) to define Drupal’s database schema in documentation that can only be read by humans. My goal, however, is to provide Drupal with “schema reflection”: the ability use and manipulate its own schema at run-time.
The Forms API provides a perfect example. Before Drupal 4.7, each form was separately constructed directly as HTML. For one module to modify another module’s form, it could either add things at the top or bottom or use string replacement to change things in the middle. This approach was error-prone and inflexible. Starting with Drupal 4.7, forms are represented as structured data until the very last minute. Now, for one module to modify another module’s form, it uses the data structure to access each element and attribute by name or insert new elements at any location. This change to data-driven forms provided an enormous increase in power and flexibility and, not incidentally, made form-generation code much easier to read and maintain.
In current versions of Drupal, database tables are created by explicit CREATE TABLE statements embedded in each module’s .install file. Since every SQL database engine uses different syntax for these statements, a separate copy of each one is required for each database. Each module has a “schema version number” stored in a central table but if a module’s code and schema get out of date (and they can) there is no way for Drupal to notice or recover from the inconsistency.
With schema reflection, Drupal’s current database schema as the modules expect it to be is represented in a single data structure. Each module only needs to define its tables once and the Schema module can create them in whatever DBMS engine is being used. The Schema module can inspect the actual current structure of the database and compare it to the module-generated schema, noticing and warning (or possibly acting) on any changes.
Finally, schema relfection will allow Drupal to act on semantic information about the schema itself. For example, the schema data structure can specify that “the comment table joins to the node table using the nid field as a many-to-one relationship.” The as-yet-undesigned Migration module could then use this information to determine that, in order to migrate a node from one system to another, it must also gather all the comments connected to that node and migrate them as well.
What will the code look like?
The code I have created so far is a very early prototype. I am certain it contains design errors and ommissions. Everything about it is subject to change. I am presenting it here just to give an idea of what I have in mind.
Every module’s tables are defined in a data structure (just like a form), indicating its columns and data types, keys, etc. Not surprisingly, it looks very much like a Views hook_views_tables structure. For example:
$schema['node'] = array(
'cols' => array(
'nid' => array('int(10) unsigned', NOT_NULL),
'vid' => array('int(10) unsigned', NOT_NULL, 'default' => '0'),
'type' => array('varchar(32)', NOT_NULL),
// and so forth
),
'keys' => array(
'PRIMARY' => array('nid, vid', 'UNIQUE'),
'vid' => array('vid', 'UNIQUE'),
'nid' => array('nid'),
// and so forth
));The ‘cols’ array has an element for each column.
Each column specifier includes the mandatory information name, type, and whether or not it can be NULL as well as optional information such as default values.
I have the mandatory information as positional elements simply to reduce typing (to eliminate ‘name’ => $name, ‘type’ => $type, etc.). This may or may not be a good idea.
Optional information can include proprietary DBMS parameters, semantic information such as ‘join n-to-1’ => ‘tablename.column’, module-specific information such as what Views needs to know for fields, filters, arguments, and sorts, and so forth.
The ‘keys’ array has an element for each table index.
Each key specifier includes the name of the key/index, the columns included in the index, and whether or not it is a unique index.
Each module that wants to define tables in the schema implements hook_schema:
function mymodule_schema() {
$schema['#version'] = 1;
$schema['mymodule_table_1'] = array(/* table definition */);
$schema['mymodule_table_2'] = array(/* table definition */);
// etc
return $schema;
}The Schema module invokes hook_schema, collects the results, decide what tables need to be created, and hands those tables off to a DBMS engine driver that translates into appropriate CREATE TABLE statements.
An open question is how schema updates will occur. The simple solution is to continue using the present install approach:
function mymodule_schema_update_2() {
// Explicitly update the schema from version 1 to version 2.
db_add_column('mymodule_table_1', 'new_column');
db_remove_column('mymodule_table_1', 'old_column');
}Another possibility is to include schema version information in the table definitions:
$schema['#version'] = 2;
$schema['mymodule_table_1'] = array(
'cols' => array(
'new_column' => array(...,
'version_added' => 2),
'old_column' => array(...,
'version_removed' => 2),
// etc
));When the Schema module sees that the version for mymodule had changed from 1 to 2, it can scan through the tables looking for things changed in version 2 and automatically add or remove them (or invoke an update callback specified in the array). One advantage of this approach is that it stores current and historical information about schema structure in one place. However, there will probably always be some kinds of schema updates that require custom code.
Core or contrib?
I do not see any reason that the Schema system needs to be part of Drupal core. Any module that wants to participate in Schema defines hook_schema; any module that does not just continues to call db_query(‘CREATE TABLE’) in hook_install. If Schema is successful, over time more and more modules will support it, just a over time more and more modules have added support for Views.
Schema would not be very useful if Drupal’s core tables were not included, of course. The Schema module will provide the schema information for core tables via its own hook_schema.
What does this buy us?
The Schema proeject will provide many benefits:
Several modules (e.g. Views) already need schema reflection; this just centralizes the information and makes it much easier to create new modules that need reflection. A module that wants to support Views or other similar modules can store any necessary data in its $schema array. In fact, many modules will probably not have to do anything more to support such reflection-requiring modules.
Code size reduction and elimination of duplicate code. The automatically-generated schema for core is only slightly larger than all the explicit CREATE TABLE statements for a single database engine, but the single schema array will support multiple engines instead of requiring a duplicate set of CREATE TABLE statements for each engine.
The Schema module can use database inspection to validate the database layout against the defined schema, reporting errors and/or disabling any modules whose tables are not what they expect.
It can provide hook_schema_alter, allowing modules to alter other modules’ tables in a clean way. For example, a node-enhancing module could add additional columns to the node table, eliminating the need to join against its own custom table.
As already described, it is a huge step towards enabling incremental data migration across Drupal systems.
The Forms API made all kinds of things possible that were not before, many of them not foreseen until after FAPI existed. Schema will, too.
Arguable, this approach is somewhat more complicated than writing a single MySQL CREATE TABLE statement. Supporting Views is extra effort, too, but has a big payoff for module developers; so will this. I assert that it is easier to create a structure like this once than it is to write and maintain separate SQL statements for MySQL, Postgres, Oracle, Sybase, MS SQL Server, and god knows what else.
Current status and call for contributors
The Schema module currently knows how to:
Inspect a MySQL database to generate a schema array for existing tables and indexes (and, quite helpfully, output the PHP code to build the $schema array for those tables).
Call hook_schema for all modules that provide it.
Identify which tables returned by hook_schema do not exist in the database.
Generate and execute MySQL CREATE TABLE statements for missing tables.
There is a lot to be done:
The structure of the $schema array needs to be worked out.
General data type names and other column and index fields need to be defined (right now the code assumes MySQL-compatible strings).
A Schema engine driver for (at least) Postgres needs to be implemented.
Much more, I’m sure.
Please let me know if you’d like to help out!
Comments
There is a lot of interest
There is a lot of interest in the "killer app" aspect of migration between test/staging/production etc.
Why wouldn't you define
Why wouldn't you define these meta-schemata in XML? It seems that this is precisely what XML was created for.
I have a few answers to your
I have a few answers to your question:
1. The goal of this project is to allow Drupal to access and use information about its own schema (what I'm calling "schema reflection"). Sure, we could define the schema in XML instead of PHP arrays, but then we would just need to write some code to translate it from XML into PHP to gain the advantages of reflection. That wouldn't be too hard, but why bother? (*)
2. Drupal programmers are used to coding in PHP. They write their modules in PHP and define all their forms in structured PHP arrays. Not all of them are familiar with XML. I think the value of sticking to existing languages and paradigms outweighs the benefit of using XML just for the sake of using it.
3. A schema defined in XML would logically go into a different file than the PHP code which uses the schema. The schema update mechanism, as I described, is likely/certain to require custom PHP code in at least some cases. Having the schema definition and schema update code in different files separates them where I think it would be better to keep them together.
All that said, the potential upside ((*) i.e. "why bother?") to using XML is that it will probably lead better towards higher-level schema-description systems (ERDs, UML, whatever). As I said in my article, however, those higher-level systems are not my short-term goal. I'd like to create a system that the Drupal community will embrace and use. Too much abstraction or required new learning upfront will prevent that.
Thanks for the feedback!
There's actually a new
There's actually a new PHP-DB-Abstraction mailing list that is discussing DB-agnostic XML-based schema definitions as we speak. You may want to look into getting involved with them, too.
sounds very promising,
sounds very promising, barry. you are right that partial data migration is a killer app.
in addition to Views, you'll want to reuse relevant code and ideas from these projects:
- importexportapi. especially their definitions.
- MDB2 is database abstraction layer that is not that helpful, but MDB2 schema is an accompanying DB independant schema definition layer. Thats pretty nice. It is XML, which is non optimal compared to PHP arrays as you noted. But still worth borrowing ideas and maybe code. There are lots of drivers and tools available for this.
Are you proposing this as
Are you proposing this as part of the Drupal core, or as a contrib module?
Contrib, at least initially.
Contrib, at least initially. From the article:
It is possible that Schema will require some patches, new hooks, or whatnot in core in order to work. Time will tell.
Schema necessary
Schema necessary precondition for migration?
I am very very interested in the incremental migration aspect of your work.
The Repligard tool (http://www.midgard-project.org/documentation/concepts-repligard/) of the Midgard CMS is the only such tool in open source CMSses. In a nutshell, this tool uses schema information in XML files and relies on a separate repligard table that maintains globally unique IDs (GUIDs) for content objects and replicates content through XML files.
Another, commercial CMS that I am familiar with, SixCMS (http://sixcms.de), uses a very similar aproach based on GUIDs but allows incremental replication to use SOAP requests between a source and a target CMS.
The use of GUIDs needs to be considered when doing application development:
- Updates to content objects need to be tracked in the repligard table. This may only be valid for deletes.
- No longer is it permissable to use numeric IDs which are only locally valid but will change once a piece of content has been migrated. Instead, handcrafted content references need to use GUIDs.
As Moshe mentioned above, Drupal already has the importexportapi. As far as I am aware however, this does not maintain GUIDs. Adding these to Drupal will be the most important step.
Looking forward to you next moves.
--
Olav
Olav, thanks for your
Olav, thanks for your feedback! Ironically, just today I started writing a post for this site explaining my ideas for the Migrate module (it isn't done yet) and, yes, I do think Schema is a necessary precondition for doing it well. I am aware of the issue with migrating IDs and have a plan for it.
I was unaware of Midgard's Repligard tool and will certainly look into it.
Unfortunately, fonts still
Unfortunately, fonts still don’t look any crisp in Inkscape
Online Degree
# Understanding the mish-mash
# Understanding the mish-mash of nested tables, 20th century HTML, and JavaScript dependencies that most CMS admin interfaces consist of is near impossible.
Online Diploma|Online Certificate
Online Schools|Online Education
haha
haha
Choose, buy and shop for on sale tiffany jewelry including Tiffany & Co Silver Necklace, Pendants, Bangles, Bracelets, Earrings, Rings and Accessories.
tiffany jewelry
We will surprise to find the high quality tiffany jewelry in much.
Everyone will focus on tiffany and co
Tiffany Bracelets
Tiffany Earrings
Tiffany Necklaces
Tiffany Rings
Official website for Ed Hardy
Official website for Ed Hardy Eyewear. The lifestyle Ed Hardy brand is based on the original Ed Hardy clothing, accessories, energy drink, Ed Hardy randed by
ed hardy ed hardy clothes
Ed hardy online store, whether you are looking for fashion Ed Hardy Clothing items, then you come to the right place, we offer a variety of ed hardy items.
Don cheap ed hardy is an American tattoo artist born and raised in Southern California in 1945. A pupil of Sailor Jerry, Hardy is recognized for incorporating.
Selling ed hardy men longsleeve,ed hardy swimsuit,ed hardy men swimsuit,ed hardy boot.
ed hardy clothes
west ed hardy,edhardy discount,ed hardy clothing,ed hardy shoes Commodity New styles have just arrived!
ED Hardy womens Hoodies
ED Hardy Womens Shoes
online bachelors degree |
online bachelors degree | online doctorate degree | online learning
work experience degree |
work experience degree | Online Degree
That little blue box -
That little blue box - everyone woman dreams of seeing it come their way. tiffany jewelry jewelry is world renowned for its stunning quality and top of the line artisanship; however, it also has a reputation for having a price tag that is way out of most people's leagues.
ThankS..muhabbet|chat
ThankS..muhabbet|chat siteleri |sohbet|yonja|forum siteleri|mirc indir|sohbet|yonja|sohbet|sohbet|
kizlarla sohbet|dini sohbet|islami sohbet|sohbet/sohbet odalari|sohbet||Sohbet|sohbet|
netlog||sohbet|netlog|yonja|sohbet chat
mirc
cinsel sohbet
The immediate past big event,
The immediate past big event, the 63rd Annual Tony Awards, was presented in the Radio City Music Hall of New York. tiffany & co tiffany jewelry saleThe popular guys of Hollywood all flocked in for this royal ceremony. tiffany charm bracelet tiffany shop onlineSusan Sarandon, Colin Hanks, Anne Hathaway, Jane Fonda and other women all dressed up fit to kill and tiffany bracelet, tiffany and co wining a round of applause and many amazements.
Louis Vuitton Malletier,
Louis Vuitton Malletier, commonly referred to as Louis Vuitton, or shortened to LV, is an international French fashion house specializing in trunks, leather goods, ready-to-wear, shoes, watches, jewelry, accessories, sunglasses, and books. Known the world over for its iconic Luxury Louis Vuittonand logo, Louis Vuitton Wallet is one of the most recognizable brands in the world. A long time symbol of prestige and wealth, the company commands some of the highest prices in the international fashion market for its products.
tiffany jewelry hot sale now
tiffany jewelry hot sale now with discount. Tiffanyhotsale provides the best Tiffany & Co jewelry, including Necklaces, Bracelets, Earrings, Pendants and so on .
Tiffany Sale Provide 100% sterling silver tiffanys jewelry,you can buy discount Tiffany & Co jewelry here.Tiffany And Co Jewelry is the best jewellry
Stunning silver jewellery from tiffany, Gucci, Links of London; quality designer silver jewelry at online prices. We have silver Bracelets, Solid Silver ...
Herve Leger womens fashions
Herve Leger womens fashions at ShopStyle. Shop popular stores to find Herve Leger womens fashions on sale - all in one place. Create and share looks based
Herve Leger
Cheap Herve Leger
Jewelry,Necklaces,Jewelry Necklaces,Links of London Necklaces,online sales a variety of world famous such as Links of London etc,with competitive price.
Links of London , the leading British contemporary jeweller was founded in 1990 by jewellery designer Annoushka Ducas and her husband John Ayton.cheap Links of London jewelry at online linksgif UK store, including Links of London Necklaces, Links of London Charms, Links of London Earrings. Links of London Silver Sweetie Bracelet Medium
Links of London Charm
Links of London
Shop for a nba jersey at
Shop for a nba jersey at SFGate Fan Shop:Why buy a nba jerseys from us? Our competitive prices on discount nba jerseyare a good reason.
Great post! keep it up. games
Great post! keep it up. games
Thanks for the share
Thanks for the share mate.
Residential Treatment
Beijing Tour Beijing
Beijing Tour Beijing
ToursChina TourChina ToursChina Travel AgencyTours In ChinaTour In ChinaTravel In ChinaChina Travel Great Wall Tours
watch faces watch wholesale
watch faces watch wholesale men's
watches women's watches ladies watches wholesale watches
Thank you for sharing this
Thank you for sharing this wonderful information!
replica watchesRolex Day Date
replica watchesRolex Day Date replicaRolex Daytona watchesreplica BreguetRolex Explorer watch for saleChristina Dior handbags
replica watchesreplica
replica watchesreplica U-Boatreplica OmegaRolex Explorer replicareplica IWCmarni handbags
Cartier watchesRolex
Cartier watchesRolex Masterpiece watchesreplica Rolex Sea-Dwellerreplica chopardreplica DeWittfriendshoes
replica handbags replica
replica handbags
replica designer handbags
gucci Handbags
fendi Handbags
Louis Vuitton handbags
LV Handbags
Prada Handbags
Bottega Veneta Handbags
Burberry Handbags
Jimmy Choo Handbags
Miu Miu Handbags
$75 Replica Rolex Watches
$75 Replica Rolex Watches sale, Our site provides Rolex replica, replica Rolex Watches, replica breitling, replica Cartier, replica Omega, Tag Heuer Watches. . .more than 100 famous rolex brands"
rolex air king
rolex datejust
rolex day date
rolex daytona
rolex gmt
rolex submariner
rolex yachtmaster
a lange sohne watches
alain silberstein watches
bell&ross watches
breguet watches
breitling watches
bvlgari watches
cartier watches
omega watches
patek philippe watches
ロレックスレプリカ
ロレックスコピー
Thanks for your useful info,
Thanks for your useful info, I think it's a good topic. So would you like the info about the air jordan shoes
cheap ugg boots
air jordans
ugg boots sale
louis vuitton has gained and
louis vuitton has gained and maintained its leading position over the past decades as a brand name of luxuries goods in the world fashion industry. louis vuitton Designer handbags symbolize both the social status and noble elegance louis vuitton
oto kurtarma estetik
oto kurtarma
estetik rehberi
kiralık oto
burun estetiği
ankara nakliyat
lahana kapsülü
ankara psikoloğ
lazer epilasyon
tüp bebek
ankara nakliyat
Welcome to here: ugg boots
Welcome to here:
ugg boots sale
Bailey Button Ugg Boots
classic tall ugg boots
classic short ugg boots
bailey button uggs
ugg boots bailey button
bailey ugg boots
ugg bailey boots
ugg bailey button
tall ugg boots
classic tall boots
short ugg boots
classic short boots
I think you will like.
There's actually a new
There's actually a new PHP-DB-Abstraction mailing list that is discussing DB-agnostic XML-based schema definitions as we speak. You may want to look into getting involved with them, too. free online games
discount christian louboutin
discount christian louboutin cheap christian louboutin on sale cheap christian louboutin on sale discount christian louboutin on sale discount christian louboutin on sale cheap christian louboutin sale cheap christian louboutin sale discount christian louboutin sale discount christian louboutin sale,Thanks of your infomation i have read it is very help full for me.
Fishing Tackle fishing
Fishing Tackle fishing reels、fishing
rods,fishing hooks、fishinglines、fishinglure from China at
http://www.pandafishing.com/.wholesale pandora jewelry,pandora
beads,pandora silver charms,pandora bracelet,pandora necklace,panodra rings from
china
fishing reels、fishing
fishing reels、fishing rods,fishing hooks、fishinglines、fishinglure from China at http://www.pandafishing.com/ .
pandora bracelet、pandara
pandora bracelet、pandara rings,pandora beads、necklace、sterling siliver jewelry、Crystal jewelry、charms from China at http://www.casijewelry.com/ .
There's actually a new
There's actually a new PHP-DB-Abstraction mailing list that is discussing DB-agnostic XML-based schema definitions as we speak. You may want to look into getting involved with them, too.
auto insurance quotes
The world is cute just
The world is cute just because of them. What will the fashion people in streets do?You See, louboutin Direct the Fashion Trend of Detroit. The present young people like to dress themselves up different, in any case just to look like different from others. In the List of Most Welcomed Embellishments, christian louboutin
are the NO.1 Therefore, we can often see many fantastic impersonations that may be fashionable, strange, or alternative in christian shoes.Habits that Stimulate Excoriation to Christian Boots, everyone has their own views Louboutin Boots, and then let's follow the street snaper to have a look at them in the Occident. Why Shop louboutin heels for WaitressesHey Colorists! Keep Away from christian high heels!
Special skirt with big lattices is rare. Have a Christian Boots and maybe you can obtain an unexpected effect Christian Pumps. The suit louboutin shoes upper body doesn't feel obtrusive, probably because of colour assortment is syncretic. So there can be such good effect.
The world is cute just
The world is cute just because of them. What will the fashion people in streets do?You See, Christian louboutin sale Direct the Fashion Trend of Detroit. The present young people like to dress themselves up different, in any case just to look like different from others. In the List of Most Welcomed Embellishments, christian louboutin uk are the NO.1 Therefore, we can often see many fantastic impersonations that may be fashionable, strange, or alternative in louboutin sale.Habits that Stimulate Excoriation to louboutin shoes uk, everyone has their own views yves Boots, and then let's follow the street snaper to have a look at them in the Occident. Why Shop ysl for WaitressesHey Colorists! Keep Away from ysl boots!
Special skirt with big lattices is rare. Have a Christian louboutins and maybe you can obtain an unexpected effect louboutin Pumps. The suit ysl uk upper body doesn't feel obtrusive, probably because of colour assortment is syncretic. So there can be such good effect.
Chipotle Mexican Grill links
Chipotle Mexican Grill links of london jewelry The fast food at these casual eateries feels slow links of london Rings thanks to organic ingredientsand an emphasis on freshness links of london sale Traffic is down at many locations links of london Necklaces but the company has been able to compensate links london by raising prices links of london Charms and bangle with faceted links of london jewellery claret For converse disguise effete stones links london jewellery Olive Garden links of london Sweetie Bracelets More food for less money hits the sweet spot these days links of london Sweetie Watches sdr
Then do you know the
Then do you know the christian louboutin,so come to see these christian louboutin shoes and the christian louboutin boots is also sex.
“Welcome! links of londonWhat
“Welcome! links of londonWhat can I do for you?” she always asks every customer who comes across. She says the same words every day and always be seen strangely. “Madam”links of london sale she hears a magnetic sound from a man. “Sorry! What can I do for you?” “I know a girl who is lovely and breezy. She does not like to be bind.” “Oh! His eyes full of love and happiness. links of london jewelleryThat girl must be very happy.” “Ah, what color doer your girlfriend like?” she asks him, “She does not my girlfriend” there is a glance of sorrow in his eye. “It is used for confession.” He nods his head. She thinks one thing suddenly.
This is one great article. I
This is one great article. I am going to bookmark it through Digg right now..!
Onwer: Services SEO
Site directory submit
Submission Article
Laptop Battery Laptop
Laptop Battery
Laptop Batteries
discount laptop battery
notebook battery
Are you want to buy
Are you want to buy uggs?There are many good ugg boots :
Post new comment