Table descriptions are helpful for newbies. They print out while using PHPMYAdmin, and with the cool dba.module (see contrib). Would be nice to add a description for the key tables in our database.mysql script and similar scripts for other DBs

Comments

jonbob’s picture

+1

If present, I could harvest this information in api.module to provide nicer developer documentation, too.

Anonymous’s picture

Here is an example for mysql. Notice the 'COMMENT' at the end:

CREATE TABLE access (
  aid tinyint(10) NOT NULL auto_increment,
  mask varchar(255) NOT NULL default '',
  type varchar(255) NOT NULL default '',
  status tinyint(2) NOT NULL default '0',
  PRIMARY KEY  (aid),
  UNIQUE KEY mask (mask)
) TYPE=MyISAM COMMENT='just a test';
coreb’s picture

Version: x.y.z » 6.x-dev

Moving from x.y.z queue to 6.x-dev.

pasqualle’s picture

Status: Active » Fixed
Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

webchick’s picture

Title: Add table descriptions to the schema setup scripts » Show table descriptions in PHPMyAdmin and other tools
Version: 6.x-dev » 7.x-dev
Status: Closed (fixed) » Active

From the issue description, this isn't fixed, as far as I can tell, but I would certainly like it to be! :) Fixing title appropriately.

In 6.x we spend a lot of time documenting each field in the database schema, which is great if you have a copy of the source code handy. However, nowhere does Schema API translate these into actual COMMENT statements so that they are displayed in PHPMyAdmin and other tools. Right now the only place to see them in a browser is with Schema module, which most people don't know about.

But, it would be awesome if it did translate these to the DB-specific COMMENT equivalents where applicable so that other tools could see this information on their own.

lilou’s picture

mfb’s picture

Status: Active » Needs work
StatusFileSize
new2.71 KB

Here's a proof of concept. In mysql, table descriptions can only be 60 characters, but column descriptions can be up to 255 characters. For postgres there is a separate COMMENT command, http://www.postgresql.org/docs/8.3/static/sql-comment.html

Crell’s picture

I don't quite get the changes in DatabaseSchema::createTable(). What are you doing there?

mfb’s picture

update_sql() cannot handle placeholders (now needed for the comments) so I replaced it with db_query(). Hopefully this can be refactored, it's a hack :)

mfb’s picture

Not sure if postgres allows you to add comments in the schema definition for a column, or only with separate COMMENT command. If the latter, then creating or altering a column will need to return an array of multiple SQL statements and a placeholder, rather than just a string of SQL.

Crell’s picture

The $ret array is going to die as soon as I get around to overhauling the schema. The new query logger will replace it. At this point I'll say go ahead and ignore it, just make the queries do what you need to do.

mfb’s picture

Status: Needs work » Needs review
StatusFileSize
new3.69 KB

In this patch, table and column comments should be working for both mysql and pgsql. I couldn't get db_query() placeholders to work in pgsql: prepared statements and COMMENT statements don't seem to play nice? But this was a good thing as I realized I should just use PDO::quote() to prepare the comments and add them directly to the query strings.

Status: Needs review » Needs work

The last submitted patch failed testing.

Crell’s picture

hm, we probably need a db_quote() wrapper if we don't have one already...

mfb’s picture

StatusFileSize
new4.17 KB

Let's see if this passes tests. Previous patch had a flaw where applying table prefixes to comments would cause them to exceed MySQL's max comment length.

mfb’s picture

Status: Needs work » Needs review
mfb’s picture

I saw some weirdness with this patch on my own test environment and am guessing it's related to this bug: http://bugs.php.net/bug.php?id=41125 I'd like to know if anyone else is seeing segfaults and/or uncompleted tests, if so we might need a work-around.

Status: Needs review » Needs work

The last submitted patch failed testing.

mfb’s picture

Status: Needs work » Needs review
StatusFileSize
new4.44 KB

Chasing head and testing a workaround for http://bugs.php.net/bug.php?id=41125

mfb’s picture

One note about this patch. I am converting schema descriptions from HTML format to plain text, because db tools like Phpmyadmin do not render comments as HTML. Specifically, a string such as &lt;none&gt; needs to be decoded to <none>.

c960657’s picture

Instead of doing the "if (driver == 'mysql')" inside DatabaseSchema::prepareComment(), I think it is cleaner to do the MySQL-specific stuff in DatabaseSchema_mysql::prepareComment().

Instead of replacing apostrophe with back-tick I suggest using U+2019. “This is the preferred character to use for apostrophe,” says Wikipedia.

mfb’s picture

StatusFileSize
new4.66 KB

Thanks, I was thinking there might be a better unicode solution :)

c960657’s picture

Looks good. I have tested this with MySQL+phpMyAdmin and PostgreSQL+pgAdmin.

(I hope we can get rid of the HTML in the schema descriptions - but that is separate project)

mfb’s picture

I'd also like to go with plain-text schema descriptions. This only involves patching ~3 lines in aggregator.install and block.install.

mfb’s picture

dries’s picture

This is pretty cool. Should we write some tests for this?

Crell’s picture

I'm not sure if tests could be written in a DB-independent way. (I've not used table comments before on any DB.)

mfb’s picture

Yeah, we'd have to add some mysql- and pgsql-specific methods for getting the comments.

In MySQL we can easily get the table and column comments from the INFORMATION_SCHEMA views. in postgres, it's not in information_schema, we apparently have to use something like this instead: SELECT pg_description.description FROM pg_description, pg_class, pg_attribute WHERE pg_attribute.attrelid = pg_class.oid AND pg_description.objoid = pg_class.oid AND pg_description.objsubid = pg_attribute.attnum AND pg_class.relname = 'node' AND pg_attribute.attname = 'nid'

mfb’s picture

StatusFileSize
new9.48 KB

I added tests.

dries’s picture

Status: Needs review » Fixed

Thanks for the tests. Committed to CVS HEAD.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

dave reid’s picture

Status: Closed (fixed) » Fixed

So do we need to be adding descriptions in all of our update functions that use db_add_table, db_add/change_field, etc?

(Sorry, remarking as fixed so it will show up and someone can answer it)

mfb’s picture

Yes, these update functions should get the full schema specification including description.

dave reid’s picture

Could you please file a new issue to make sure all existing update functions in core get schema descriptions with them? There are a lot that do not. Post the link back here so I can followup. Thanks!

mfb’s picture

Status: Fixed » Closed (fixed)
Issue tags: -DX (Developer Experience)

Automatically closed -- issue fixed for 2 weeks with no activity.