scheman 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e73fa1b30a5a2616d7b7a44c8b3b2dfd750e3d9
4
- data.tar.gz: dba7df4899f8acaf89b4f55bd611403c773e0768
3
+ metadata.gz: 13a488256dda03bd9e7c75477ab532b805ba3491
4
+ data.tar.gz: c4f69982678f0427156d0d127f7fcdc0eaa9f5d6
5
5
  SHA512:
6
- metadata.gz: 4830bd819919bfb956a0b17a1068300071ba6e1974ac09ee4e9cfd03980043fa80630214f742f91218e8ec107843244d3fd55ba0396d0fe653c1322c4b7adb6d
7
- data.tar.gz: 84543f84774b5ca849df3689d3738b1b89465d14a22ca2d30e32a6e35b4ea17155a71e79073f01ae4b45976f9d4cab8cdb3764d79d594fbfd98ad476df56048f
6
+ metadata.gz: 7c2b41f51bf2f871e20e045c4f4828f292cdf288e4c5f2c8f4697b7a34950d16f05c9529d2f72ba8808f2afca3c3949c5b4b92fa254c4ddb3fb9da2d68d5f9cf
7
+ data.tar.gz: 474496316452b1eb06fd78ff64cdd445dac31c75138d8beb9683c3b107cabfd4849d3a35052959575f51571e7138786c9b87d0ed228f62b24a29cd0c3b02e574
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.0.3
2
+ * Support DEFAULT field qualifier
3
+
1
4
  ## 0.0.2
2
5
  * Support ADD INDEX
3
6
  * Support DROP INDEX
data/README.md CHANGED
@@ -9,7 +9,8 @@ require "scheman"
9
9
 
10
10
  before = <<-SQL
11
11
  CREATE TABLE `table1` (
12
- `column1` INTEGER(11) PRIMARY KEY NOT NULL AUTO INCREMENT
12
+ `column1` INTEGER(11) PRIMARY KEY NOT NULL AUTO INCREMENT,
13
+ `column2` DATETIME DEFAULT NOW()
13
14
  );
14
15
 
15
16
  CREATE TABLE `table2` (
@@ -21,7 +22,8 @@ SQL
21
22
  after = <<-SQL
22
23
  CREATE TABLE `table1` (
23
24
  `column1` CHAR(11) NOT NULL AUTO INCREMENT,
24
- `column2` VARCHAR(255) NOT NULL,
25
+ `column2` DATETIME DEFAULT CURRENT_TIMESTAMP(),
26
+ `column3` VARCHAR(255) NOT NULL DEFAULT "a",
25
27
  PRIMARY KEY (`column2`)
26
28
  );
27
29
 
@@ -46,7 +48,7 @@ CREATE TABLE `table3` (
46
48
  PRIMARY KEY (`column1`)
47
49
  );
48
50
 
49
- ALTER TABLE `table1` ADD COLUMN `column2` VARCHAR(255) NOT NULL,
51
+ ALTER TABLE `table1` ADD COLUMN `column3` VARCHAR(255) NOT NULL DEFAULT "a",
50
52
  CHANGE COLUMN `column1` CHAR(11) NOT NULL AUTO INCREMENT,
51
53
  DROP PRIMARY KEY,
52
54
  ADD PRIMARY KEY `column2`;
data/lib/scheman/diff.rb CHANGED
@@ -75,9 +75,11 @@ module Scheman
75
75
  if before_table = before_schema.tables_indexed_by_name[after_table.name]
76
76
  after_table.fields.each do |after_field|
77
77
  if before_field = before_table.fields_indexed_by_name[after_field.name]
78
- result << {
79
- alter_field: after_field.to_hash.merge(table_name: after_table.name),
80
- }
78
+ if after_field != before_field
79
+ result << {
80
+ alter_field: after_field.to_hash.merge(table_name: after_table.name),
81
+ }
82
+ end
81
83
  end
82
84
  end
83
85
  end
@@ -99,6 +99,33 @@ module Scheman
99
99
  spaces.maybe
100
100
  end
101
101
 
102
+ rule(:string) do
103
+ (
104
+ single_quoted(
105
+ (
106
+ (str('\\') >> match('.')) |
107
+ str("''") |
108
+ match('[^\\\']')
109
+ ).repeat
110
+ ) | double_quoted(
111
+ (
112
+ (str('\\') >> match('.')) |
113
+ str('""') |
114
+ match('[^\\\"]')
115
+ ).repeat
116
+ )
117
+ ).as(:string)
118
+ end
119
+
120
+ rule(:bit) do
121
+ (
122
+ str("b") >> (
123
+ single_quoted(match("0|1").repeat(1, 64)) |
124
+ double_quoted(match("0|1").repeat(1, 64))
125
+ )
126
+ ).as(:bit)
127
+ end
128
+
102
129
  rule(:delimiter) do
103
130
  str(";")
104
131
  end
@@ -107,10 +134,6 @@ module Scheman
107
134
  spaces >> match('[\S]').repeat >> spaces
108
135
  end
109
136
 
110
- rule(:string) do
111
- any.repeat(1)
112
- end
113
-
114
137
  rule(:eol) do
115
138
  delimiter >> spaces?
116
139
  end
@@ -258,7 +281,7 @@ module Scheman
258
281
  (field_qualifier >> (spaces >> field_qualifier).repeat)
259
282
  end
260
283
 
261
- # TODO: default value, on update
284
+ # TODO: on update
262
285
  rule(:field_qualifier) do
263
286
  not_null_qualifier |
264
287
  null_qualifier |
@@ -266,7 +289,26 @@ module Scheman
266
289
  auto_increment_qualifier |
267
290
  character_set_qualifier |
268
291
  collate_qualifier |
269
- unique_key_qualifier
292
+ unique_key_qualifier |
293
+ default_qualifier
294
+ end
295
+
296
+ rule(:default_qualifier) do
297
+ (
298
+ case_insensitive_str("default") >> spaces >> default_value
299
+ ).as(:default_qualifier)
300
+ end
301
+
302
+ rule(:default_value) do
303
+ (current_timestamp | string | bit | unclassified_default_value).as(:default_value)
304
+ end
305
+
306
+ rule(:unclassified_default_value) do
307
+ (match('[\w\d:.-]')).repeat(1).as(:unclassified_default_value)
308
+ end
309
+
310
+ rule(:current_timestamp) do
311
+ (case_insensitive_str("current_timestamp()") | case_insensitive_str("now()")).as(:current_timestamp)
270
312
  end
271
313
 
272
314
  rule(:unique_key_qualifier) do
@@ -441,6 +483,28 @@ module Scheman
441
483
  identifier.to_s
442
484
  end
443
485
 
486
+ rule(string: simple(:value)) do
487
+ {
488
+ type: "string",
489
+ value: value.to_s[1..-2],
490
+ }
491
+ end
492
+
493
+ rule(bit: simple(:value)) do
494
+ {
495
+ type: "bit",
496
+ value: value.to_s[2..-2].to_i(2),
497
+ }
498
+ end
499
+
500
+
501
+ rule(unclassified_default_value: simple(:value)) do
502
+ {
503
+ type: "unclassified",
504
+ value: value.to_s,
505
+ }
506
+ end
507
+
444
508
  rule(field_value: simple(:field_value)) do
445
509
  field_value.to_s
446
510
  end
@@ -529,6 +593,21 @@ module Scheman
529
593
  }
