[Feature Request] Allow to specify the size of the indexes in the domain mapping
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
When working with indexes in MySQL (maybe in other databases too), there is an index size limit. If there is a string field that is bigger than the index size, Grails doesn't create the index even if it's defined in the mapping.
In MySQL it's possible to define a big varchar, then for the index to be created:
CREATE TABLE t1 (
col1 VARCHAR(10),
col2 VARCHAR(4096),
INDEX (col1, col2(10))
);
Without the size in the index, this error is thrown: Specified key was too long; max key length is 3072 bytes
CREATE TABLE t1 (
col1 VARCHAR(10),
col2 VARCHAR(4096),
INDEX (col1, col2)
);
Though something like that defined as a Grails Domain class would fail silently.
### Task List
- [ ] Steps to reproduce provided
- [ ] Stacktrace (if present) provided
- [ ] Example that reproduces the problem uploaded to Github
- [ ] Full description of the issue provided (see below)
### Steps to Reproduce
1. Use MySQL as the database.
2. Define a domain with a big string constraint:
```groovy
class DataIndex {
String archetypeId
String archetypePath
static constraints = {
archetypePath(maxSize: 2048)
}
static mapping = {
archetypeId index: 'aid,aidpath'
archetypePath index: 'aidpath'
}
}
```
3. Run the app so the database schema is generated.
4. You will notice the index that should contain archetypeId and archetypePath is not created in the database.
### Expected Behaviour
There is no problem with the behavior, since the current mapping DSL doesn't allow to specify the size of the index. What I would propose is something like a maxSize on the long field when defining the indexes, because that is the one that causes issues independently of the index or indexes that include the corresponding table column:
```groovy
class DataIndex {
String archetypeId
String archetypePath
static constraints = {
archetypePath(maxSize: 2048)
}
static mapping = {
archetypeId index: 'aid,aidpath'
archetypePath index: 'aidpath', maxSize: 255
}
}
```
### Actual Behaviour
An index size is not supported in the mapping DSL.
### Environment Information
- **Operating System**: Linux Minth 21.3
- **GORM Version:** the one that's on Grails 5.3.3
- **Grails Version (if using Grails):** Grails 5.3.3
- **JDK Version:** OpenJDK 11.0.20.1
### Example Application
- TODO: link to github repository with example that reproduces the issue
Contributor guide
Research direction
Start by reproducing the DataIndex domain mapping with MySQL and inspect the mapping DSL and schema-generation path used by Grails 5.3.3. Determine how an index size could be represented for archetypePath and verify that the generated database index includes the requested prefix size without silently failing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy, mysql
- Domain
- backend, databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100