schema_monkey 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/schema_monkey/active_record/connection_adapters/mysql_adapter.rb +2 -2
- data/lib/schema_monkey/active_record/connection_adapters/postgresql_adapter.rb +1 -1
- data/lib/schema_monkey/active_record/connection_adapters/sqlite3_adapter.rb +2 -2
- data/lib/schema_monkey/active_record/schema_dumper.rb +53 -5
- data/lib/schema_monkey/middleware.rb +6 -2
- data/lib/schema_monkey/module_support.rb +1 -1
- data/lib/schema_monkey/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64213e6bb8ad8a77e11fb2f07d485297cfde3274
|
4
|
+
data.tar.gz: e565725ac60a6c33273d17361a298e645c75534c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7db8563d4c8d9c15b3be4ba2e8f81b1a0bcdfb3ce438591984e6157891957ca6bbd09734116b6f044a061c883bca2ff8b53a03035f693f1615d4cb08adf874c1
|
7
|
+
data.tar.gz: 1132f8762ff275080817795e6744348352dbed392f9ac742a7ddeece6e9d39973b3c30ef9e1e9cae34bc82467a2679cb0ac53743aa76758b7a8aa2204c64b883
|
@@ -14,13 +14,13 @@ module SchemaMonkey
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def indexes_with_schema_monkey(table_name, query_name=nil)
|
17
|
-
Middleware::Query::Indexes.start connection: self, table_name: table_name, query_name: query_name do |env|
|
17
|
+
Middleware::Query::Indexes.start connection: self, table_name: table_name, query_name: query_name, index_definitions: [] do |env|
|
18
18
|
env.index_definitions += indexes_without_schema_monkey env.table_name, env.query_name
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
def tables_with_schema_monkey(query_name=nil, database=nil, like=nil)
|
23
|
-
Middleware::Query::Tables.start connection: self, query_name: query_name, database: database, like: like do |env|
|
23
|
+
Middleware::Query::Tables.start connection: self, query_name: query_name, database: database, like: like, tables: [] do |env|
|
24
24
|
env.tables += tables_without_schema_monkey env.query_name, env.database, env.like
|
25
25
|
end
|
26
26
|
end
|
@@ -20,7 +20,7 @@ module SchemaMonkey
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def indexes_with_schema_monkey(table_name, query_name=nil)
|
23
|
-
Middleware::Query::Indexes.start connection: self, table_name: table_name, query_name: query_name do |env|
|
23
|
+
Middleware::Query::Indexes.start connection: self, table_name: table_name, query_name: query_name, index_definitions: [] do |env|
|
24
24
|
env.index_definitions += indexes_without_schema_monkey env.table_name, env.query_name
|
25
25
|
end
|
26
26
|
end
|
@@ -14,13 +14,13 @@ module SchemaMonkey
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def indexes_with_schema_monkey(table_name, query_name=nil)
|
17
|
-
Middleware::Query::Indexes.start connection: self, table_name: table_name, query_name: query_name do |env|
|
17
|
+
Middleware::Query::Indexes.start connection: self, table_name: table_name, query_name: query_name, index_definitions: [] do |env|
|
18
18
|
env.index_definitions += indexes_without_schema_monkey env.table_name, env.query_name
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
def tables_with_schema_monkey(query_name=nil, table_name=nil)
|
23
|
-
Middleware::Query::Tables.start connection: self, query_name: query_name, table_name: table_name do |env|
|
23
|
+
Middleware::Query::Tables.start connection: self, query_name: query_name, table_name: table_name, tables: [] do |env|
|
24
24
|
env.tables += tables_without_schema_monkey env.query_name, env.table_name
|
25
25
|
end
|
26
26
|
end
|
@@ -48,15 +48,22 @@ module SchemaMonkey
|
|
48
48
|
@dependencies[tablename].sort.uniq.reject{|t| @dumper.ignored? t}.each(&block)
|
49
49
|
end
|
50
50
|
|
51
|
-
class Table < KeyStruct[:name, :pname, :options, :columns,
|
51
|
+
class Table < KeyStruct[:name, :pname, :options, :columns, :indexes, :statements, :trailer]
|
52
|
+
def initialize(*args)
|
53
|
+
super
|
54
|
+
self.columns ||= []
|
55
|
+
self.indexes ||= []
|
56
|
+
self.statements ||= []
|
57
|
+
self.trailer ||= []
|
58
|
+
end
|
52
59
|
|
53
60
|
def assemble(stream)
|
54
61
|
stream.write " create_table #{pname.inspect}"
|
55
62
|
stream.write ", #{options}" unless options.blank?
|
56
63
|
stream.puts " do |t|"
|
57
|
-
typelen =
|
58
|
-
namelen =
|
59
|
-
|
64
|
+
typelen = columns.map{|col| col.type.length}.max
|
65
|
+
namelen = columns.map{|col| col.name.length}.max
|
66
|
+
columns.each do |column|
|
60
67
|
stream.write " "
|
61
68
|
column.assemble(stream, typelen, namelen)
|
62
69
|
stream.puts ""
|
@@ -65,6 +72,11 @@ module SchemaMonkey
|
|
65
72
|
stream.puts " #{statement}"
|
66
73
|
end
|
67
74
|
stream.puts " end"
|
75
|
+
indexes.each do |index|
|
76
|
+
stream.write " add_index #{pname.inspect}, "
|
77
|
+
index.assemble(stream)
|
78
|
+
stream.puts ""
|
79
|
+
end
|
68
80
|
trailer.each do |statement|
|
69
81
|
stream.puts " #{statement}"
|
70
82
|
end
|
@@ -95,6 +107,21 @@ module SchemaMonkey
|
|
95
107
|
stream.write "# #{comments}" unless comments.blank?
|
96
108
|
end
|
97
109
|
end
|
110
|
+
|
111
|
+
class Index < KeyStruct[:name, :columns, :options]
|
112
|
+
|
113
|
+
def add_option(option)
|
114
|
+
self.options = [options, option].reject(&:blank?).join(', ')
|
115
|
+
end
|
116
|
+
|
117
|
+
def assemble(stream)
|
118
|
+
stream.write [
|
119
|
+
columns.inspect,
|
120
|
+
"name: #{name.inspect}",
|
121
|
+
options
|
122
|
+
].reject(&:blank?).join(", ")
|
123
|
+
end
|
124
|
+
end
|
98
125
|
end
|
99
126
|
end
|
100
127
|
|
@@ -106,6 +133,7 @@ module SchemaMonkey
|
|
106
133
|
alias_method_chain :table, :schema_monkey
|
107
134
|
alias_method_chain :foreign_keys, :schema_monkey
|
108
135
|
alias_method_chain :trailer, :schema_monkey
|
136
|
+
alias_method_chain :indexes, :schema_monkey
|
109
137
|
public :ignored?
|
110
138
|
end
|
111
139
|
end
|
@@ -132,7 +160,7 @@ module SchemaMonkey
|
|
132
160
|
Middleware::Dumper::Extensions.start dumper: self, connection: @connection, extensions: @dump.extensions do |env|
|
133
161
|
stream = StringIO.new
|
134
162
|
extensions_without_schema_monkey(stream)
|
135
|
-
|
163
|
+
env.dump.extensions << stream.string unless stream.string.blank?
|
136
164
|
end
|
137
165
|
end
|
138
166
|
|
@@ -174,6 +202,26 @@ module SchemaMonkey
|
|
174
202
|
}
|
175
203
|
end
|
176
204
|
end
|
205
|
+
|
206
|
+
def indexes_with_schema_monkey(table, _)
|
207
|
+
Middleware::Dumper::Indexes.start dumper: self, connection: @connection, dump: @dump, table: @dump.tables[table] do |env|
|
208
|
+
stream = StringIO.new
|
209
|
+
indexes_without_schema_monkey(env.table.name, stream)
|
210
|
+
env.table.indexes += stream.string.split("\n").map { |string|
|
211
|
+
m = string.strip.match %r{
|
212
|
+
^
|
213
|
+
add_index \s*
|
214
|
+
[:'"](?<table>[^'"\s]+)['"]? \s* , \s*
|
215
|
+
(?<columns>.*) \s*
|
216
|
+
name: \s* [:'"](?<name>[^'"\s]+)['"]? \s*
|
217
|
+
(, \s* (?<options>.*))?
|
218
|
+
$
|
219
|
+
}x
|
220
|
+
columns = m[:columns].tr(%q{[]'":}, '').strip.split(/\s*,\s*/)
|
221
|
+
Dump::Table::Index.new name: m[:name], columns: columns, options: m[:options]
|
222
|
+
}
|
223
|
+
end
|
224
|
+
end
|
177
225
|
end
|
178
226
|
end
|
179
227
|
end
|
@@ -48,12 +48,12 @@ module SchemaMonkey
|
|
48
48
|
extend Stack
|
49
49
|
# :database and :like are only for mysql
|
50
50
|
# :table_name is only for sqlite3
|
51
|
-
Env = KeyStruct[:connection, :query_name, :table_name, :database, :like, tables
|
51
|
+
Env = KeyStruct[:connection, :query_name, :table_name, :database, :like, :tables]
|
52
52
|
end
|
53
53
|
|
54
54
|
module Indexes
|
55
55
|
extend Stack
|
56
|
-
Env = KeyStruct[:connection, :table_name, :query_name, index_definitions
|
56
|
+
Env = KeyStruct[:connection, :table_name, :query_name, :index_definitions]
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
@@ -95,6 +95,10 @@ module SchemaMonkey
|
|
95
95
|
extend Stack
|
96
96
|
Env = KeyStruct[:dumper, :connection, :dump, :table]
|
97
97
|
end
|
98
|
+
module Indexes
|
99
|
+
extend Stack
|
100
|
+
Env = KeyStruct[:dumper, :connection, :dump, :table]
|
101
|
+
end
|
98
102
|
end
|
99
103
|
|
100
104
|
module Model
|
@@ -39,9 +39,9 @@ module SchemaMonkey
|
|
39
39
|
modules << parent if opts.and_self
|
40
40
|
modules += parent.constants.reject{|c| parent.autoload? c}.map{|c| parent.const_get(c)}.select(&it.is_a?(Module))
|
41
41
|
modules.reject! &it.to_s =~ opts.reject if opts.reject
|
42
|
+
modules += modules.flat_map { |mod| get_modules(mod, reject: opts.reject, recursive: true) } if opts.recursive
|
42
43
|
modules.select! &it.to_s =~ opts.match if opts.match
|
43
44
|
modules.select! &it.respond_to?(opts.respond_to) if opts.respond_to
|
44
|
-
modules += modules.flat_map { |mod| get_modules(mod, opts.except(:prefix, :and_self)) } if opts.recursive
|
45
45
|
modules
|
46
46
|
end
|
47
47
|
end
|