{"id":13878636,"url":"https://github.com/zverok/the_schema_is","last_synced_at":"2025-04-04T13:08:17.346Z","repository":{"id":47742477,"uuid":"242509506","full_name":"zverok/the_schema_is","owner":"zverok","description":"ActiveRecord schema annotations done right","archived":false,"fork":false,"pushed_at":"2024-12-22T13:44:37.000Z","size":69,"stargazers_count":75,"open_issues_count":3,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T12:03:52.820Z","etag":null,"topics":["activerecord","annotations","rails","schema"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zverok.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-23T12:04:41.000Z","updated_at":"2025-03-11T20:32:38.000Z","dependencies_parsed_at":"2024-01-13T20:36:24.096Z","dependency_job_id":"4c3f9c4c-5fbe-4878-9b8f-e581ec369f19","html_url":"https://github.com/zverok/the_schema_is","commit_stats":{"total_commits":42,"total_committers":3,"mean_commits":14.0,"dds":0.04761904761904767,"last_synced_commit":"33af70257d849f02886d4b16ac5c8f20d05e11bf"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Fthe_schema_is","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Fthe_schema_is/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Fthe_schema_is/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Fthe_schema_is/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zverok","download_url":"https://codeload.github.com/zverok/the_schema_is/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247176865,"owners_count":20896545,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","annotations","rails","schema"],"created_at":"2024-08-06T08:01:55.386Z","updated_at":"2025-04-04T13:08:17.323Z","avatar_url":"https://github.com/zverok.png","language":"Ruby","funding_links":[],"categories":["Ruby","Gems"],"sub_categories":["Database Structure and Schema Management"],"readme":"# The schema is ...\n\n[![Gem Version](https://badge.fury.io/rb/the_schema_is.svg)](http://badge.fury.io/rb/the_schema_is)\n![Build Status](https://github.com/zverok/the_schema_is/workflows/CI/badge.svg?branch=master)\n\n`the_schema_is` is a model schema annotation DSL for ActiveSupport models, enforced by Rubocop. [Jump to detailed description →](#so-how-your-approach-is-different).\n\n### Why annotate?\n\nAn important part of class' public interface is **what attributes objects of this class have**. In ActiveRecord, attributes are inferred from DB columns and only can be seen in `db/schema.rb`, which is unfortunate.\n\nWe believe it _should_ be part _immediately available_ information of class definition. \"It is drawn automatically from DB\" is kinda clever, but it _does not_ helps to read the code. \"Auto-deduction from DB\" could be used to compare actual table content's to the definition in Ruby but **not** to skip the definition.\n\n\u003e Fun fact: most of other languages' ORM have chosen \"explictly list attributes in the model\" approach, for some reason! For example, Python's [Django](https://docs.djangoproject.com/en/3.0/topics/db/models/#quick-example), Elixir's [Ecto](https://hexdocs.pm/phoenix/ecto.html#the-schema), Go's [Beego](https://beego.me/docs/mvc/model/overview.md#quickstart) and [Gorm](https://gorm.io/docs/#Quick-Start), Rust's [Diesel](https://github.com/diesel-rs/diesel/blob/v1.3.0/examples/postgres/getting_started_step_1/src/models.rs), most of popular [NodeJS's options](https://www.codediesel.com/javascript/nodejs-mysql-orms/), and PHP's [Symphony](https://symfony.com/doc/current/doctrine.html#creating-an-entity-class) (but, to be honest, not [Laravel](https://laravel.com/docs/6.x/eloquent#eloquent-model-conventions)).\n\n### Well then, why not [annotate](https://github.com/ctran/annotate_models) gem?\n\nAnnotate gem provides a very powerful and configurable CLI/rake task which allows adding to your model (and factory/route/spec) files comment looking like...\n\n```ruby\n# == Schema Information\n#\n# Table name: users\n#\n#  id                             :integer          not null, primary key\n#  email                          :string           default(\"\"), not null\n#  encrypted_password             :string           default(\"\"), not null\n#  last_sign_in_at                :datetime\n#  last_sign_in_ip                :inet\n#  created_at                     :datetime         not null\n#  updated_at                     :datetime         not null\n# ....\n```\n\nIt kinda achieves the goal, but in our experience, it also brings some problems:\n\n* annotation **regeneration is disruptive**, just replacing the whole block with a new one, which produces a lot of \"false changes\" (e.g. one field with a bit longer name was added → spacing of all fields were changed);\n* if on different developer's machines **column order or defaults is different** in dev. DB, annotate also decides to rewrite all the annotations, sometimes adding tens files \"changed\" to PR;\n* regeneration makes it **hard to use schema annotation for commenting/explaining** some fields: because regeneration will lose them, and because comments-between-comments will be hard to distinguish;\n* the **syntax of annotations is kinda ad-hoc**, which makes it harder to add them by hand, so regeneration becomes the _only_ way to add them.\n\n### So, how your approach is different?..\n\n`the_schema_is` allows you to do this:\n\n```ruby\nclass User \u003c ApplicationRecord\n  the_schema_is \"users\" do |t|\n    t.string \"email\", default: \"\", null: false\n    t.string \"encrypted_password\", null: false\n    t.datetime \"last_sign_in_at\"\n    t.inet \"last_sign_in_ip\"\n    t.datetime \"created_at\", null: false\n    t.datetime \"updated_at\", null: false\n    # ...\n  end\nend\n```\n\nIdea is, it is _exactly_ the same DSL that `db/schema.rb` uses, so:\n\n* it can be just copied from there (or written by hands in usual migration syntax);\n* it is _code_, which can be supplemented with _comments_ explaining what some column does, or why the defaults are this way; it also can be structured with columns reordering and extra blank lines.\n\nSo, in reality, your annotation may look like this:\n\n```ruby\nclass User \u003c ApplicationRecord\n  the_schema_is \"users\" do |t|\n    t.string \"email\", default: \"\", null: false\n    # We use RSA encryption currently.\n    t.string \"encrypted_password\", null: false\n\n    t.inet \"last_sign_in_ip\" # FIXME: Legacy, we don't use it anymore because GDPR\n\n    t.datetime \"last_sign_in_at\"\n\n    t.datetime \"created_at\", null: false\n    t.datetime \"updated_at\", null: false\n    # ...\n  end\nend\n```\n\nNow, `the_schema_is` gem consists of this DSL and _custom [Rubocop](https://www.rubocop.org/) cops_ which check the correspondence of this DSL in model classes to your `db/schema.rb` (and can automatically fix discrepancies found).\n\nUsing existing Rubocop's infrastructure brings several great benefits:\n\n* you can include checking \"if all annotations are actual\" in your CI/pre-commit hooks easily;\n* you can preview problems found, and then fix them automatically (with `rubocop -a`) or manually however you see suitable;\n* the changes made with auto-correct is very local (just add/remove/change line related to relevant column), so your custom structuring, like separating groups of related columns with empty lines and comments, will be preserved;\n* rubocop is easy to run on some sub-folder or one file, or files corresponding to some pattern; or exclude permanently for some file or folder.\n\n### But what the block itself does?\n\nNothing.\n\n**Ugh... What?**\n\nThat's just how it is (at least for now) ¯\\\\\\_(ツ)\\_/¯\n\nThe block isn't even evaluated at all (so potentially can contain any code, and only Rubocop's cop will complain). In the future, it _can_ do some useful things (like, on app run in development environment compare scheme of the real DB with declarations in class), but for now, it is just noop declarative schema copy-paste.\n\n## Usage\n\n1. Add to your Gemfile `gem 'the_schema_is'` and run `bundle install`.\n2. Add to your `.rubocop.yml` this:\n  ```yaml\n  require:\n    - the_schema_is/cops\n  ```\n3. Run `rubocop` and see what it now says about your models.\n4. Now you can add schema definitions manually, or allow `rubocop --auto-correct` (or `-a`) to do its job! NB: you can always use `rubocop --auto-correct --only TheSchemaIs` to auto-correct ONLY this schema thing\n\nTo make reporting cleaner, all cops are split into:\n\n* `Presence`\n* `WrongTableName`\n* `MissingColumn`\n* `UnknownColumn`\n* `WrongColumnDefinition`\n\nIt is not advisable to selectively turn them off, but you may know better (for example, some may experiment with leaving in models just `t.\u003ctype\u003e '\u003cname\u003e'` without details about defaults and limit, and therefore turn off `WrongColumnDefinition`), all of it is pretty experimental!\n\n## Setting\n\n`the_schema_is` cops support some configuration, which should be done on the namespace level in your `.rubocop.yml`, for example:\n\n```yaml\nTheSchemaIs:\n  Schema: db/other-schema-file.rb\n```\n\nCurrently available settings are:\n\n* `TablePrefix` to help `the_schema_is` deduce table name from class name;\n* `Schema` to set path to schema (by default `db/schema.rb`);\n* `BaseClass` to help `the_schema_is` guess what is a model class (by default `ApplicationRecord` and `ActiveRecord::Base`);\n* `RemoveDefinitions`: list of definition keys to remove (for example, `[index, foreign_key, limit]`) when copying definitions into models; this might be desirable for leaner `the_schema_is` statements, displaying only field types/names.\n\nSo, if you have your custom-named base class, you should do:\n\n```yaml\nTheSchemaIs:\n  BaseClass: OurOwnBase\n```\n\nNote that Rubocop allows per-folder settings out of the box, which allows TheSchemaIs to support complicated configurations with multiple databases and engines.\n\nFor example, consider your models are split into `app/models/users/` and `app/models/products` which are stored in the different databases, then you probably have different schemas and base classes for them. So, to configure it properly, you might want to do in `app/models/users/.rubocop.yml`:\n\n```yaml\n# Don't forget this for all other cop settings to not be ignored\ninherit_from: ../../../.rubocop.yml\n\nTheSchemaIs:\n  BaseClass: Users::BaseRecord\n  Schema: db/users_schema.rb\n```\n\n## Some Q\u0026A\n\n* **Q: It doesn't check the actual DB?**\n  * A: No, it does not! At the current moment, our belief is that in a healthy Rails codebase `schema.rb` is always corresponding to DB state, so checking against it is enough. This approach makes the tooling much easier (with existing Rubocop's ecosystem of parsers/offenses/configurations).\n* **Q: What if I don't use Rubocop?**\n  * A: You may want to try, at least? Do you know that you may disable or configure most of its checks to your liking? And auto-correct any code to your preferences?.. Or automatically create \"TODO\" config-file (which disables all the cops currently raising offenses, and allows to review them and later setup one-by-one)?.. It is much more than \"linter making your code to complain about some rigid style guide\".\n* **Q: Cool, but I still don't want to.**\n  * A: ...OK, then you can disable all cops _except_ for `TheSchemaIs` namespace :)\n* **How do I annotate my fabrics, model specs, routes, controllers, ... (which `annotate` allows)?**\n  * A: You don't. The same way you don't copy-paste the whole definition of the class into spec file which tests this class: Definition is in one place, tests and other code using this definition is another. DRY!\n* **Rubocop is unhappy with the code `TheSchemaIs` generated**.\n  * A: There are two known things in generated `the_schema_is` blocks that Rubocop may complain about:\n    * Usage of double quotes for strings, if your config insists on single quotes: that's because we just copy code objects from `schema.rb`. Rubocop's auto-correct will fix it :) (Even in one run: \"fixing TheSchemaIs, then fixing quotes\");\n    * Too long blocks (if you have tables with dozens of columns, God forbid... as we do). It can be fixed by adding this to `.rubocop.yml`:\n  ```yaml\n  Metrics/BlockLength:\n    ExcludedMethods:\n      - the_schema_is\n  ```\n\n## Author and License\n\n[Victor Shepelev aka \"zverok\"](https://zverok.github.io), MIT.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzverok%2Fthe_schema_is","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzverok%2Fthe_schema_is","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzverok%2Fthe_schema_is/lists"}