|
| 1 | +TinMan SQL Naming Conventions |
| 2 | +========= |
| 3 | + |
| 4 | +Gives structure to databases with heart. |
| 5 | + |
| 6 | +This document outlines which naming conventions from the style guide can or cannot currently be enforced using SQLFluff, and provides configuration settings for enforceable rules. This ruleset is a extension and merger of three different rulesets for SQL tables: |
| 7 | + |
| 8 | +* [https://x.com/sehrope](https://x.com/sehrope) wrote [How I Write SQL, Part 1: Naming Conventions](https://launchbylunch.com/posts/2014/Feb/16/sql-naming-conventions/). When clarity is needed on how to use these, start here. |
| 9 | +* [DURC](https://github.com/ftrotter/durc_is_crud) naming conventions |
| 10 | +* [SQLFluff default rules](https://docs.sqlfluff.com/en/stable/configuration/default_configuration.html) |
| 11 | + |
| 12 | +We intend to write additional sqlfluff rules to support the rules that sqlfluff does not already support when possible. eventually. given infinite time and resources. |
| 13 | + |
| 14 | +⸻ |
| 15 | + |
| 16 | +Rules Not Currently Enforceable by SQLFluff |
| 17 | +------ |
| 18 | + |
| 19 | +These require either a custom rule, external linter, or manual review: |
| 20 | + |
| 21 | +* Use full English words, not abbreviations (so international_business_machines and not ibm) |
| 22 | +* Don’t use data types as names (so sales_count and not sales_count_int. Let the INT data type say it is an integer) |
| 23 | +* Use singular nouns for table names (person and not persons or people) |
| 24 | +* Primary key should be named simply id |
| 25 | +* Foreign keys should follow the pattern `<table_name_here>_id` |
| 26 | + * Two foreign keys in the table requires a prefix on the foreign key column, to differentiate the two links. Like `<helpful_name>_<table_name_here>_id` etc. |
| 27 | + * Foreign keys can and should be created dynamically based on this naming convention. |
| 28 | +* Name indexes explicitly with table and column ( fk_npiidentifier_npi_id and not fk_identifiers_need_keys) |
| 29 | +* Name constraints clearly (uq_interop_endpoint_url and not uq_intendu) |
| 30 | +* The varchar or text field listed first in the table is assumed to be the "auto-suggest search" field for the table. If this is not correct, then the table needs an explicit select_name field or postfixed field. |
| 31 | +* DURC Reserved names: |
| 32 | + * a column named select_name or that ends in \_select_name is reserved for the field that will power an auto-suggest lookup on the table. |
| 33 | + * column names with a postfix of _markdown are used to invoke a front-end markdown editor |
| 34 | + * column names with a postfix of _code will invoke a code-editor on the front end. |
| 35 | + |
| 36 | +⸻ |
| 37 | + |
| 38 | +Rules Enforceable by SQLFluff |
| 39 | +------ |
| 40 | + |
| 41 | +These can be configured using standard SQLFluff rules: |
| 42 | +* Avoid quoted identifiers → L014: Discourages use of quoted identifiers. |
| 43 | +* Use all lowercase for identifiers → L010: Enforces case on keywords and identifiers. |
| 44 | +* Use underscores to separate words (snake_case) → L040: Enforces naming convention patterns. |
| 45 | +* Avoid reserved words as identifiers → L036: Detects use of reserved keywords as identifiers. |
| 46 | + |
| 47 | +⸻ |
| 48 | + |
| 49 | +How to run |
| 50 | +---- |
| 51 | +I suggest for now that people use the [VS Code sqlfluff linter to help using this for now](https://marketplace.visualstudio.com/items?itemName=dorzey.vscode-sqlfluff). |
| 52 | +Further automations might happen with github actions later on. |
| 53 | + |
| 54 | +[.sqlfluff](.sqlfluff) Configuration |
| 55 | +---- |
| 56 | + |
| 57 | +This is a starting point and will change over time. |
| 58 | + |
| 59 | + |
| 60 | +```conf |
| 61 | +[sqlfluff] |
| 62 | +dialect = postgres |
| 63 | +
|
| 64 | +[sqlfluff:rules] |
| 65 | +exclude_rules = L009 # Optional: skip alias prefixing if undesired |
| 66 | +
|
| 67 | +[sqlfluff:rules:L010] |
| 68 | +capitalisation_policy = lower # Force lowercase for identifiers |
| 69 | +
|
| 70 | +[sqlfluff:rules:L014] |
| 71 | +extended_capitalisation_policy = lower # Reinforce lowercase for quoted identifiers |
| 72 | +ignore_words = '' # Do not allow any exceptions |
| 73 | +
|
| 74 | +[sqlfluff:rules:L036] |
| 75 | +# No config needed — enabled by default |
| 76 | +
|
| 77 | +
|
| 78 | +[sqlfluff:rules:capitalisation.keywords] |
| 79 | +# Keywords |
| 80 | +capitalisation_policy = upper |
| 81 | +
|
| 82 | +[sqlfluff:rules:L040] |
| 83 | +capitalisation_policy = upper |
| 84 | +naming_convention = snake_case |
| 85 | +``` |
0 commit comments