{"action":"opened","issue":{"url":"https://api.github.com/repos/luckyframework/migrator/issues/12","repository_url":"https://api.github.com/repos/luckyframework/migrator","labels_url":"https://api.github.com/repos/luckyframework/migrator/issues/12/labels{/name}","comments_url":"https://api.github.com/repos/luckyframework/migrator/issues/12/comments","events_url":"https://api.github.com/repos/luckyframework/migrator/issues/12/events","html_url":"https://github.com/luckyframework/migrator/issues/12","id":270755186,"number":12,"title":"Enforce a default or existing value when adding a non-nullable field","user":{"login":"edwardloveall","id":49549,"avatar_url":"https://avatars1.githubusercontent.com/u/49549?v=4","gravatar_id":"","url":"https://api.github.com/users/edwardloveall","html_url":"https://github.com/edwardloveall","followers_url":"https://api.github.com/users/edwardloveall/followers","following_url":"https://api.github.com/users/edwardloveall/following{/other_user}","gists_url":"https://api.github.com/users/edwardloveall/gists{/gist_id}","starred_url":"https://api.github.com/users/edwardloveall/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edwardloveall/subscriptions","organizations_url":"https://api.github.com/users/edwardloveall/orgs","repos_url":"https://api.github.com/users/edwardloveall/repos","events_url":"https://api.github.com/users/edwardloveall/events{/privacy}","received_events_url":"https://api.github.com/users/edwardloveall/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-11-02T18:15:05Z","updated_at":"2017-11-02T18:15:05Z","closed_at":null,"author_association":"NONE","body":"The idea here is that you have a table with existing data and you want to add a non-nullable column to it. Adding that column without a value to fill it in, or a default value will cause postgres (or whatever database) to throw an error.\r\n\r\nThe solution would be to require the `add` method inside of an `alter` table statement to have either a default value, or fill value:\r\n\r\n```crystal\r\nalter :posts do\r\n  add state : String, default: \"draft\"\r\nend\r\n```\r\n\r\nSince there are already posts in the database, requiring them all to have a `state` would cause postgres to error. This would set a default value like this:\r\n\r\n```sql\r\nCREATE TABLE posts (\r\n    state varchar(20) DEFAULT 'draft'\r\n);\r\n```\r\n\r\nBut sometimes, you don't necessarily want a default, but you do need to supply an existing value:\r\n\r\n```crystal\r\nalter :tasks do\r\n  add completed_at : Time, fill_existing_with: Time.new(2017, 11, 2)\r\nend\r\n```\r\n\r\n`fill_existing_with` would do the equivalent of:\r\n\r\n```sql\r\nUPDATE tasks SET completed_at = '2017-11-02';\r\n```\r\n\r\nIf either of these keys weren't specified, a compile time error like this would be thrown:\r\n\r\n> Adding a required field to an existing table requires you to set a default or use fill_existing_with. Example: add positive : Bool, fill_existing_with: false"}}
{"actor":{"display_login":"edwardloveall"}}