scheman 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,390 @@
1
+ require "spec_helper"
2
+
3
+ describe Scheman::Parsers::Mysql do
4
+ let(:instance) do
5
+ described_class.new
6
+ end
7
+
8
+ describe ".parse" do
9
+ subject do
10
+ begin
11
+ described_class.parse(str).to_hash
12
+ rescue => exception
13
+ puts exception.cause.ascii_tree rescue nil
14
+ raise
15
+ end
16
+ end
17
+
18
+ context "with full syntax" do
19
+ let(:str) do
20
+ <<-EOS.strip_heredoc
21
+ # comment
22
+
23
+ USE database_name;
24
+
25
+ SET variable_name=value;
26
+
27
+ DROP TABLE table_name;
28
+
29
+ CREATE DATABASE database_name;
30
+
31
+ CREATE TABLE `table1` (
32
+ `column1` INTEGER NOT NULL AUTO INCREMENT,
33
+ `column2` VARCHAR(255) NOT NULL,
34
+ PRIMARY KEY (`column1`)
35
+ );
36
+
37
+ ALTER TABLE table_name ADD FOREIGN KEY (column_name) REFERENCES table_name (column_name);
38
+
39
+ INSERT INTO table_name (column_name) VALUES ('value');
40
+
41
+ DELIMITER //
42
+ EOS
43
+ end
44
+
45
+ it "succeeds in parse" do
46
+ should == [
47
+ {
48
+ database_name: "database_name",
49
+ },
50
+ {
51
+ create_table: {
52
+ name: "table1",
53
+ fields: [
54
+ {
55
+ field: {
56
+ name: "column1",
57
+ type: "integer",
58
+ values: [],
59
+ qualifiers: [
60
+ {
61
+ qualifier: {
62
+ type: "not_null",
63
+ },
64
+ },
65
+ {
66
+ qualifier: {
67
+ type: "auto_increment",
68
+ },
69
+ },
70
+ ],
71
+ },
72
+ },
73
+ {
74
+ field: {
75
+ name: "column2",
76
+ type: "varchar",
77
+ values: ["255"],
78
+ qualifiers: [
79
+ {
80
+ qualifier: {
81
+ type: "not_null",
82
+ },
83
+ },
84
+ ],
85
+ },
86
+ },
87
+ ],
88
+ indices: [
89
+ {
90
+ index: {
91
+ column: "column1",
92
+ primary: true,
93
+ name: nil,
94
+ type: nil,
95
+ },
96
+ },
97
+ ],
98
+ },
99
+ },
100
+ ]
101
+ end
102
+ end
103
+
104
+ context "with CREATE DATABASE" do
105
+ let(:str) do
106
+ "CREATE DATABASE database_name;"
107
+ end
108
+
109
+ it "succeeds in parse" do
110
+ should == []
111
+ end
112
+ end
113
+
114
+ context "with CREATE SCHEMA" do
115
+ let(:str) do
116
+ "CREATE SCHEMA database_name;"
117
+ end
118
+
119
+ it "succeeds in parse" do
120
+ should == []
121
+ end
122
+ end
123
+
124
+ context "with NOT NULL field qualifier" do
125
+ let(:str) do
126
+ "CREATE TABLE `table1` (`column1` INTEGER NOT NULL);"
127
+ end
128
+
129
+ it "succeeds in parse field qualifier" do
130
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier][:type].should == "not_null"
131
+ end
132
+ end
133
+
134
+ context "with NULL field qualifier" do
135
+ let(:str) do
136
+ "CREATE TABLE `table1` (`column1` INTEGER NULL);"
137
+ end
138
+
139
+ it "succeeds in parse" do
140
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier][:type].should == "null"
141
+ end
142
+ end
143
+
144
+ context "with AUTO INCREMENT field qualifier" do
145
+ let(:str) do
146
+ "CREATE TABLE `table1` (`column1` INTEGER AUTO INCREMENT);"
147
+ end
148
+
149
+ it "succeeds in parse" do
150
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier][:type].should == "auto_increment"
151
+ end
152
+ end
153
+
154
+ context "with PRIMARY KEY field qualifier" do
155
+ let(:str) do
156
+ "CREATE TABLE `table1` (`column1` INTEGER PRIMARY KEY);"
157
+ end
158
+
159
+ it "succeeds in parse" do
160
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier][:type].should == "primary_key"
161
+ end
162
+ end
163
+
164
+ context "with UNSIGNED field type qualifier" do
165
+ let(:str) do
166
+ "CREATE TABLE `table1` (`column1` INTEGER UNSIGNED);"
167
+ end
168
+
169
+ it "succeeds in parse" do
170
+ subject[0][:create_table][:fields][0][:field][:qualifiers].should be_empty
171
+ end
172
+ end
173
+
174
+ context "with CHARACTER SET field type qualifier" do
175
+ let(:str) do
176
+ "CREATE TABLE `table1` (`column1` VARCHAR(255) CHARACTER SET utf8);"
177
+ end
178
+
179
+ it "succeeds in parse" do
180
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0].should == {
181
+ qualifier: {
182
+ type: "character_set",
183
+ value: "utf8",
184
+ },
185
+ }
186
+ end
187
+ end
188
+
189
+ context "with COLLATE field type qualifier" do
190
+ let(:str) do
191
+ "CREATE TABLE `table1` (`column1` VARCHAR(255) COLLATE utf8_general_ci);"
192
+ end
193
+
194
+ it "succeeds in parse" do
195
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0].should == {
196
+ qualifier: {
197
+ type: "collate",
198
+ value: "utf8_general_ci",
199
+ },
200
+ }
201
+ end
202
+ end
203
+
204
+ context "with UNIQUE KEY field qualifier" do
205
+ let(:str) do
206
+ "CREATE TABLE `table1` (`column1` INTEGER UNIQUE KEY);"
207
+ end
208
+
209
+ it "succeeds in parse" do
210
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier][:type].should == "unique_key"
211
+ end
212
+ end
213
+
214
+ context "with UNIQUE INDEX field qualifier" do
215
+ let(:str) do
216
+ "CREATE TABLE `table1` (`column1` INTEGER UNIQUE INDEX);"
217
+ end
218
+
219
+ it "succeeds in parse" do
220
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier][:type].should == "unique_key"
221
+ end
222
+ end
223
+
224
+ context "with KEY field qualifier" do
225
+ let(:str) do
226
+ "CREATE TABLE `table1` (`column1` INTEGER KEY);"
227
+ end
228
+
229
+ it "succeeds in parse" do
230
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier][:type].should == "key"
231
+ end
232
+ end
233
+
234
+ context "with INDEX field qualifier" do
235
+ let(:str) do
236
+ "CREATE TABLE `table1` (`column1` INTEGER INDEX);"
237
+ end
238
+
239
+ it "succeeds in parse" do
240
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier][:type].should == "key"
241
+ end
242
+ end
243
+
244
+ context "with PRIMARY KEY" do
245
+ let(:str) do
246
+ <<-EOS.strip_heredoc
247
+ CREATE TABLE `table1` (
248
+ `column1` INTEGER,
249
+ PRIMARY KEY (`column1`)
250
+ );
251
+ EOS
252
+ end
253
+
254
+ it "succeeds in parse" do
255
+ subject[0][:create_table][:indices][0].should == {
256
+ index: {
257
+ column: "column1",
258
+ name: nil,
259
+ primary: true,
260
+ type: nil,
261
+ },
262
+ }
263
+ end
264
+ end
265
+
266
+ context "with PRIMARY KEY BTREE" do
267
+ let(:str) do
268
+ <<-EOS.strip_heredoc
269
+ CREATE TABLE `table1` (
270
+ `column1` INTEGER,
271
+ PRIMARY KEY BTREE (`column1`)
272
+ );
273
+ EOS
274
+ end
275
+
276
+ it "succeeds in parse" do
277
+ subject[0][:create_table][:indices][0].should == {
278
+ index: {
279
+ column: "column1",
280
+ name: nil,
281
+ primary: true,
282
+ type: "btree",
283
+ },
284
+ }
285
+ end
286
+ end
287
+
288
+ context "with PRIMARY KEY ... BTREE" do
289
+ let(:str) do
290
+ <<-EOS.strip_heredoc
291
+ CREATE TABLE `table1` (
292
+ `column1` INTEGER,
293
+ PRIMARY KEY (`column1`) BTREE
294
+ );
295
+ EOS
296
+ end
297
+
298
+ it "succeeds in parse" do
299
+ subject[0][:create_table][:indices][0].should == {
300
+ index: {
301
+ column: "column1",
302
+ name: nil,
303
+ primary: true,
304
+ type: "btree",
305
+ },
306
+ }
307
+ end
308
+ end
309
+
310
+ context "with KEY" do
311
+ let(:str) do
312
+ "CREATE TABLE `table1` (`column1` INTEGER, KEY index1 (`column1`));"
313
+ end
314
+
315
+ it "succeeds in parse" do
316
+ subject[0][:create_table][:indices][0].should == {
317
+ index: {
318
+ column: "column1",
319
+ name: "index1",
320
+ type: nil,
321
+ },
322
+ }
323
+ end
324
+ end
325
+
326
+ context "with USING BTREE" do
327
+ let(:str) do
328
+ "CREATE TABLE `table1` (`column1` INTEGER, KEY index1 USING BTREE (`column1`));"
329
+ end
330
+
331
+ it "succeeds in parse" do
332
+ subject[0][:create_table][:indices][0].should == {
333
+ index: {
334
+ column: "column1",
335
+ name: "index1",
336
+ type: "btree",
337
+ },
338
+ }
339
+ end
340
+ end
341
+
342
+ context "with USING BTREE after column name" do
343
+ let(:str) do
344
+ "CREATE TABLE `table1` (`column1` INTEGER, KEY index1 (`column1`) USING BTREE);"
345
+ end
346
+
347
+ it "succeeds in parse" do
348
+ subject[0][:create_table][:indices][0].should == {
349
+ index: {
350
+ column: "column1",
351
+ name: "index1",
352
+ type: "btree",
353
+ },
354
+ }
355
+ end
356
+ end
357
+
358
+ context "with FULLTEXT" do
359
+ let(:str) do
360
+ "CREATE TABLE `table1` (`column1` INTEGER, FULLTEXT index1 (`column1`));"
361
+ end
362
+
363
+ it "succeeds in parse" do
364
+ subject[0][:create_table][:indices][0].should == {
365
+ index: {
366
+ column: "column1",
367
+ name: "index1",
368
+ type: "fulltext",
369
+ },
370
+ }
371
+ end
372
+ end
373
+
374
+ context "with SPATIAL" do
375
+ let(:str) do
376
+ "CREATE TABLE `table1` (`column1` INTEGER, SPATIAL index1 (`column1`));"
377
+ end
378
+
379
+ it "succeeds in parse" do
380
+ subject[0][:create_table][:indices][0].should == {
381
+ index: {
382
+ column: "column1",
383
+ name: "index1",
384
+ type: "spatial",
385
+ },
386
+ }
387
+ end
388
+ end
389
+ end
390
+ end
@@ -0,0 +1,11 @@
1
+ require "active_support/core_ext/string/strip"
2
+ require "scheman"
3
+ require "pathname"
4
+ require "pp"
5
+
6
+ RSpec.configure do |config|
7
+ config.treat_symbols_as_metadata_keys_with_true_values = true
8
+ config.run_all_when_everything_filtered = true
9
+ config.filter_run :focus
10
+ config.add_setting :root, default: Pathname.new(File.expand_path("../../", __FILE__))
11
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scheman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: parslet
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 2.14.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 2.14.1
97
+ description:
98
+ email:
99
+ - r7kamura@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/scheman.rb
110
+ - lib/scheman/diff.rb
111
+ - lib/scheman/errors.rb
112
+ - lib/scheman/parser_builder.rb
113
+ - lib/scheman/parsers/base.rb
114
+ - lib/scheman/parsers/mysql.rb
115
+ - lib/scheman/schema.rb
116
+ - lib/scheman/version.rb
117
+ - lib/scheman/views/base.rb
118
+ - lib/scheman/views/mysql.rb
119
+ - scheman.gemspec
120
+ - spec/scheman/diff_spec.rb
121
+ - spec/scheman/parsers/mysql_spec.rb
122
+ - spec/spec_helper.rb
123
+ homepage: https://github.com/r7kamura/scheman
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.2.2
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Manage database schema based on schema definition file.
147
+ test_files:
148
+ - spec/scheman/diff_spec.rb
149
+ - spec/scheman/parsers/mysql_spec.rb
150
+ - spec/spec_helper.rb
151
+ has_rdoc: