Databases: Set or update collation for single column in XMLDB?

Databases: Set or update collation for single column in XMLDB?

by Glenn Ansley -
Number of replies: 5

Hi,

Is it possible to set or update the collation of a specific 'text' column with the XMLDB format? Looked through the documentation and couldn't find anything. I am now looking through the code but if someone knows... that would be awesome.

 

Thanks.

Average of ratings: -
In reply to Glenn Ansley

Re: Databases: Set or update collation for single column in XMLDB?

by Eloy Lafuente (stronk7) -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Glenn,

I think the answer is "No". The rationale is that collations/charsets are handled really different by every RDBMS and, although the issue has raised various times in the past, I think our "agnostic" position is the correct, avoiding to enter in that minefield completely.

So I think that you'll have to perform any change in that area following the documentation for your RDBMS. Moodle will, simply apply the defaults always.

Hope it helps, ciao smile

In reply to Eloy Lafuente (stronk7)

Re: Databases: Set or update collation for single column in XMLDB?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hang on Eloy, the whole point of XMLDB is to hide the evil inconsistencies between what you need to do to define database tables in all the different DBs.

And, if you recall MDL-29332, you will remember that is is pretty much inevitable that will will have to tackle collations at some point.

The nub of the problem is that if you want locale-senstive sorting in MySQL, then you have to specify a case-insensitive collation. And, if you specify a _ci collation, then MySQL thinks that 'x' = 'X' which is a horrible problems for columns like idnumber, username, role.shortname, calculated question type variable names, ...

In the discussion we had (some time ago) I though we had agreed this approach:

  1. Define the database using a binary collation. This probably has the advantage of making joins quicker for varchar columns.
  2. Then, only specify locale-specific collations in ORDER BY clauses.
The advantage of this is that for a multi-lingual site like moodle.org, we can sort thing using the current language for each user, rather than having one collation hard-coded in the DB definition.
The disadvantage is that it will be a huge amount of work to covert Moodle to this new way of doing things.
And a lesser disadvantage is that for each lang pack, and for each supported DB type, we have to specify what collation to use for sorting. (presumably that has to go in langconfig.php).
Do you remember that discussion we had? Do you still agree that this is the right long-term plan? How long will it be before we can start working towards this long-term-plan?
Average of ratings: Useful (3)
In reply to Tim Hunt

Re: Databases: Set or update collation for single column in XMLDB?

by Eloy Lafuente (stronk7) -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Peer reviewers Picture of Plugin developers Picture of Testers

100% agree, what else can I say. cool

But that does not include to "set or update the collation of a specific 'text' column with the XMLDB format", that was the Q above. wink

Personally I'm using binary collations everywhere since ages ago (yes I know it breaks some ordering here and there). But any collation breaks ordering, for people using other languages, so no big difference for me (English/Spanish).

And yes, I think that the sql_orderby() (or whatever we call it) is something to implement and apply soon (2.3, yay!?) to courses/users lists. I'm sure there are not thousands of uses really requiring it.

Also, don't forget that there was some problem for PostgreSQL, so the collation only could be used for ordering starting with 9.x (if I'm not wrong).

Anyway, in summary, yes, there are plans to provide better (and dynamic) collation-cross-db compatible sorting. But, no, there are no plans to allow to specify/change any collation from the XMLDB definitions/stuff.

Thanks for reviving the thing Tim, ciao smile

In reply to Eloy Lafuente (stronk7)

Re: Databases: Set or update collation for single column in XMLDB?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Good. sql_order_by in 2.3 would be great.

To clarify your penultimate sentence, what we are proposing is that, on install, as part of the environment checks, Moodle will check that the whole database is set to a sane (case-sensitive) collation.

In reply to Eloy Lafuente (stronk7)

Re: Databases: Set or update collation for single column in XMLDB?

by Glenn Ansley -

Thanks Eloy. For what its worth, that tracker ticket Tim referenced is the reason I was looking for the information. I just placed a $DB->execute_sql() after the create_table call in the upgrade script. Not very pretty but it seems to have done the trick for us.

Thanks again.