Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QUAL: Add sqlfluff (SQL code and style check) #29097

Merged
merged 4 commits into from
Apr 15, 2024

Conversation

mdeweerd
Copy link
Contributor

QUAL: Add sqlfluff (SQL code and style check)

This adds a validity and style check on the .sql files. The same tool can be used to fix style (which can be set up as a pre-commit hook).

Curerntly there is an issue in sqlfluff as it does not accept empty "--" comments.

It also notifies that 'rowid' is a keyword. It may be a biter overzealous there. The message can be avoided by quoting the rowid field in backticks ("rowid").

@mdeweerd mdeweerd changed the title QUAL: Add sqlfluff (SQL code and style check) (CIP) QUAL: Add sqlfluff (SQL code and style check) (WIP) Mar 28, 2024
@mdeweerd mdeweerd force-pushed the qual/sqlfluff branch 6 times, most recently from c454a40 to afd728d Compare March 28, 2024 11:13
@mdeweerd
Copy link
Contributor Author

mdeweerd commented Apr 10, 2024

@eldy

adding primary key on multiple columns:

sqlfluff does not accept (parse) a line like this:

ALTER TABLE example ADD PRIMARY KEY pk_example (fk_table1, fk_table2);

but it accepts

ALTER TABLE table0 ADD CONSTRAINT pk_example PRIMARY KEY (fk_table1, fk_table2);

Which matches an example in:
https://www.w3schools.com/sql/sql_primarykey.ASP#midcontentadcontainer

Such a case can be found in htdocs/install/mysql/migration/3.1.0-3.2.0.sql on line 160:

ALTER TABLE llx_categorie_fournisseur ADD PRIMARY KEY pk_categorie_fournisseur (fk_categorie, fk_societe);

I did not examine mysql's documentation on this.

Something that I should try to get parsed by sqlfluff or something that should be updated in dolibarr ?

Use of rowid

rowid is considered a keyword and it is also colored in my editor:

image

A solution is to quote the field `rowid`

Ignore the sqlfluf notice or update the sql files to quote the rowid field name.

EDIT:
According to the BNF on this page: https://mariadb.com/kb/en/alter-table/ , PRIMARY KEY can be followed by the index type, not an index name. And index_type is USING {BTREE | HASH | RTREE} so the sql statement does not match the BNF definition (which means that sqlfluff rightfully fails to parse the statement).

@eldy
Copy link
Member

eldy commented Apr 12, 2024

May be we can exclude files into dev/initdemo/ to avoid errors
"Length of file 'dev/initdemo/mysqldump_dolibarr_19.0.0.sql' is 5198834 bytes which is over the limit...

We don't mind scanning such files mysqldump as they are no code written by user but generated by mysql dump.

@eldy
Copy link
Member

eldy commented Apr 12, 2024

According to the BNF on this page: https://mariadb.com/kb/en/alter-table/ , PRIMARY KEY can be followed by the index type, not an index name. And index_type is USING {BTREE | HASH | RTREE} so the sql statement does not match the BNF definition (which means that sqlfluff rightfully fails to parse the statement).

Yes, the SQL has a wrong syntax. Howerver, it works correctly. As such sql files were provided on old version (and were working correctly), it is not a good idea to modify files tagged on old past version.
Can we exclude files into /install/mysql/migration ?

@mdeweerd
Copy link
Contributor Author

May be we can exclude files into dev/initdemo/ to avoid errors
These are warnings, but I can add them to the exclude list to avoid the notifications.

Yes, the SQL has a wrong syntax. Howerver, it works correctly. As such sql files were provided on old version (and were working correctly), it is not a good to modify files tagged on old past version.

They are still executed on more recent versions of mysql in theory they should be updated - I think that the syntax is also wrong for the old sql versions.

However to avoid the work of modifying them, they can be excluded.

@mdeweerd mdeweerd force-pushed the qual/sqlfluff branch 2 times, most recently from 2f1b16b to a086856 Compare April 12, 2024 21:58
@mdeweerd
Copy link
Contributor Author

@eldy
I ignored the sql files that were either too bit or resulted in notifications.

I propose to:

  • Run this only on changed SQL files in PR's;
  • Run it on all SQL files in develop and mainstream versions.

@mdeweerd mdeweerd force-pushed the qual/sqlfluff branch 2 times, most recently from 65e289c to cdc3beb Compare April 14, 2024 20:29
# QUAL: Add sqlfluff (SQL code and style check)

This adds a validity and style check on the .sql files.
The same tool can be used to fix style (which can be set up as a
pre-commit hook).
@mdeweerd mdeweerd marked this pull request as ready for review April 14, 2024 20:58
@mdeweerd
Copy link
Contributor Author

The CI will run sqlfluff on all enabled files in the main branches, only on changed files in pull requets.

@mdeweerd mdeweerd changed the title QUAL: Add sqlfluff (SQL code and style check) (WIP) QUAL: Add sqlfluff (SQL code and style check) Apr 14, 2024
@eldy eldy merged commit fad3e2d into Dolibarr:develop Apr 15, 2024
7 checks passed
@mdeweerd mdeweerd deleted the qual/sqlfluff branch January 8, 2025 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants