Permalink
Browse files

create short unique index names

  • Loading branch information...
1 parent 821dfcf commit 10e2d6cf9edf0f65df8e744d177a55a755405cd0 Chris Eberly committed Jun 28, 2011
Showing with 24 additions and 16 deletions.
  1. +14 −14 features/schema.feature
  2. +9 −1 lib/orel/relation/namer.rb
  3. +1 −1 spec/relation/namer_spec.rb
View
28 features/schema.feature
@@ -17,7 +17,7 @@ Feature: Create MySQL tables from relational definitions
"""
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
- UNIQUE KEY `user_id` (`id`)
+ UNIQUE KEY `i_b80bb7740288fda1f201890375a60c8f` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""
@@ -39,7 +39,7 @@ Feature: Create MySQL tables from relational definitions
CREATE TABLE `users` (
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
- UNIQUE KEY `user_first_name_last_name` (`first_name`,`last_name`)
+ UNIQUE KEY `fn_ln_453236cc5833e48a53bb6efb24da3d77` (`first_name`,`last_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""
@@ -75,7 +75,7 @@ Feature: Create MySQL tables from relational definitions
`bio` text NOT NULL,
`good` tinyint(1) NOT NULL,
`time` bigint(20) NOT NULL,
- UNIQUE KEY `user_id` (`id`)
+ UNIQUE KEY `i_b80bb7740288fda1f201890375a60c8f` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""
@@ -104,7 +104,7 @@ Feature: Create MySQL tables from relational definitions
CREATE TABLE `user_deleted` (
`at` datetime NOT NULL,
`user_id` int(11) NOT NULL,
- UNIQUE KEY `user_deleted_user_id` (`user_id`),
+ UNIQUE KEY `ui_e8701ad48ba05a91604e480dd60899a3` (`user_id`),
CONSTRAINT `user_deleted_users_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@@ -118,7 +118,7 @@ Feature: Create MySQL tables from relational definitions
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
- UNIQUE KEY `user_id` (`id`)
+ UNIQUE KEY `i_b80bb7740288fda1f201890375a60c8f` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""
@@ -148,7 +148,7 @@ Feature: Create MySQL tables from relational definitions
`at` datetime NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
- UNIQUE KEY `user_deleted_first_name_last_name` (`first_name`,`last_name`),
+ UNIQUE KEY `fn_ln_453236cc5833e48a53bb6efb24da3d77` (`first_name`,`last_name`),
CONSTRAINT `user_deleted_users_fk` FOREIGN KEY (`first_name`, `last_name`) REFERENCES `users` (`first_name`, `last_name`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@@ -163,7 +163,7 @@ Feature: Create MySQL tables from relational definitions
CREATE TABLE `users` (
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
- UNIQUE KEY `user_first_name_last_name` (`first_name`,`last_name`)
+ UNIQUE KEY `fn_ln_453236cc5833e48a53bb6efb24da3d77` (`first_name`,`last_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""
@@ -195,15 +195,15 @@ Feature: Create MySQL tables from relational definitions
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`user_id` int(11) NOT NULL,
- UNIQUE KEY `thing_id` (`id`),
+ UNIQUE KEY `i_b80bb7740288fda1f201890375a60c8f` (`id`),
KEY `things_users_fk` (`user_id`),
CONSTRAINT `things_users_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
- UNIQUE KEY `user_id` (`id`)
+ UNIQUE KEY `i_b80bb7740288fda1f201890375a60c8f` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""
@@ -234,14 +234,14 @@ Feature: Create MySQL tables from relational definitions
`name` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
- UNIQUE KEY `thing_first_name_last_name_name` (`first_name`,`last_name`,`name`),
+ UNIQUE KEY `fn_ln_n_b7f37fdab28b11c9e42cccaee91cd8a3` (`first_name`,`last_name`,`name`),
CONSTRAINT `things_users_fk` FOREIGN KEY (`first_name`, `last_name`) REFERENCES `users` (`first_name`, `last_name`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
- UNIQUE KEY `user_first_name_last_name` (`first_name`,`last_name`)
+ UNIQUE KEY `fn_ln_453236cc5833e48a53bb6efb24da3d77` (`first_name`,`last_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""
@@ -280,14 +280,14 @@ Feature: Create MySQL tables from relational definitions
CREATE TABLE `parts` (
`pno` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
- UNIQUE KEY `part_pno` (`pno`)
+ UNIQUE KEY `p_a640dd405e21ee73d9ad0c1153971c0f` (`pno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `shipments` (
`qty` int(11) NOT NULL,
`sno` varchar(255) NOT NULL,
`pno` varchar(255) NOT NULL,
- UNIQUE KEY `shipment_sno_pno` (`sno`,`pno`),
+ UNIQUE KEY `s_p_c2051a7c46108e2d1104c78f78c9862e` (`sno`,`pno`),
KEY `shipments_parts_fk` (`pno`),
CONSTRAINT `shipments_parts_fk` FOREIGN KEY (`pno`) REFERENCES `parts` (`pno`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `shipments_suppliers_fk` FOREIGN KEY (`sno`) REFERENCES `suppliers` (`sno`) ON DELETE NO ACTION ON UPDATE NO ACTION
@@ -296,7 +296,7 @@ Feature: Create MySQL tables from relational definitions
CREATE TABLE `suppliers` (
`sno` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
- UNIQUE KEY `supplier_sno` (`sno`)
+ UNIQUE KEY `s_96466301bed4aeef20378fe7bb5277e6` (`sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""
View
10 lib/orel/relation/namer.rb
@@ -1,3 +1,5 @@
+require 'digest/md5'
+
module Orel
module Relation
class Namer
@@ -39,7 +41,13 @@ def foreign_key_name(attribute_name)
# Used to generate sql
def unique_key_name(attribute_names)
- [@name, attribute_names].flatten.join('_').to_sym
+ short_names = attribute_names.map do |a|
+ a.to_s.split('_').map do |part|
+ part[0,1]
+ end.join
+ end
+ full_name = [short_names, Digest::MD5.hexdigest(attribute_names.join('::'))].join('_')
+ full_name[0,64].to_sym
end
# Used to generate sql
View
2 spec/relation/namer_spec.rb
@@ -21,7 +21,7 @@
subject.foreign_key_name(:id).should == :user_id
end
specify "a unique key name" do
- subject.unique_key_name([:first_name, :last_name]).should == :user_first_name_last_name
+ subject.unique_key_name([:first_name, :last_name]).should == ('fn_ln_' + Digest::MD5.hexdigest('first_name::last_name')).to_sym
end
specify "a foreign key constraint name" do
pending

0 comments on commit 10e2d6c

Please sign in to comment.