Skip to content

Commit fb718d4

Browse files
committed
add change_primary_key method
1 parent 6db1658 commit fb718d4

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

README.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ FEATURES
1212
* `drop_foreign_key(table, field)`
1313
* **primary keys**
1414
* `primary_key(table, field)`
15+
* `change_primary_key(table, field)`
1516
* **repair_tables**
1617
* `repair_tables :users, :clients`
1718
* `repair_tables` (all tables)

lib/migration_helper.rb

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ def primary_key(table, field)
3131
execute "ALTER TABLE #{table} ADD PRIMARY KEY(#{field_list(field)})"
3232
end
3333

34+
# Modifies the primary key of +table+, which right now has already a primary key defined
35+
#
36+
# table: The table name
37+
# field: A field (or array of fields) of the table that will be part of the primary key
38+
def change_primary_key(table, field)
39+
execute "ALTER TABLE #{table} DROP PRIMARY KEY, ADD PRIMARY KEY(#{field_list(field)})"
40+
end
41+
3442
# Execute REPAIR TABLE in each table given as parameter or in all of them
3543
# if none is indicated
3644
#

0 commit comments

Comments
 (0)