kelredd-useful 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,86 @@
1
+ module Useful
2
+ module ActiveRecordHelpers
3
+ module MysqlMigrationHelpers
4
+
5
+ module ClassMethods
6
+
7
+ def foreign_key(from_table, from_column, to_table, opts={})
8
+ opts[:destination_key] = 'id' unless opts[:destination_key]
9
+ constraint_name = "fk_#{from_table}_#{from_column}"
10
+ execute %{alter table #{from_table}
11
+ add constraint #{constraint_name}
12
+ foreign key (#{from_column})
13
+ references #{to_table}(#{opts[:destination_key]})}
14
+ end
15
+
16
+ def drop_foreign_key(from_table, *from_columns)
17
+ from_columns.each { |from_column|
18
+ constraint_name = "fk_#{from_table}_#{from_column}"
19
+ execute %{alter table #{from_table} drop FOREIGN KEY #{constraint_name}}
20
+ }
21
+ end
22
+
23
+ def remove_column_with_fk(table, column)
24
+ drop_foreign_key(table, column)
25
+ remove_column(table, column)
26
+ end
27
+
28
+ def safe_drop_table(table_name)
29
+ execute "drop table if exists #{table_name}"
30
+ end
31
+
32
+ def unique_constraint(table_name, columns)
33
+ execute %{ALTER TABLE #{table_name} ADD CONSTRAINT uniq_#{columns.join('_')} UNIQUE KEY (#{columns.join(", ")})}
34
+ end
35
+
36
+ def set_auto_increment(table, number)
37
+ execute %{ALTER TABLE #{table} AUTO_INCREMENT = #{number}}
38
+ end
39
+
40
+ def clear_table(table_to_clear)
41
+ execute %{delete from #{table_to_clear}}
42
+ end
43
+
44
+ def create_view(view_name,sql_query_definition)
45
+ execute %{
46
+ CREATE SQL SECURITY INVOKER VIEW #{view_name.to_s} AS
47
+ #{sql_query_definition}
48
+ }
49
+ end
50
+
51
+ def drop_view(view_name)
52
+ execute %{
53
+ DROP VIEW #{view_name}
54
+ }
55
+ end
56
+
57
+ def alter_view(view_name,sql_query_definition)
58
+ execute %{
59
+ ALTER SQL SECURITY INVOKER VIEW #{view_name.to_s} AS
60
+ #{sql_query_definition}
61
+ }
62
+ end
63
+
64
+ def raise_err(msg = '')
65
+ raise ActiveRecord::IrreversibleMigration, msg
66
+ end
67
+
68
+ end
69
+
70
+ module InstanceMethods
71
+ end
72
+
73
+ def self.included(receiver)
74
+ receiver.extend ClassMethods
75
+ receiver.send :include, InstanceMethods
76
+ end
77
+
78
+ end
79
+ end
80
+ end
81
+
82
+ module ActiveRecord
83
+ class Migration
84
+ include Useful::ActiveRecordHelpers::MysqlMigrationHelpers
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ Dir[File.join(File.dirname(__FILE__), "active_record_helpers" ,"*.rb")].each do |file|
2
+ require file
3
+ end
@@ -3,7 +3,7 @@ module Useful
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- TINY = 3
6
+ TINY = 4
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kelredd-useful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelredd
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-16 00:00:00 -07:00
12
+ date: 2009-06-17 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,6 +25,9 @@ files:
25
25
  - README.rdoc
26
26
  - Rakefile
27
27
  - lib/useful
28
+ - lib/useful/active_record_helpers
29
+ - lib/useful/active_record_helpers/mysql_migration_helpers.rb
30
+ - lib/useful/active_record_helpers.rb
28
31
  - lib/useful/rails_extensions
29
32
  - lib/useful/rails_extensions/environment_tests.rb
30
33
  - lib/useful/rails_extensions.rb