{"id":13484345,"url":"https://github.com/DAddYE/mini_record","last_synced_at":"2025-03-27T16:30:43.116Z","repository":{"id":56883862,"uuid":"2325305","full_name":"DAddYE/mini_record","owner":"DAddYE","description":"ActiveRecord meets DataMapper, with MiniRecord you are be able to write schema inside your models.","archived":false,"fork":false,"pushed_at":"2022-08-09T03:07:42.000Z","size":156,"stargazers_count":265,"open_issues_count":17,"forks_count":34,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-20T23:01:37.122Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://github.com/DAddYE/mini_record","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DAddYE.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-09-04T22:49:30.000Z","updated_at":"2024-04-01T17:05:38.000Z","dependencies_parsed_at":"2022-08-20T13:10:47.685Z","dependency_job_id":null,"html_url":"https://github.com/DAddYE/mini_record","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DAddYE%2Fmini_record","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DAddYE%2Fmini_record/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DAddYE%2Fmini_record/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DAddYE%2Fmini_record/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DAddYE","download_url":"https://codeload.github.com/DAddYE/mini_record/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245882224,"owners_count":20687854,"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":[],"created_at":"2024-07-31T17:01:22.845Z","updated_at":"2025-03-27T16:30:41.512Z","avatar_url":"https://github.com/DAddYE.png","language":"Ruby","readme":"[![Build Status](https://secure.travis-ci.org/DAddYE/mini_record.png)](http://travis-ci.org/DAddYE/mini_record)\n\n\nMiniRecord is a **micro** extension for the `ActiveRecord` gem.\n\nMiniRecord will allow you to create/edit/manage columns, directly in your **model**.\n\n\n## Features\n\n* Define columns/properties inside your model\n* Perform migrations automatically\n* Auto upgrade your schema\n* Add, Remove, Change **columns**\n* Add, Remove, Change **indexes**\n\n## Instructions\n\nWhat you need is to move/remove your `db/schema.rb`.\nThis avoid conflicts.\n\nAdd to your `Gemfile`:\n\n```sh\ngem 'mini_record'\n```\n\nTo optionally block any destructive actions on the database, create a file `config/initializers/mini_record.rb` and add:\n\n```ruby\nMiniRecord.configure do |config|\n  config.destructive = false\nend\n```\n\nThat's all!\n\n## Examples\n\nRemember that inside properties you can use all migrations methods,\nsee [documentation](http://api.rubyonrails.org/classes/ActiveRecord/Migration.html)\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  field :title_en, :title_jp\n  field :description_en, :description_jp, as: :text\n  field :permalink, index: true, limit: 50\n  field :comments_count, as: :integer\n  field :category, as: :references, index: true\nend\nPost.auto_upgrade!\n```\n\nInstead of `field` you can pick an alias: `key, field, property, col`\n\nIf the option `:as` is omitted, minirecord will assume it's a `:string`.\n\nRemember that as for `ActiveRecord` you can choose different types:\n\n```ruby\n:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time,\n:date, :binary, :boolean, :references, :belongs_to, :timestamp\n```\n\nYou can also provide other options like:\n\n```ruby\n:limit, :default, :null, :precision, :scale\n\n# example\nclass Foo \u003c ActiveRecord::Base\n  field :title, default: \"MyTitle\" # as: :string is not necessary since is a default\n  field :price, as: :decimal, scale: 8, precision: 2\nend\n```\n\nSee [ActiveRecord::TableDefinition](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html)\nfor more details.\n\n### Perform upgrades\n\nFinally, when you execute `MyModel.auto_upgrade!`, missing columns, indexes and tables will be created on the fly. \n\nIndexes and columns present in the db but **not** in your model schema/definition will be **deleted** also from your db.\n\n### Single Table Inheritance\n\nMiniRecord as ActiveRecord support STI:\n\n```ruby\n  class Pet \u003c ActiveRecord::Base; end\n  class Dog \u003c Pet; end\n  class Cat \u003c Pet; end\n  ActiveRecord::Base.auto_upgrade!\n```\n\nWhen you perform `ActiveRecord::Base.auto_upgrade!`, just **1** table will be created with the `type` column (indexed as well).\n\n### ActiveRecord Relations\n\nMiniRecord has built-in support of `belongs_to`, _polymorphic associations_ as well with _habtm_ relations. \n\nYou don't need to do anything in particular, is not even necessary define the field for them since they will be handled automatically.\n\n#### belongs_to\n```ruby\nclass Address \u003c ActiveRecord::Base\n  belongs_to :person\nend\n```\nWill result in a indexed `person_id` column. You can use a different one using the `foreign_key` option:\n\n```ruby\nbelongs_to :person, foreign_key: :person_pk\n```\n\n#### belongs_to with foreign key in database\n\n```ruby\nclass Address \u003c ActiveRecord::Base\n  belongs_to :person\n  index :person_id, foreign: true\nend\n```\n\nThis is the same example, but foreign key will be added to the database with help of\n[foreigner](https://github.com/matthuhiggins/foreigner) gem.\n\nIn this case you have more control (if needed).\n\nTo remove the key please use `:foreign =\u003e false`\nIf you simple remove the index, the foreign key will not be removed.\n\n#### belongs_to (polymorphic)\n\n```ruby\nclass Address \u003c ActiveRecord::Base\n  belongs_to :addressable, polymorphic: true\nend\n```\n\nWill create an `addressable_id` and an `addressable_type` column with composite indexes:\n\n```ruby\nadd_index(:addresses), [:addressable_id, :addressable_type]\n```\n\n#### habtm\n```ruby\nclass Address \u003c ActiveRecord::Base\n  has_and_belongs_to_many :people\nend\n```\n\nWill generate a \"addresses_people\" (aka: join table) with indexes on the id columns\n\n### Adding a new column\n\nSuper easy, open your model and just add it:\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  field :title\n  field :body, as: :text # \u003c\u003c- this\n  field :permalink, index: true\n  field :comments_count, as: :integer\n  field :category, as: :references, index: true\nend\nPost.auto_upgrade!\n```\n\nSo now when you invoke `MyModel.auto_upgrade!` a diff between the old schema an the new one will detect changes and create the new column.\n\n### Removing a column\n\nIt's exactly the same as in the previous example.\n\n### Rename columns\n\nSimply adding a `rename_field` declaration and mini_record will do a `connection.rename_column` in the next `auto_upgrade!` but **only** if the db has the old column and not the new column. \n\nThis means that you still need to have a `field` declaration for the new column name so subsequent `MyModel.auto_upgrade!` will not remove the column. \n\nYou are free to leave the `rename_field` declaration in place or you can remove it once the new column exists in the db.\n\nMoving from:\n```ruby\nclass Vehicle \u003c ActiveRecord::Base\n  field :color\nend\n```\n\nTo:\n```ruby\nclass Vehicle \u003c ActiveRecord::Base\n  rename_field :color, new_name: :body_color\n  field :body_color\nend\n```\n\nThen perhaps later:\n```ruby\nclass Vehicle \u003c ActiveRecord::Base\n  rename_field :color, new_name: :body_color\n  rename_field :body_color, new_name: :chassis_color\n  field :chassis_color\nend\n```\n\n### Change the type of columns\n\nWhere when you rename a column the task should be _explicit_ changing the type is _implicit_.\n\nThis means that if you have\n\n```ruby\nfield :total, as: :integer\n```\n\nand later on you'll figure out that you wanted a `float`\n\n```ruby\nfield :total, as: :float\n```\n\nWill automatically change the type the the first time you'll invoke `auto_upgrade`.\n\n\n### Add/Remove indexes\n\nIn the same way we manage columns MiniRecord will detect new indexes and indexes that needs to be removed.\n\nSo when you perform `MyModel.auto_upgrade!` a SQL command like:\n\n```SQL\nPRAGMA index_info('index_people_on_name')\nCREATE INDEX \"index_people_on_surname\" ON \"people\" (\"surname\")\n```\n\nA quick hint, sometimes index gets too verbose/long:\n\n```ruby\nclass Fox \u003c ActiveRecord::Base\n  field :foo, index: true\n  field :foo, index: :custom_name\n  field :foo, index: [:foo, :bar]\n  field :foo, index: { column: [:branch_id, :party_id], unique: true, name: 'by_branch_party' }\nend\n```\n\nHere is where `add_index` comes handy, so you can rewrite the above in:\n\n```ruby\nclass Fox \u003c ActiveRecord::Base\n  field :foo\n  add_index :foo\n  add_index :custom_name\n  add_index [:foo, :bar]\n  add_index [:branch_id, :party_id], unique: true, name: 'by_branch_party'\nend\n```\n\n### Suppress default indexes for associations\n\nIf you do not need the default index for a `belongs_to` or `has_and_belongs_to_many` relationship, such as if you are using a composite index instead, you can suppress it from being created (or remove it) using `suppress_index` on the association:\n\n```ruby\nclass PhoneNumber \u003c ActiveRecord::Base\n  field :position\n  belongs_to :person\n  suppress_index :person\n  add_index [:person_id, :position]\nend\n```\n\n### Passing options to Create Table\n\nIf you need to pass particular options to your `CREATE TABLE` statement, you can do so with `create_table` in the Model:\n\n```ruby\nclass Fox \u003c ActiveRecord::Base\n  create_table :options =\u003e 'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'\n  field :foo\nend\n```\n\n## Contributors\n\nA special thanks to all who have contributed in this project:\n\n* Dmitriy Partsyrniy\n* Steven Garcia\n* Carlo Bertini\n* Nate Wiger\n* Dan Watson\n* Guy Boertje\n* virtax\n* Nagy Bence\n* Takeshi Yabe\n* blahutka\n* 4r2r\n\n## Author\n\nDAddYE, you can follow me on twitter [@daddye](http://twitter.com/daddye) or take a look at my site [daddye.it](http://www.daddye.it)\n\n## Copyright\n\nCopyright (C) 2011-2014 Davide D'Agostino - [@daddye](http://twitter.com/daddye)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and\nassociated documentation files (the “Software”), to deal in the Software without restriction, including without\nlimitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,\nand to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM,\nDAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","funding_links":[],"categories":["ORM/ODM Extensions","Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDAddYE%2Fmini_record","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDAddYE%2Fmini_record","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDAddYE%2Fmini_record/lists"}