This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgen_tables.rb
107 lines (98 loc) · 3.06 KB
/
gen_tables.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require_relative 'extra_databases'
require_relative 'insert_into_file'
require 'pmap'
require 'etc'
def gen_scaffold(t)
clazz_name = t.camelize.singularize
cols = ActiveRecord::Base.connection.columns(t).delete_if{|x| x.name=="created_at" || x.name=="updated_at"}
fields = cols.map{|x| x.name+":"+x.type.to_s}.join(" ")
puts "rails g scaffold #{clazz_name} #{fields} -f" if $verbose
system("rails g scaffold #{clazz_name} #{fields} -f > /dev/null")
end
def gen_route(t)
route = <<-FOO
match '#{t}(/:action(/:id(.:format)))', via: [:options], to: lambda {|env| [200, {'Content-Type' => 'text/plain'}, ["OK\n"]]}
match '#{t}/batch_update(.:format)', action: :batch_update, controller: :#{t}, via: [:post]
FOO
open('config/route_codegen.rb', 'a') do |f|
f.puts route
end
end
def fix_primary_key(t, id='id')
single = t.singularize
filename = "app/models/#{single}.rb"
insert_into_file(filename, "\n self.primary_key = '"+id+"'", "\nend", false)
end
def try_fix_primary_key(t, views)
clazz = Object.const_get(t.singularize.camelize)
if clazz.primary_key
if views.find{|x| x==t}
fix_primary_key(t, clazz.primary_key) #activerecord库对视图的主键处理有bug,所以这里强制设置视图的主键
end
return
end
if clazz.attribute_names.find{|x| x=='id'}
fix_primary_key(t)
return
end
id = clazz.attribute_names.find{|x| x.ends_with? '_id'}
if id
puts "警告:表#{t}的主键没有,启发式规则设置为#{id}"
fix_primary_key(t, id)
else
puts "警告: 表#{t}不存在主键"
end
end
def fix_table_name(t)
single = t.singularize
if single == t || single+"s" != t #表的名字是单数,或者是类似y结尾的不规则英文复数规则
filename = "app/models/#{single}.rb"
insert_into_file(filename, "\n self.table_name = '#{t}'", "\nend", false)
end
end
def fix_connection(t, extra_db)
single = t.singularize
filename = "app/models/#{single}.rb"
str = extra_db+'_#{Rails.env}'
insert_into_file(filename, "\n establish_connection \"#{str}\".to_sym", "\nend", false)
end
def gen_db_tables(hash, re_try=false, parallel=false)
errors = {}
hash.each do |db, tables|
establish_conn(db)
errors[db] = []
tables.each do |t|
print '.' unless $verbose
next if t == 'ar_internal_metadata' || t == 'ar_internal_metadatas'
next if t == 'schema_migrations'
succ = true
begin
flag = gen_scaffold(t)
unless flag
errors[db] << t
succ = false
end
rescue Exception => e
puts e if $verbose
errors[db] << t
succ = false
end
if succ
gen_route(t)
fix_table_name(t)
fix_connection(t, db) if db != :DEFAULT
end
end
end
if re_try && errors.size>0
puts "\nretry #{errors}"
gen_db_tables(errors, false, false)
end
if re_try # 表示首次调用,非递归
hash.each do |db, tables|
establish_conn(db)
views = ActiveRecord::Base.connection.retrieve_views
tables.each{|t| try_fix_primary_key(t, views) }
end
end
end