schema_generator 0.2.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/schema_generator.rb +42 -5
  2. data/templates/schema.rb +20 -0
  3. metadata +3 -2
data/schema_generator.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'pathname'
2
+ require 'mysql'
2
3
 
3
4
  class SchemaGenerator < Rails::Generator::Base
4
5
  def initialize(*runtime_args)
@@ -43,7 +44,7 @@ class SchemaGenerator < Rails::Generator::Base
43
44
  return nil unless file =~/\.rb$/
44
45
 
45
46
  begin
46
- file =~ %r{^(db/migrate/#{migration_number}_(.*)).rb$}
47
+ file =~ %r{^(db/migrate/0*#{migration_number}_(.*)).rb$}
47
48
  include_name = $1.to_s
48
49
  class_base_name = $2.camelize
49
50
 
@@ -59,6 +60,7 @@ class SchemaGenerator < Rails::Generator::Base
59
60
  m.template 'schema.postgresql.sql', File.join('db', "schema.postgresql.sql")
60
61
  m.template 'schema.mysql.sql', File.join('db', "schema.mysql.sql")
61
62
  m.template 'schema.sqlite.sql', File.join('db', "schema.sqlite.sql")
63
+ m.template 'schema.rb', File.join('db', "schema.rb")
62
64
  end
63
65
  end
64
66
  end
@@ -76,16 +78,26 @@ module ActiveRecord
76
78
 
77
79
  class << self
78
80
  alias_method :old_find, :find
81
+ alias_method :old_update_all, :update_all
79
82
  alias_method :old_create, :create
83
+ alias_method :old_transaction, :transaction
80
84
 
81
85
  def dbmigrate_find(*args)
82
86
  []
83
87
  end
84
88
 
89
+ def dbmigrate_update_all(*args)
90
+ true
91
+ end
92
+
85
93
  def dbmigrate_create(attributes = nil)
86
94
  DBMigrator::Database.new_data(self,attributes)
87
95
  end
88
96
 
97
+ def dbmigrate_transaction(*objects)
98
+ yield
99
+ end
100
+
89
101
  end
90
102
  end
91
103
 
@@ -106,7 +118,7 @@ module ActiveRecord
106
118
 
107
119
  def self.add_column(tablename,name,type,options={})
108
120
  table=DBMigrator::Database.tables.find{|t| t.name == tablename}
109
- if(table.fields.find{|f| f.name == name})
121
+ if(table.fields.find{|f| f.name.to_s == name.to_s})
110
122
  raise "Duplicate field definition for #{tablename}.#{name}"
111
123
  end
112
124
  table.column(name,type,options)
@@ -116,6 +128,27 @@ module ActiveRecord
116
128
  table = DBMigrator::Database.tables.find{|t| t.name == tablename}
117
129
  field = table.fields.delete_if {|f| f.name == name}
118
130
  end
131
+
132
+ def self.change_column(tablename, name, type, options={})
133
+ table=DBMigrator::Database.tables.find{|t| t.name == tablename}
134
+ column = table.fields.find{|f| f.name.to_s == name.to_s}
135
+ if not column
136
+ raise "Missing field definition for #{tablename}.#{name} in change_column"
137
+ else
138
+ column.type = type
139
+ column.options = options
140
+ end
141
+ end
142
+
143
+ def self.rename_column(tablename, oldname, newname)
144
+ table = DBMigrator::Database.tables.find{|t| t.name == tablename}
145
+ column = table.fields.find{|f| f.name.to_s == oldname.to_s}
146
+ if not column
147
+ raise "Missing field definition for #{tablename}.#{oldname} in rename_column (found #{table.fields.collect {|f| f.name}.join(', ')})"
148
+ else
149
+ column.name = newname
150
+ end
151
+ end
119
152
 
120
153
  def self.add_index(tablename,name,options={})
121
154
  if(DBMigrator::Database.indexes.find {|i| i.table == tablename and i.name == name})
@@ -199,6 +232,8 @@ module DBMigrator
199
232
  class << ActiveRecord::Base
200
233
  alias find old_find
201
234
  alias create old_create
235
+ alias update_all old_update_all
236
+ alias transaction old_transaction
202
237
  end
203
238
 
204
239
  ActiveRecord::Base.class_eval 'alias save old_save'
@@ -208,6 +243,8 @@ module DBMigrator
208
243
  class << ActiveRecord::Base
209
244
  alias find dbmigrate_find
210
245
  alias create dbmigrate_create
246
+ alias update_all dbmigrate_update_all
247
+ alias transaction dbmigrate_transaction
211
248
  end
212
249
  ActiveRecord::Base.class_eval 'alias save dbmigrate_save'
213
250
  end
@@ -217,11 +254,11 @@ module DBMigrator
217
254
  @@commandstring=''
218
255
  case databasename
219
256
  when 'postgresql'
220
- @@dbdriver = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new(nil)
257
+ @@dbdriver = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new(nil,nil)
221
258
  when 'mysql'
222
- @@dbdriver = ActiveRecord::ConnectionAdapters::MysqlAdapter.new(nil,nil)
259
+ @@dbdriver = ActiveRecord::ConnectionAdapters::MysqlAdapter.new(nil,nil,nil)
223
260
  when 'sqlite'
224
- @@dbdriver = ActiveRecord::ConnectionAdapters::SQLiteAdapter.new(nil)
261
+ @@dbdriver = ActiveRecord::ConnectionAdapters::SQLiteAdapter.new(nil,nil)
225
262
  else
226
263
  raise 'Unknown driver'
227
264
  end
@@ -0,0 +1,20 @@
1
+ # This file is autogenerated. Instead of editing this file, please use the
2
+ # migrations feature of ActiveRecord to incrementally modify your database, and
3
+ # then regenerate this schema definition.
4
+
5
+ ActiveRecord::Schema.define(:version => <%= DBMigrator::Database.version %>) do
6
+
7
+ <% DBMigrator::Database.tables.sort_by {|table| table.name.to_s }.each do |table| -%>
8
+ create_table "<%= table.name %>"<% table.options.keys.each do |k| %>, :<%= k %> => <%= table.options[k].inspect %><% end %>, :force => true do |t|
9
+ <% table.fields.each do |field| -%>
10
+ t.column "<%= field.name %>", :<%= field.type %><% field.options.keys.each do |k| %>, :<%= k %> => <%= field.options[k].inspect %><% end %>
11
+ <% end -%>
12
+ end
13
+
14
+ <% end -%>
15
+
16
+ <% DBMigrator::Database.indexes.sort_by {|index| index.table.to_s}.each do |index| -%>
17
+ add_index "<%= index.table %>", ["<%= index.name %>"], :name => "<%= index.table %>_<%= index.name %>_index"
18
+ <% end -%>
19
+
20
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: schema_generator
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2005-10-24 00:00:00 -07:00
6
+ version: 0.9.0
7
+ date: 2005-12-10 00:00:00 -08:00
8
8
  summary: "The rails schema generator generates complete SQL schemas from a collection of
9
9
  rails migrations. It currently produces one schema file each for MySQL,
10
10
  PostgreSQL, and SQLite."
@@ -33,6 +33,7 @@ authors:
33
33
  files:
34
34
  - templates/schema.mysql.sql
35
35
  - templates/schema.postgresql.sql
36
+ - templates/schema.rb
36
37
  - templates/schema.sqlite.sql
37
38
  - schema_generator.rb
38
39
  - README