
geting database metadata for mssql and mysql
For the database I am updating it must run on either mssql or mysql. That makes things a bit more difficult since my upgrade scripts must work for either db.
Today I am doing more research on how to get the metadata about the existing database. Basically get the tables, columns, and indexes for the database, compare to what I need for the upgrade and do the create or alter table commands needed.
For MS SQL you run different sql commands to get that metadata.
something like:
2FROM information_schema.tables
You can do that for tables and columns to get what you need. But you can't do that for indexes. Why? No idea. I found this little gem, however.
2isclustered = indexproperty(i.id, i.name, 'IsClustered'),
3"column" = col_name(i.id, ik.colid), ik.keyno
4FROM sysindexes i
5JOIN sysindexkeys ik ON i.id = ik.id
6AND i.indid = ik.indid
7WHERE i.indid BETWEEN 1 AND 254
8AND indexproperty(i.id, name, 'IsHypothetical') = 0
9AND indexproperty(i.id, name, 'IsStatistics') = 0
10AND indexproperty(i.id, name, 'IsAutoStatistics') = 0
11AND objectproperty(i.id, 'IsMsShipped') = 0
12ORDER BY "table", "isclustered" DESC, i.name, ik.keyno
I just tested it out and even on my SQL 2000 machine it works exactly as I need.
For MySQL you need to run things a bit different. Well, no you can use information_schema but from what I read only with ver 5 or above. Since I have users on 3 and 4, I need to do something more MySQL specific.
2from ecommerceTBL
3
4show index
5from ecommerceTBL
So I figure I write up an XML document describing exactly how the DB should be in final state, then loop through it to make sure all tables exist, all columns exist and all indexes exist. If they don't run the appropriate create or alter command. Then in the future when I need to upgrade a site I can just run this script to build the database to the latest and greatest.

NAVIGATION
HomeAbout Me
RSS
Search
Subscribe
Recent Entries
Google TV ReviewPlaybook - Developers It's Time To Get Started
cf.Objective 2011 - Speak Your Mind
Timesheets, Project Management, and Invoicing - FreshBooks Review
A New Phase of My Life
Recent Comments
FireFox 3.6 KTML Editor Fix
Herman said: Hello,
Sinds Firefox 10 is out the filebrowser in KTML and CSS Styles are not avaible... Any sugges...
[More]
OTA OK?
AnoraDD said: I get 18 of these exact sms's everyday! How do I get it to STOP?!?
[More]
Coldfusion Hosting with Network Solutions
LIzm said: Ugh. I have a client who insists on hosting with them and two weeks after first contact, a very simp...
[More]
IE nested list item whitespace solution: vertical-align:bottom
Lauren said: Thought I'd add to the thank yous...
Thank you!
[More]
OTA OK?
Rita said: Thank you, this was very helpful.
[More]
Calendar
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 |
Archives By Subject
blogs (31) [RSS]books (4) [RSS]
Consulting (2) [RSS]
Crazy (39) [RSS]
DIY (8) [RSS]
Flex (3) [RSS]
games (10) [RSS]
GRRR (13) [RSS]
Ideas (11) [RSS]
Local (15) [RSS]
LOLpics (2) [RSS]
money (9) [RSS]
music (3) [RSS]
Personal (28) [RSS]
Photos (8) [RSS]
Politics (8) [RSS]
Projects (22) [RSS]
Review (20) [RSS]
RPM (9) [RSS]
Spam (16) [RSS]
Technology (69) [RSS]
Testing (3) [RSS]
TV (15) [RSS]
video (32) [RSS]
Web Dev (230) [RSS]
World of Warcraft (16) [RSS]

Just a thought . . .
On the flip side my code now takes an xml doc (which it also makes) and creates any tbls, columns and indexes that don't already exist. Then runs maintenance code on them that may be needed for an upgrade.
One other option - did you ever look at using JDBC metadata? The JDBC API has some useful features. If you've never looked at it before this article in CFDJ should help:
http://coldfusion.sys-con.com/read/45569.htm
The solution I came up with works for now, but I may have to revisit it again with dataMGR or similar tools. Adding new databases to support will be a pain. Then again that could be tricky even with datamgr since some columns need to change based on the DB. datetime for mssql and datetime for mysql are not the similar enough for me for example.
I just happened onto this article. I like your approach. Certainly your thinking is similar to what mine was when I started working on that feature of DataMgr.
What problem are you having with datetime between MSSQL and MySQL?
In DataMgr, I define all of the data types using the data types from cfqueryparam and then map those to database data types (allows me to use cfqueryparam for all the CRUD work as well).
I like the idea of tracking indexes as well. I can certainly see where that would be helpful.
In addition I have issue when a table already exists and my script detects a field not existing. Idealy the field would be Null, but not have a default value. However the db's dont' like to have an alter table script to make a column that allows NNull without also a default value. Or was it Not Null?
Honestly I am away from my main computer on vacation and my memory is a bit fuzzy. :-)
The best way to do this is to alter my master DB to clean up some inconsistancies so that the issue doesn't come up.
Oh, and I have reports that with mysql 5 my scripts have issues with default values and nulls that they don't have with mysql 4, though I haven't debugged it yet.
The index detection and creation is pretty easy and works well. However it doesn't do anything more than a very simple index.