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

Supporting natural keys #166

Merged
merged 1 commit into from
Mar 28, 2018
Merged

Conversation

maiha
Copy link
Contributor

@maiha maiha commented Mar 27, 2018

This PR enhances Model.primary to accept auto option that works as a natural key if false.

Example (use RDB as KVS)

mysql> DESCRIBE kvss;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| k     | varchar(255) | NO   | PRI | NULL    |       |
| v     | varchar(255) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
class Kvs < Granite::ORM::Base
  adapter mysql
  primary k : String
  field   v : String
end

First, if you use a primary key other than Int as described above, it will give a compile error with following suggestion.

Failed to define Kvs#save: Primary key must be Int(32|64), or set `auto: false` for natural keys.

  primary k : String, auto: false

After adding auto: false that disables auto number feature, we can play with nartural keys.

class Kvs < Granite::ORM::Base
  adapter mysql
  primary k : String, auto: false
  field   v : String
end
## Create
port = Kvs.new(k: "mysql_port", v: "3306")
port.new_record? # => true
port.save        # => true
# INSERT INTO `kvs` (`v`, `k`) VALUES (?, ?): ["3306", "mysql_port"]
Kvs.count        # => 1

## Read
port = Kvs.find("mysql_port")
port.new_record? # => false
port.v           # => "3306"

## Update
port.v = "3307"
port.new_record? # => false
port.save        # => true
# UPDATE `kvs` SET `v`=? WHERE `k`=?: ["3307", "mysql_port"]

## Delete
port.destroy     # => true
Kvs.count        # => 0

Best regards,

@drujensen
Copy link
Member

@maiha Excellent work! 💯

@drujensen drujensen mentioned this pull request Mar 27, 2018
@Blacksmoke16
Copy link
Contributor

Blacksmoke16 commented Mar 27, 2018

port = Kvs.new(k: "mysql_port", v: "3306")
p port.inspect
"#<Kvs:0x5591aabc39c0 @new_record=true, @destroyed=false, @errors=[], @k=nil, @v=\"3306\", @updated_at=nil, @created_at=nil>"

@maiha
Shouldn't the k instance variable be set to "mysql_port" when inspecting the object?

@maiha
Copy link
Contributor Author

maiha commented Mar 27, 2018

Pushed -f to resolve conflicts.


it "sets the value when the primary is defined as `auto: false`" do
Kvs.new(k: "foo").k?.should eq("foo")
Kvs.new(k: "foo", v: "v").k?.should eq("foo")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Blacksmoke16
Yep, I just added new with mass-assignment yesterday into this PR!
maiha:master + new(*mass-assignment*)maiha:natural-key

If you are using maiha:master branch, could you try again with maiha:natural-key or this pr branch? This should work!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah perfect! Was behind a few commits i guess :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@drujensen drujensen merged commit 48b7da4 into amberframework:master Mar 28, 2018
@robacarp
Copy link
Member

@maiha thank you for this contribution

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.

4 participants