530
594
  end
531
595
 
596
+ rule(default_qualifier: subtree(:tree)) do
597
+ {
598
+ type: "default",
599
+ value: tree,
600
+ }
601
+ end
602
+
603
+ # @note We use Symbol to distinguish from string value
604
+ rule(current_timestamp: simple(:current_timestamp)) do
605
+ {
606
+ type: "current_timestamp",
607
+ value: nil,
608
+ }
609
+ end
610
+
532
611
  rule(database_name: simple(:database_name)) do
533
612
  {
534
613
  database_name: database_name.to_s,
@@ -105,7 +105,7 @@ module Scheman
105
105
  # @note Size can be 2 values but not supported yet
106
106
  # @return [String, nil]
107
107
  def size
108
- field[:size]
108
+ @field[:size]
109
109
  end
110
110
 
111
111
  # @return [Array<Hash>] Sorted qualifiers, without index-related types
@@ -1,3 +1,3 @@
1
1
  module Scheman
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -79,6 +79,10 @@ module Scheman
79
79
  rule(drop_index: subtree(:drop_index)) do
80
80
  DropIndex.new(drop_index)
81
81
  end
82
+
83
+ rule(default_value: subtree(:tree)) do
84
+ DefaultValue.new(tree)
85
+ end
82
86
  end
83
87
 
84
88
  class Node
@@ -344,6 +348,19 @@ module Scheman
344
348
  end
345
349
  end
346
350
  end
351
+
352
+ class DefaultValue < Node
353
+ def to_s
354
+ case @element[:type]
355
+ when "string"
356
+ @element[:value].inspect
357
+ when "current_timestamp"
358
+ "CURRENT_TIMESTAMP()"
359
+ else
360
+ @element[:value]
361
+ end
362
+ end
363
+ end
347
364
  end
348
365
  end
349
366
  end
@@ -20,7 +20,8 @@ describe Scheman::Diff do
20
20
  let(:before_schema) do
21
21
  <<-EOS.strip_heredoc
22
22
  CREATE TABLE `table1` (
23
- `column1` INTEGER(11) PRIMARY KEY NOT NULL AUTO INCREMENT
23
+ `column1` INTEGER(11) PRIMARY KEY NOT NULL AUTO INCREMENT,
24
+ `column2` DATETIME DEFAULT NOW()
24
25
  );
25
26
 
26
27
  CREATE TABLE `table2` (
@@ -34,7 +35,8 @@ describe Scheman::Diff do
34
35
  <<-EOS.strip_heredoc
35
36
  CREATE TABLE `table1` (
36
37
  `column1` CHAR(11) NOT NULL AUTO INCREMENT,
37
- `column2` VARCHAR(255) NOT NULL,
38
+ `column2` DATETIME DEFAULT CURRENT_TIMESTAMP(),
39
+ `column3` VARCHAR(255) NOT NULL DEFAULT "a",
38
40
  PRIMARY KEY (`column2`)
39
41
  );
40
42
 
@@ -61,7 +63,7 @@ describe Scheman::Diff do
61
63
  PRIMARY KEY (`column1`)
62
64
  );
63
65
 
64
- ALTER TABLE `table1` ADD COLUMN `column2` VARCHAR(255) NOT NULL,
66
+ ALTER TABLE `table1` ADD COLUMN `column3` VARCHAR(255) NOT NULL DEFAULT "a",
65
67
  CHANGE COLUMN `column1` CHAR(11) NOT NULL AUTO INCREMENT,
66
68
  DROP PRIMARY KEY,
67
69
  ADD PRIMARY KEY `column2`;
@@ -241,6 +241,114 @@ describe Scheman::Parsers::Mysql do
241
241
  end
242
242
  end
243
243
 
244
+ context "with DEFAULT CURRENT_TIMESTAMP()" do
245
+ let(:str) do
246
+ "CREATE TABLE `table1` (`column1` INTEGER DEFAULT CURRENT_TIMESTAMP());"
247
+ end
248
+
249
+ it "succeeds in parse" do
250
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier].should == {
251
+ type: "default",
252
+ value: {
253
+ default_value: {
254
+ type: "current_timestamp",
255
+ value: nil,
256
+ },
257
+ },
258
+ }
259
+ end
260
+ end
261
+
262
+ context "with DEFAULT NOW()" do
263
+ let(:str) do
264
+ "CREATE TABLE `table1` (`column1` INTEGER DEFAULT NOW());"
265
+ end
266
+
267
+ it "succeeds in parse" do
268
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier].should == {
269
+ type: "default",
270
+ value: {
271
+ default_value: {
272
+ type: "current_timestamp",
273
+ value: nil,
274
+ },
275
+ },
276
+ }
277
+ end
278
+ end
279
+
280
+ context "with DEFAULT with double quoted value" do
281
+ let(:str) do
282
+ %<CREATE TABLE `table1` (`column1` INTEGER DEFAULT "a");>
283
+ end
284
+
285
+ it "succeeds in parse" do
286
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier].should == {
287
+ type: "default",
288
+ value: {
289
+ default_value: {
290
+ type: "string",
291
+ value: "a",
292
+ },
293
+ },
294
+ }
295
+ end
296
+ end
297
+
298
+ context "with DEFAULT with single quoted value" do
299
+ let(:str) do
300
+ "CREATE TABLE `table1` (`column1` INTEGER DEFAULT 'a');"
301
+ end
302
+
303
+ it "succeeds in parse" do
304
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier].should == {
305
+ type: "default",
306
+ value: {
307
+ default_value: {
308
+ type: "string",
309
+ value: "a",
310
+ },
311
+ },
312
+ }
313
+ end
314
+ end
315
+
316
+ context "with DEFAULT with bit value" do
317
+ let(:str) do
318
+ "CREATE TABLE `table1` (`column1` INTEGER DEFAULT b'1111');"
319
+ end
320
+
321
+ it "succeeds in parse" do
322
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier].should == {
323
+ type: "default",
324
+ value: {
325
+ default_value: {
326
+ type: "bit",
327
+ value: 15,
328
+ },
329
+ },
330
+ }
331
+ end
332
+ end
333
+
334
+ context "with DEFAULT with unclassified value" do
335
+ let(:str) do
336
+ "CREATE TABLE `table1` (`column1` INTEGER DEFAULT 0);"
337
+ end
338
+
339
+ it "succeeds in parse" do
340
+ subject[0][:create_table][:fields][0][:field][:qualifiers][0][:qualifier].should == {
341
+ type: "default",
342
+ value: {
343
+ default_value: {
344
+ type: "unclassified",
345
+ value: "0",
346
+ },
347
+ },
348
+ }
349
+ end
350
+ end
351
+
244
352
  context "with PRIMARY KEY" do
245
353
  let(:str) do
246
354
  <<-EOS.strip_heredoc
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scheman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-08 00:00:00.000000000 Z
11
+ date: 2014-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport