db_suit_rails 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,48 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ # Author: M. Sakano (Wise Babel Ltd)
4
+
5
+ require 'db_suit_rails/sql_skelton/fkey'
6
+
7
+ $stdout.sync=true
8
+ $stderr.sync=true
9
+ # print '$LOAD_PATH=';p $LOAD_PATH
10
+
11
+ #################################################
12
+ # Unit Test
13
+ #################################################
14
+
15
+ #if $0 == __FILE__
16
+ gem "minitest"
17
+ # require 'minitest/unit'
18
+ require 'minitest/autorun'
19
+ # MiniTest::Unit.autorun
20
+
21
+ class TestUnitSqlSkeltonFkey < MiniTest::Test
22
+ T = true
23
+ F = false
24
+ SCFNAME = File.basename(__FILE__)
25
+
26
+ def setup
27
+ # @ib = 1
28
+ end
29
+ # teardown is not often used.
30
+ def teardown
31
+ # @foo = nil
32
+ end
33
+
34
+
35
+ def test_basic01
36
+ fk = SqlSkelton::Fkey.new('job', 'person_id', 'company', 'eid')
37
+ assert_equal 'job', fk.child[:tbl]
38
+ assert_equal 'person_id', fk.child[:col]
39
+ assert_equal 'company', fk.parent[:tbl]
40
+ assert_equal 'eid', fk.parent[:col]
41
+ assert_raises(DbSuitRailsFkeyError){ SqlSkelton::Fkey.new('job', nil, 'company', 'eid') }
42
+ end
43
+
44
+ end # class TestUnitSqlSkeltonFkey < MiniTest::Test
45
+
46
+ #end # if $0 == __FILE__
47
+
48
+
@@ -0,0 +1,145 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ # Author: M. Sakano (Wise Babel Ltd)
4
+
5
+ require 'db_suit_rails/sql_skelton/tbl_index'
6
+
7
+ $stdout.sync=true
8
+ $stderr.sync=true
9
+ # print '$LOAD_PATH=';p $LOAD_PATH
10
+
11
+ #################################################
12
+ # Unit Test
13
+ #################################################
14
+
15
+ #if $0 == __FILE__
16
+ gem "minitest"
17
+ # require 'minitest/unit'
18
+ require 'minitest/autorun'
19
+ # MiniTest::Unit.autorun
20
+
21
+ # For test use.
22
+ NewTmpClass = true
23
+
24
+ class TestUnitSqlSkeltonTblIndex < MiniTest::Test
25
+ T = true
26
+ F = false
27
+ SCFNAME = File.basename(__FILE__)
28
+ TI = SqlSkelton::TblIndex
29
+
30
+ def setup
31
+ # @ib = 1
32
+ end
33
+ # teardown is not often used.
34
+ def teardown
35
+ # @foo = nil
36
+ end
37
+
38
+
39
+ def test_initialize01
40
+ c1 = TI.new('tblold')
41
+ assert_equal ['tblold'], c1.tblmaps.keys
42
+ c2 = TI.new(['tblold'])
43
+ assert_equal ['tblold'], c2.tblmaps.keys
44
+ c3 = TI.new()
45
+ c3.push('tblold')
46
+ assert_equal ['tblold'], c3.tblmaps.keys
47
+ c3.push('tblold2')
48
+ assert_equal %w(tblold tblold2), c3.tblmaps.keys
49
+ end
50
+
51
+ def test_push01
52
+ oldtbl = 'tblold1'
53
+ ci = TI.new(oldtbl)
54
+ assert_raises(DbSuitRailsError){ ci.push(oldtbl) }
55
+
56
+ assert ci.table_exist?(oldtbl)
57
+ assert !ci.table_exist?('naiyo')
58
+
59
+ ci.set_newtblval_with(oldtbl, nil)
60
+ assert_nil ci.newtblval(oldtbl)
61
+ end
62
+
63
+ def test_update01
64
+ oldtbls = %w(tblold1 tblold2 tblold3)
65
+ newtbls = %w(tblold1s tblold2s tblold3s)
66
+ ci = TI.new(oldtbls[0])
67
+
68
+ ci.push(oldtbls[1])
69
+
70
+ assert_equal oldtbls[0..1], ci.oldtblnames()
71
+ assert_equal oldtbls[0..0]+[nil], ci.oldtblnames(exclude: oldtbls[1], compact: false)
72
+ assert_equal oldtbls[0..0], ci.oldtblnames(exclude: oldtbls[1], compact: true)
73
+
74
+ assert_raises(DbSuitRailsError){ ci.update('naiyo') }
75
+
76
+ assert_nil ci.newtblval(oldtbls[0])
77
+ ci.update(oldtbls[0])
78
+ assert_equal oldtbls[0]+'s', ci.newtblval(oldtbls[0]) # Pluralized
79
+
80
+ # Test update()
81
+ ci.set_newtblval_with(oldtbls[1], 'tekitoh')
82
+ assert_equal 'tekitoh', ci.newtblval(oldtbls[1])
83
+ ci.update( oldtbls[1])
84
+ assert_equal 'tekitoh', ci.newtblval(oldtbls[1])
85
+ ci.update( oldtbls[1], force: true)
86
+ assert_equal oldtbls[1]+'s', ci.newtblval(oldtbls[1]) # Pluralized
87
+
88
+ # Test update!() - wrapper of push()
89
+ ci.update!( oldtbls[2])
90
+ assert_equal oldtbls[2]+'s', ci.newtblval(oldtbls[2]) # Pluralized
91
+
92
+ # Test update!() - wrapper of update()
93
+ ci.set_newtblval_with(oldtbls[1], 'tekitoh')
94
+ assert_equal 'tekitoh', ci.newtblval(oldtbls[1])
95
+ ci.update!( oldtbls[1])
96
+ assert_equal oldtbls[1]+'s', ci.newtblval(oldtbls[1]) # Pluralized
97
+
98
+ # Test newtblnames()
99
+ assert_equal newtbls, ci.newtblnames()
100
+
101
+ # Test update!(:all) - wrapper of update()
102
+ ci.set_newtblval_with(oldtbls[1], 'tekitoh')
103
+ assert_equal 'tekitoh', ci.newtblval(oldtbls[1])
104
+ assert (newtbls != ci.newtblnames())
105
+ ci.update!(:all)
106
+ assert_equal oldtbls[1]+'s', ci.newtblval(oldtbls[1]) # Pluralized
107
+ assert_equal newtbls, ci.newtblnames()
108
+ assert_equal newtbls[0..1], ci.newtblnames(exclude: oldtbls[2], compact: true)
109
+ end
110
+
111
+
112
+ def test_mk_newtblname01
113
+ oldtbls = %w(study study_id_seq study_pkey studyunique string objects new_tmp_classes)
114
+ newtbls = %w(studies studies_id_seq studies_pkey studiesunique string_clis object_clis new_tmp_class_clis)
115
+ ci = TI.new(oldtbls)
116
+
117
+ assert_equal [nil]*oldtbls.size, ci.newtblnames()
118
+ oldtbls.each_index do |i|
119
+ assert_equal newtbls[i], ci.instance_eval{ mk_newtblname(oldtbls[i]) }
120
+ end
121
+
122
+ ci.update!()
123
+ assert_equal newtbls, ci.newtblnames()
124
+ end
125
+
126
+ def test_updated_tbl01
127
+ oldtbls = %w(study study_id_seq study_pkey studyunique)
128
+ newtbls = %w(studies studies_id_seq studies_pkey studiesunique)
129
+ ci = TI.new()
130
+
131
+ assert_equal newtbls[0], ci.updated_tbl!(oldtbls[0])
132
+
133
+ ## updated_tbl! not updates if the record exists.
134
+ ci.push(oldtbls[1])
135
+ assert( newtbls[1] != ci.updated_tbl!(oldtbls[1]))
136
+
137
+ ## update! does update.
138
+ ci.update!(oldtbls[1])
139
+ assert_equal newtbls[1], ci.updated_tbl!(oldtbls[1])
140
+ end
141
+ end # class TestUnitSqlSkeltonTblIndex < MiniTest::Test
142
+
143
+ #end # if $0 == __FILE__
144
+
145
+
@@ -0,0 +1,245 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ # Author: M. Sakano (Wise Babel Ltd)
4
+
5
+ require 'tempfile'
6
+ require 'db_suit_rails/sql_skelton'
7
+
8
+ $stdout.sync=true
9
+ $stderr.sync=true
10
+ # print '$LOAD_PATH=';p $LOAD_PATH
11
+
12
+ #################################################
13
+ # Unit Test
14
+ #################################################
15
+
16
+ #if $0 == __FILE__
17
+ gem "minitest"
18
+ # require 'minitest/unit'
19
+ require 'minitest/autorun'
20
+ # MiniTest::Unit.autorun
21
+
22
+ class TestUnitSqlSkelton < MiniTest::Test
23
+ T = true
24
+ F = false
25
+ SCFNAME = File.basename(__FILE__)
26
+ SS = SqlSkelton
27
+
28
+ def setup
29
+ @tmpioin = Tempfile.new
30
+ @tmpioin.sync=true
31
+
32
+ @tmpioin.puts <<EOF
33
+
34
+ --
35
+ -- Name: shop_b01; Type: TABLE; Schema: public; Owner: seller
36
+ --
37
+
38
+ CREATE TABLE public.shop_b01 (
39
+ id integer NOT NULL,
40
+ tax_id bigint varying(50) NOT NULL REFERENCES tax_table(vat_id),
41
+ office_id character varying(50) NOT NULL,
42
+ file_path character varying(300) NOT NULL
43
+ );
44
+
45
+ ALTER TABLE shop_b01 OWNER TO seller;
46
+
47
+ --
48
+ -- Name: shop_b01_id_seq; Type: SEQUENCE; Schema: public; Owner: seller
49
+ --
50
+
51
+ CREATE SEQUENCE shop_b01_id_seq
52
+ START WITH 1
53
+ CACHE 1;
54
+
55
+ ALTER TABLE shop_b01_id_seq OWNER TO seller;
56
+
57
+ --
58
+ -- Name: shop_b01 id; Type: DEFAULT; Schema: public; Owner: seller
59
+ --
60
+
61
+ ALTER TABLE ONLY shop_b01 ALTER COLUMN id SET DEFAULT nextval('shop_b01_id_seq'::regclass);
62
+
63
+ --
64
+ -- Data for Name: shop_b01; Type: TABLE DATA; Schema: public; Owner: seller
65
+ --
66
+
67
+ COPY shop_b01 (id, office_id, file_path) FROM stdin;
68
+ 1 5 012ABCD /ab1/20181111010101/JAPAN SHOPSKU01/X0.d/01
69
+ 2 6 012XYZA /ab2/20181111010101/NIHON SHOPSKU01/X9.d/02
70
+ \\.
71
+
72
+ --
73
+ -- Name: shop_b01_id_seq; Type: SEQUENCE SET; Schema: public; Owner: seller
74
+ --
75
+
76
+ SELECT pg_catalog.setval('shop_b01_id_seq', 24, true);
77
+
78
+ --
79
+ -- Name: shop_b01 shop_b01_pkey; Type: CONSTRAINT; Schema: public; Owner: seller
80
+ --
81
+
82
+ ALTER TABLE ONLY shop_b01
83
+ ADD CONSTRAINT shop_b01_pkey PRIMARY KEY (id);
84
+ ALTER TABLE ONLY shop_b01
85
+ ADD CONSTRAINT shop_b01_qkey PRIMARY KEY (id, tax_id, office_id);
86
+
87
+ --
88
+ -- Name: shop_b01 shop_b01unique; Type: CONSTRAINT; Schema: public; Owner: seller
89
+ --
90
+
91
+ ALTER TABLE ONLY shop_b01
92
+ ADD CONSTRAINT shop_b01unique UNIQUE (office_id, file_path);
93
+
94
+ --
95
+ -- Name: shop_b01 tg01; Type: TRIGGER; Schema: public; Owner: seller
96
+ --
97
+
98
+ CREATE TRIGGER tg01 BEFORE INSERT OR UPDATE ON public.shop_b01 FOR EACH ROW EXECUTE PROCEDURE tg_ins_upd_trriger();
99
+
100
+ EOF
101
+ end
102
+
103
+ def teardown
104
+ @tmpioin.close
105
+ end
106
+
107
+
108
+ def test_initialize01
109
+ s1 = SS.new(@tmpioin.path)
110
+
111
+ assert ( /_rails_db\.sql$/ =~ s1.outfile )
112
+ #print "DEBUG:"; p s1.instance_eval{ @strall }
113
+ assert ( /^ALTER/ =~ s1.instance_eval{ @strall } )
114
+ assert_equal 'mapping.csv', s1.mappingcsv
115
+ end
116
+
117
+ def test_get_foreign_keys01
118
+ s1 = SS.new(@tmpioin.path)
119
+ strin = 'FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)'
120
+ (fkeys, strout) = s1.get_foreign_keys(strin, 'this_t')
121
+ assert_equal 'this_t', fkeys[0].child[:tbl]
122
+ assert_equal 'b', fkeys[0].child[:col]
123
+ assert_equal 'c', fkeys[1].child[:col]
124
+ assert_equal 'other_table', fkeys[0].parent[:tbl]
125
+ assert_equal 'c1', fkeys[0].parent[:col]
126
+ assert_equal 'other_table', fkeys[1].parent[:tbl]
127
+ assert_equal 'c2', fkeys[1].parent[:col]
128
+ assert_equal strin.sub(/_table/, '_tables'), strout
129
+
130
+ (fkeys, strout) = s1.get_foreign_keys('naiyo', 'this_t')
131
+ assert_nil fkeys
132
+ assert_equal 'naiyo', strout
133
+ end
134
+
135
+ def test_get_foreign_keys02
136
+ s1 = SS.new(@tmpioin.path)
137
+ strin = ' REFERENCES products (product_no),'
138
+ (fkeys, strout) = s1.get_foreign_keys(strin, 'this_t', 'col_old3')
139
+ assert_equal 1, fkeys.size
140
+ assert_equal 'this_t', fkeys[0].child[:tbl]
141
+ assert_equal 'col_old3', fkeys[0].child[:col]
142
+ assert_equal 'products', fkeys[0].parent[:tbl]
143
+ assert_equal 'product_no', fkeys[0].parent[:col]
144
+ assert_equal strin, strout
145
+ end
146
+
147
+ def test_get_hsflag_in_read01
148
+ s1 = SS.new(@tmpioin.path)
149
+ hs = {
150
+ :in_create => nil,
151
+ :in_comment => false,
152
+ :in_sentence => false,
153
+ :from_stdin => false,
154
+ :tbl_cur => nil,
155
+ }
156
+ assert_equal hs, s1.get_hsflag_in_read()
157
+ s='tbl8'
158
+ hs2 = hs.merge({:in_create => s})
159
+ assert_equal s, hs2[:in_create]
160
+ assert_equal hs.merge({:in_create => s}), s1.get_hsflag_in_read(in_create: s)
161
+
162
+ assert_equal true, s1.get_hsflag_in_read("abc", in_sentence: true)[:in_sentence]
163
+ assert_equal false, s1.get_hsflag_in_read("abc; \n", in_sentence: true)[:in_sentence]
164
+ assert_equal true, s1.get_hsflag_in_read("a\\; \n", in_sentence: true)[:in_sentence]
165
+ assert_equal false, s1.get_hsflag_in_read("\\\\; ", in_sentence: true)[:in_sentence]
166
+ end
167
+
168
+ def test_read01
169
+ s1 = SS.new(@tmpioin.path)
170
+ assert_match(/^\s*ADD CONSTRAINT/ , s1.instance_eval{ @strall })
171
+ assert( /office_id_cli/ !~ s1.instance_eval{ @strall })
172
+ assert s1.col_index.empty?
173
+ # open('/tmp/a.sql', 'w'){|iow| iow.print File.read(@tmpioin.path)} ## DEBUG
174
+
175
+ str_refac = s1.read(stage: :refactoring, setstr: false)
176
+ assert( /^\s*ADD CONSTRAINT/ !~ str_refac)
177
+ assert( /office_id_cli/ !~ str_refac)
178
+ assert s1.col_index.empty?, "debug: #{s1.col_index.inspect}"
179
+ # open('/tmp/b.sql', 'w'){|iow| iow.print str_refac} ## DEBUG
180
+
181
+ assert_raises(DbSuitRailsError){ s1.read(stage: :indexing) } # b/c refactoring is not made.
182
+ str_indexed = s1.read(stage: :indexing, instr: str_refac)
183
+ assert_equal "id_cli", s1.col_index.newcolval("shop_b01", "id")
184
+ assert_equal "office_id_cli", s1.col_index.newcolval("shop_b01", "office_id")
185
+ assert( /^\s*ADD CONSTRAINT/ !~ str_indexed)
186
+ assert( /id_cli/ !~ str_indexed)
187
+ assert( s1.instance_eval{@strall} != str_indexed)
188
+ # open('/tmp/c.sql', 'w'){|iow| iow.print str_indexed} ## DEBUG
189
+
190
+ ## Now, redoing (with setstr option is true implicitly) so the instance variable @strall is set.
191
+ s1.read(stage: :refactoring)
192
+ s1.read(stage: :indexing)
193
+ assert_equal s1.instance_eval{@strall}, str_indexed
194
+
195
+ str_final = s1.read(stage: :final, setstr: false)
196
+ assert_match(/id_cli/, str_final)
197
+ # open('/tmp/d.sql', 'w'){|iow| iow.print str_final} ## DEBUG
198
+
199
+ assert s1.tbl_index.tblmaps.key?('shop_b01')
200
+ assert_equal s1.tbl_index.tblmaps['shop_b01'], 'shop_b01s'
201
+ assert_equal 3, s1.tbl_index.tblmaps.keys.size, sprintf("Table keys = %s", s1.tbl_index.tblmaps.keys.inspect)
202
+
203
+ str_tmp = str_final.split("\n").grep(/ALTER TABLE/).join("\n")
204
+ assert_match(/^-- ALTER TABLE .+ PRIMARY KEY \(id_cli\)/, str_tmp)
205
+ assert_match( /^ALTER TABLE .+ UNIQUE \(office_id_cli, file_path\)/, str_tmp)
206
+
207
+ # Tests the "*_id" column name acceptable as it is.
208
+ assert_match( /^ALTER TABLE .+ UNIQUE \(id_cli, tax_id, office_id_cli\)/, str_tmp)
209
+
210
+ hst = { 'shop_b01' => 'shop_b01s',
211
+ "tax_table"=>"tax_tables",
212
+ 'shop_b01_id_seq' => 'shop_b01s_id_seq', }
213
+ # 'shop_b01_pkey' => 'shop_b01s_pkey',
214
+ # 'shop_b01unique' => 'shop_b01sunique', }
215
+ assert_equal hst, s1.tbl_index.tblmaps
216
+
217
+ fk_tax = SqlSkelton::Fkey.new("shop_b01", 'tax_id', "tax_table", "vat_id")
218
+ hsc = { 'shop_b01' =>
219
+ { :order=>%w(id tax_id office_id file_path),
220
+ "id" =>{:name=>"id_cli", :fkey=>nil},
221
+ "tax_id" =>{:name=>"tax_id", :fkey=>fk_tax}, # due to Foreign key, _id is preserved.
222
+ "office_id"=>{:name=>"office_id_cli", :fkey=>nil},
223
+ "file_path"=>{:name=>"file_path", :fkey=>nil},
224
+ },
225
+ "tax_table" =>
226
+ { :order=>%w(vat_id),
227
+ "vat_id" =>{:name=>"vat_id_cli", :fkey=>nil},
228
+ } }
229
+ assert_equal hsc.keys.sort, s1.col_index.colmaps.keys.sort
230
+ assert_equal hsc, s1.col_index.colmaps
231
+ # id integer NOT NULL,
232
+ #office_id character varying(50) NOT NULL,
233
+ #file_path character varying(300) NOT NULL,
234
+
235
+ end
236
+
237
+ def test_run01
238
+ s1 = SS.new(@tmpioin.path, '/tmp/out.sql', mappingcsv: '/tmp/mapping.csv')
239
+ s1.run()
240
+ end
241
+ end # class TestUnitSqlSkelton < MiniTest::Test
242
+
243
+ #end # if $0 == __FILE__
244
+
245
+
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db_suit_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ platform: ruby
6
+ authors:
7
+ - Masa Sakano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
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
+ description: Database conversion software to make it suit Ruby-on-Rails.
28
+ email:
29
+ executables:
30
+ - mk_sqlskelton
31
+ - copy_nline
32
+ extensions: []
33
+ extra_rdoc_files:
34
+ - README.en.rdoc
35
+ files:
36
+ - ".gitignore"
37
+ - ChangeLog
38
+ - Makefile
39
+ - README.en.rdoc
40
+ - Rakefile
41
+ - bin/copy_nline
42
+ - bin/mk_sqlskelton
43
+ - db_suit_rails.gemspec
44
+ - lib/db_suit_rails/db_suit_rails_error.rb
45
+ - lib/db_suit_rails/sql_skelton.rb
46
+ - lib/db_suit_rails/sql_skelton/col_index.rb
47
+ - lib/db_suit_rails/sql_skelton/fkey.rb
48
+ - lib/db_suit_rails/sql_skelton/tbl_index.rb
49
+ - lib/db_suit_rails/sql_skelton/utils.rb
50
+ - test/sql_skelton/test_col_index.rb
51
+ - test/sql_skelton/test_fkey.rb
52
+ - test/sql_skelton/test_tbl_index.rb
53
+ - test/test_sql_skelton.rb
54
+ homepage: https://www.wisebabel.com
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options:
60
+ - "--charset=UTF-8"
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '2.0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.7.3
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Database conversion to suit Ruby-on-Rails
79
+ test_files:
80
+ - test/sql_skelton/test_col_index.rb
81
+ - test/sql_skelton/test_fkey.rb
82
+ - test/sql_skelton/test_tbl_index.rb
83
+ - test/test_sql_skelton.rb