liquigen 0.0.3 → 0.0.4

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.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -0
  3. data/Gemfile.lock +1 -1
  4. data/README.md +160 -7
  5. data/bin/liquigen +96 -12
  6. data/lib/liquigen.rb +16 -0
  7. data/lib/liquigen/add_column.rb +14 -0
  8. data/lib/liquigen/change_set.rb +15 -4
  9. data/lib/liquigen/create_index.rb +15 -0
  10. data/lib/liquigen/create_table.rb +1 -1
  11. data/lib/liquigen/drop_table.rb +11 -0
  12. data/lib/liquigen/handlers/add_column.rb +26 -0
  13. data/lib/liquigen/handlers/add_index.rb +34 -0
  14. data/lib/liquigen/handlers/base.rb +20 -4
  15. data/lib/liquigen/handlers/change_type.rb +29 -0
  16. data/lib/liquigen/handlers/create_table.rb +10 -3
  17. data/lib/liquigen/handlers/drop_table.rb +26 -0
  18. data/lib/liquigen/handlers/rename_column.rb +31 -0
  19. data/lib/liquigen/handlers/rename_table.rb +29 -0
  20. data/lib/liquigen/handlers/sql.rb +26 -0
  21. data/lib/liquigen/modify_data_type.rb +15 -0
  22. data/lib/liquigen/rename_column.rb +17 -0
  23. data/lib/liquigen/rename_table.rb +13 -0
  24. data/lib/liquigen/sql.rb +11 -0
  25. data/lib/liquigen/version.rb +1 -1
  26. data/spec/liquigen/change_set_spec.rb +10 -0
  27. data/spec/liquigen/handlers/add_column_spec.rb +18 -0
  28. data/spec/liquigen/handlers/add_index_spec.rb +18 -0
  29. data/spec/liquigen/handlers/change_type_spec.rb +18 -0
  30. data/spec/liquigen/{handler_spec.rb → handlers/create_table_spec.rb} +40 -4
  31. data/spec/liquigen/handlers/drop_table_spec.rb +18 -0
  32. data/spec/liquigen/handlers/rename_column_spec.rb +18 -0
  33. data/spec/liquigen/handlers/rename_table_spec.rb +18 -0
  34. data/spec/liquigen/handlers/sql_spec.rb +18 -0
  35. metadata +25 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c2773ea35aa9555f6b68b5ef319621381b3f0305f1ca0df85d9e7fbd42e623c
4
- data.tar.gz: 1c4f07d4ff42bf55e59ef731884a399cc2c7801c9aa5c101754c459729c2ccf8
3
+ metadata.gz: 828dd9e9045d6e0c612b17f2919d0d23573d3ccade3d7a665bde8abe6b889302
4
+ data.tar.gz: 362285b1394e2f66155d596a72330de7a7e62cbacf8fa2bc08096c16da1b5277
5
5
  SHA512:
6
- metadata.gz: 2dc5eb7267abb2ce4ce5bdff6b1676c39651b8e1c47006b982b14859833c241780b4577b9af0081e851c05a3e32e7967573ba19fa0cd1ca230bae4b2bfebfae4
7
- data.tar.gz: e295be52897d817da9df062d3909d02af3feb4a8c74a2f874043cfc5af469764f4450e5ce7de48e9c527a5ef4c38c23c5cd506c8cbcf661d3a25e80e50da1424
6
+ metadata.gz: f1db59837bafb0cc1d511ddb086ebee6a09f944d2f99fce5ab12199cfb8c2f51c966710c28c774d2aee85a0d0b5236c8b069e9b83081db836e06f426d29578a5
7
+ data.tar.gz: 2e9352ce87d00eb6cd02690859dfbe1c2a5b52c2944219c3b04aa8ee0b0109099dbc7abbf20aa83ee9570cc6cd33cf76efcdefe1d0a908957b7d97859dad51e4
data/.rubocop.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  AllCops:
2
+ TargetRubyVersion: 2.5.1
2
3
  Exclude:
3
4
  - 'tmp/**/*'
4
5
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- liquigen (0.0.2)
4
+ liquigen (0.0.3)
5
5
  activesupport
6
6
  gli (= 2.18.0)
7
7
 
data/README.md CHANGED
@@ -4,7 +4,9 @@ Comparing with ruby, java sucks.
4
4
 
5
5
  It is a simple tool for java liquibase script generation.
6
6
 
7
- Only support mysql db and create_table currently. And it is not a perfect tool, only help to create basic content with simple name. Please change the generated content manually.
7
+ Only support mysql db. And it is not a perfect tool, can help to create basic content with simple name. Please change the generated content manually if the generation was wrong.
8
+
9
+ [Liquibase documentation](http://www.liquibase.org/documentation/changes/).
8
10
 
9
11
  ## how to use
10
12
  1. gem install liquigen
@@ -37,25 +39,176 @@ databaseChangeLog:
37
39
  name: id
38
40
  type: bigint
39
41
  constraints:
40
- - constraint:
41
42
  primaryKey: true
42
43
  autoIncrement: true
43
44
  - column:
44
45
  name: name
45
46
  type: varchar(255)
46
47
  constraints:
47
- - constraint:
48
48
  nullable: false
49
49
 
50
50
  ```
51
51
 
52
- ## modify_table
52
+ ## rename_table
53
+ ```bash
54
+ liquigen rename_table users:s_users customers:s_customers
55
+ ```
56
+
57
+ Result file:
58
+ ```yaml
59
+ databaseChangeLog:
60
+ - changeSet:
61
+ id: 20181121150900_RenameTableUsersAndCustomer
62
+ author: Jeremy
63
+ changes:
64
+ - renameTable:
65
+ oldTableName: users
66
+ newTableName: s_users
67
+ - renameTable:
68
+ oldTableName: customers
69
+ newTableName: s_customers
70
+
71
+ ```
72
+
73
+ ## drop_table
74
+ ```bash
75
+ liquigen drop_table user customer
76
+ ```
77
+
78
+ ## add_column
79
+ ```bash
80
+ liquigen add_column -t users name:string email:string
81
+ ```
82
+
83
+ Result:
84
+ ```yaml
85
+ databaseChangeLog:
86
+ - changeSet:
87
+ id: 20181121155512_AddColumn_User_NameAndEmail
88
+ author: Jeremy Cui
89
+ changes:
90
+ - addColumn:
91
+ tableName: users
92
+ columns:
93
+ - column:
94
+ name: name
95
+ type: varchar(255)
96
+ constraints:
97
+ nullable: false
98
+ - column:
99
+ name: email
100
+ type: varchar(255)
101
+ constraints:
102
+ nullable: false
103
+
104
+ ```
105
+
106
+ ## rename_column
107
+ ```bash
108
+ liquigen rename_column user:status:status_ok:string customer:status:status_ok:string
109
+ ```
110
+
111
+ Result:
112
+ ```yaml
113
+ databaseChangeLog:
114
+ - changeSet:
115
+ id: 20181121162306_RenameColumn_UserStatusToStatusOkAndCustomerStatusToStatusOk
116
+ author: Jeremy Cui
117
+ changes:
118
+ - renameColumn:
119
+ tableName: users
120
+ oldColumnName: status
121
+ newColumnName: status_ok
122
+ columnDataType: varchar(255)
123
+ - renameColumn:
124
+ tableName: customers
125
+ oldColumnName: status
126
+ newColumnName: status_ok
127
+ columnDataType: varchar(255)
128
+
129
+ ```
130
+
131
+ ## change_type
132
+ ```bash
133
+ liquigen change_type user:id:integer customer:name:string
134
+ ```
135
+
136
+ Result:
137
+ ```yaml
138
+ databaseChangeLog:
139
+ - changeSet:
140
+ id: '20181121145051'
141
+ author: Jeremy
142
+ changes:
143
+ - modifyDataType:
144
+ tableName: users
145
+ columnName: id
146
+ newDateType: bigint
147
+ - modifyDataType:
148
+ tableName: customers
149
+ columnName: name
150
+ newDateType: varchar(255)
151
+
152
+ ```
153
+
154
+ ## create_index
155
+ ```bash
156
+ liquigen add_index -t user name:string email:string
157
+ ```
158
+
159
+ ## sql
160
+ ```bash
161
+ liquigen sql "update users set column=true" "update customers set x = 1"
162
+ ```
53
163
 
54
- ToDo.
164
+ ## How to let the liquibase use migration directory
55
165
 
56
- ## drop table
166
+ * In your java project, open application.yml, add the following lines:
167
+ ```yaml
168
+ spring:
169
+ liquibase:
170
+ change-log: classpath:/db/changelog-master.yaml
171
+ ```
172
+ * Edit your changelog-master.yaml file.
173
+ ```yaml
174
+ databaseChangeLog:
175
+ - includeAll:
176
+ path: "db/migrations/"
177
+ ```
178
+
179
+ That's it.
180
+ * Liquigen will create the directory automatically.
181
+
182
+
183
+ ## How to use the latest source in your java project
184
+
185
+ 1. Add .ruby-version file to your project root path.
186
+
187
+ ```ruby
188
+ ruby-2.5.1
189
+
190
+ ```
191
+ You can also use the other version.
192
+
193
+ 2. Add Gemfile file as well.
194
+ ```ruby
195
+ source 'https://rubygems.org'
196
+
197
+ gem 'liquigen', :git => 'https://github.com/jerecui/liquigen'
198
+ ```
199
+ 3. Bundle install
200
+
201
+ ```sh
202
+ bundle install
203
+
204
+ ```
205
+ 4. cd your project in command line, type liquigen xxxxx
206
+
207
+ ```sh
208
+ # say
209
+ liquigen add_table -t user id:integer name:string
210
+ ```
57
211
 
58
- ToDo.
59
212
 
60
213
  ## Contributing
61
214
  Bug reports and pull requests are welcome on GitHub at https://github.com/jerecui/liquigen.
data/bin/liquigen CHANGED
@@ -22,31 +22,115 @@ class App
22
22
 
23
23
  desc 'add table script'
24
24
  arg_name 'Describe arguments to add_table here'
25
+ long_desc %{
26
+ Example
27
+ liquigen add_table -t user id:integer name:string
28
+ }
25
29
  command :add_table do |c|
26
30
  c.desc 'table name'
27
31
  c.default_value 'table'
28
32
  c.flag :t
29
33
 
30
34
  c.action do |_, options, props|
31
- table = options[:t].underscore.singularize
32
- handler = Liquigen::Handlers::CreateTable.new table, props
35
+ handler = Liquigen::Handlers::CreateTable.new options[:t], props
33
36
  handler.process
34
37
  end
35
38
  end
36
39
 
37
- desc 'Describe modify_table here'
38
- arg_name 'Describe arguments to modify_table here'
39
- command :modify_table do |c|
40
- c.action do |global_options, options,args|
41
- puts 'waiting for the implementation'
40
+ desc 'rename tables'
41
+ long_desc %{
42
+ If you want to change the table users to s_users, customers to s_customers,
43
+ Use the following command:
44
+ liquigen rename_table user:s_users customer:s_customers
45
+ }
46
+ arg_name 'old_table_name:new_table_name'
47
+ command :rename_table do |c|
48
+ c.action do |_, _, args|
49
+ handler = Liquigen::Handlers::RenameTable.new args
50
+ handler.process
42
51
  end
43
52
  end
44
53
 
45
- desc 'Describe drop_table here'
46
- arg_name 'Describe arguments to drop_table here'
54
+ desc 'drop tables'
55
+ long_desc %{
56
+ example:
57
+ liquigen drop_table user customer
58
+ }
59
+ arg_name 'table1 table2 ...'
47
60
  command :drop_table do |c|
48
- c.action do |global_options, options,args|
49
- puts 'waiting for the implementation'
61
+ c.action do |_, _, args|
62
+ handler = Liquigen::Handlers::DropTable.new args
63
+ handler.process
64
+ end
65
+ end
66
+
67
+ desc 'add columns script'
68
+ arg_name 'Describe arguments to add_column here'
69
+ long_desc %{
70
+ Example
71
+ liquigen add_column -t user id:integer name:string
72
+ }
73
+ command :add_column do |c|
74
+ c.desc 'table name'
75
+ c.default_value 'table'
76
+ c.flag :t
77
+
78
+ c.action do |_, options, props|
79
+ handler = Liquigen::Handlers::AddColumn.new options[:t], props
80
+ handler.process
81
+ end
82
+ end
83
+
84
+ desc 'rename columns script'
85
+ arg_name 'Describe arguments to rename_column here'
86
+ long_desc %{
87
+ Example
88
+ liquigen rename_column user:status:status_ok:string customer:status:status_ok:string
89
+ }
90
+ command :rename_column do |c|
91
+ c.action do |_, _, props|
92
+ handler = Liquigen::Handlers::RenameColumn.new props
93
+ handler.process
94
+ end
95
+ end
96
+
97
+ desc 'change type'
98
+ long_desc %{
99
+ If you want to change the table column type,
100
+ Use the following command:
101
+ liquigen change_type user:name:string
102
+ }
103
+ arg_name 'table:column_name:new_type'
104
+ command :change_type do |c|
105
+ c.action do |_, _, args|
106
+ handler = Liquigen::Handlers::ChangeType.new args
107
+ handler.process
108
+ end
109
+ end
110
+
111
+ desc 'Describe sql here'
112
+ arg_name 'Describe arguments to raw sql here'
113
+ command :sql do |c|
114
+ c.action do |_, _, args|
115
+ handler = Liquigen::Handlers::Sql.new args
116
+ handler.process
117
+ end
118
+ end
119
+
120
+ desc 'add index script'
121
+ arg_name 'Describe arguments to add_index here'
122
+ long_desc %{
123
+ Example
124
+ liquigen add_index -t user id:integer name:string
125
+ }
126
+ command :add_index do |c|
127
+ c.desc 'table name'
128
+ c.default_value 'table'
129
+ c.flag :t
130
+
131
+ c.action do |_, options, props|
132
+ handler = Liquigen::Handlers::AddIndex.new options[:t], props
133
+ handler.process
50
134
  end
51
135
  end
52
136
 
@@ -68,7 +152,7 @@ class App
68
152
  on_error do |err|
69
153
  if debugging?
70
154
  STDERR.puts err.message
71
- STDERR.puts err.backtrace.join( "\n" )
155
+ STDERR.puts err.backtrace.join("\n")
72
156
  else
73
157
  next if GLI::CustomExit === err
74
158
 
data/lib/liquigen.rb CHANGED
@@ -4,12 +4,28 @@ require 'active_support'
4
4
  require 'active_support/inflector'
5
5
 
6
6
  require 'liquigen/create_table'
7
+ require 'liquigen/add_column'
8
+ require 'liquigen/rename_table'
9
+ require 'liquigen/modify_data_type'
10
+ require 'liquigen/rename_column'
11
+ require 'liquigen/sql'
12
+ require 'liquigen/create_index'
13
+ require 'liquigen/drop_table'
14
+
7
15
  require 'liquigen/change_set'
8
16
  require 'liquigen/change'
9
17
  require 'liquigen/column'
10
18
  require 'liquigen/constraint'
19
+
11
20
  require 'liquigen/handlers/base'
12
21
  require 'liquigen/handlers/create_table'
22
+ require 'liquigen/handlers/add_column'
23
+ require 'liquigen/handlers/rename_table'
24
+ require 'liquigen/handlers/change_type'
25
+ require 'liquigen/handlers/rename_column'
26
+ require 'liquigen/handlers/sql'
27
+ require 'liquigen/handlers/add_index'
28
+ require 'liquigen/handlers/drop_table'
13
29
 
14
30
  # Add requires for other files you add to your project here, so
15
31
  # you just need to require this one file in your bin file
@@ -0,0 +1,14 @@
1
+ require 'liquigen/change'
2
+
3
+ module Liquigen
4
+ class AddColumn < Change
5
+ attr_accessor :table_name
6
+
7
+ attr_accessor :columns
8
+
9
+ def initialize(table_name)
10
+ self.table_name = table_name&.underscore&.pluralize
11
+ self.columns = []
12
+ end
13
+ end
14
+ end
@@ -4,11 +4,22 @@ module Liquigen
4
4
  attr_accessor :author
5
5
  attr_accessor :changes
6
6
 
7
- def initialize
8
- self.id = Time.new.strftime('%Y%m%d%H%M%S')
9
- # todo Get the current git config name
10
- self.author = 'Jeremy'
7
+ def initialize(file_name)
8
+ self.id = file_name
9
+ self.author = "#{git_user} <#{git_email}>"
11
10
  self.changes = []
12
11
  end
12
+
13
+ def git_user
14
+ author = ''
15
+ IO.popen('git config -l | grep user.name') { |x| author = x.gets }
16
+ author = author.gsub(/user.name=(\w*)/, '\1').strip || 'yourname'
17
+ end
18
+
19
+ def git_email
20
+ email = []
21
+ IO.popen('git config -l | grep user.email') { |x| email = x.gets }
22
+ email = email.gsub(/user.email=(\w*)/, '\1').strip
23
+ end
13
24
  end
14
25
  end
@@ -0,0 +1,15 @@
1
+ require 'liquigen/change'
2
+
3
+ module Liquigen
4
+ class CreateIndex < Change
5
+ attr_accessor :table_name
6
+ attr_accessor :index_name
7
+ attr_accessor :columns
8
+
9
+ def initialize(table_name)
10
+ self.table_name = table_name&.underscore&.pluralize
11
+ self.index_name = ''
12
+ self.columns = []
13
+ end
14
+ end
15
+ end
@@ -7,7 +7,7 @@ module Liquigen
7
7
  attr_accessor :columns
8
8
 
9
9
  def initialize(table_name)
10
- self.table_name = table_name.pluralize
10
+ self.table_name = table_name&.underscore&.pluralize
11
11
  self.columns = []
12
12
  end
13
13
  end
@@ -0,0 +1,11 @@
1
+ require 'liquigen/change'
2
+
3
+ module Liquigen
4
+ class DropTable < Change
5
+ attr_accessor :table_name
6
+
7
+ def initialize(table)
8
+ self.table_name = table.underscore.pluralize
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,26 @@
1
+ require 'liquigen/handlers/base'
2
+
3
+ module Liquigen::Handlers
4
+ class AddColumn < CreateTable
5
+ def action_name
6
+ 'AddColumn'
7
+ end
8
+
9
+ # Add Column
10
+ def file_suffix
11
+ table.capitalize
12
+
13
+ columns = props.map do |item|
14
+ item.split(':')[0].camelize
15
+ end.uniq.join('And')
16
+
17
+ "#{table.capitalize}_#{columns}"
18
+ end
19
+
20
+ def build_one_changeset(set)
21
+ change = Liquigen::AddColumn.new(table)
22
+ set.changes << change
23
+ props.each { |kv| change.columns << build_column(kv) }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,34 @@
1
+ require 'liquigen/handlers/base'
2
+
3
+ module Liquigen::Handlers
4
+ class AddIndex < CreateTable
5
+ def action_name
6
+ 'AddIndex'
7
+ end
8
+
9
+ # Add Column
10
+ def file_suffix
11
+ table.capitalize
12
+
13
+ columns = props.map do |item|
14
+ item.split(':')[0].camelize
15
+ end.uniq.join('And')
16
+
17
+ "On#{table.capitalize}_#{columns}"
18
+ end
19
+
20
+ def build_one_changeset(set)
21
+ change = Liquigen::CreateIndex.new(table)
22
+ set.changes << change
23
+ props.each { |kv| change.columns << build_column(kv) }
24
+
25
+ column_names = props.map { |x| x.split(':')[0] }.uniq.join('_')
26
+
27
+ change.index_name = "idx_#{table.underscore.pluralize}_#{column_names}"
28
+ end
29
+
30
+ def constraints?
31
+ false
32
+ end
33
+ end
34
+ end
@@ -12,14 +12,20 @@ module Liquigen::Handlers
12
12
  attr_accessor :props
13
13
  attr_accessor :sets
14
14
 
15
+ attr_accessor :id
16
+
15
17
  def initialize(table, props)
16
18
  self.table = table
17
19
  self.props = props
18
20
  self.sets = []
21
+
22
+ self.id = build_id
19
23
  end
20
24
 
21
25
  def process
22
- build_change_sets
26
+ set = Liquigen::ChangeSet.new(id)
27
+ build_one_changeset(set)
28
+ sets << set
23
29
 
24
30
  file_path = build_file_name
25
31
  File.open(file_path, 'w+') { |f| f.write(sets.to_yaml(indentation: 4)) }
@@ -33,12 +39,20 @@ module Liquigen::Handlers
33
39
  'liquigen'
34
40
  end
35
41
 
42
+ def file_suffix
43
+ table.capitalize
44
+ end
45
+
46
+ def build_id
47
+ "#{Time.new.strftime('%Y%m%d%H%M%S')}_#{action_name}_#{file_suffix}"
48
+ end
49
+
36
50
  def build_file_name
37
51
  dir = 'src/main/resources/db/migrations'
38
52
 
39
53
  FileUtils.mkdir_p(dir)
40
54
 
41
- "#{dir}/#{Time.new.strftime('%Y%m%d%H%M%S')}_#{action_name}#{table.capitalize}.yaml"
55
+ "#{dir}/#{id}.yaml"
42
56
  end
43
57
 
44
58
  def process_lines(file_path)
@@ -80,7 +94,7 @@ module Liquigen::Handlers
80
94
  next_blank = lines[index + 1].gsub(/^(\s+).*$/, '\1')
81
95
 
82
96
  valid = false
83
- valid = true if lines[index + 1].include?('-')
97
+ valid = true if lines[index + 1].include?('-') && current_blank.size == next_blank.size
84
98
  valid = true if current_blank.size < next_blank.size
85
99
 
86
100
  empty_marks << index unless valid
@@ -91,7 +105,9 @@ module Liquigen::Handlers
91
105
 
92
106
  def camelize_words(lines)
93
107
  lines.map do |line|
94
- line.gsub(/[\w^:]+/) { |x| x.camelize(:lower) }
108
+ left = line.gsub(/^(.*?):.*$/, '\1')
109
+ right = line.gsub(/^.*?(:.*)$/, '\1')
110
+ left.gsub(/[\w^:]+/) { |x| x.camelize(:lower) } + right
95
111
  end
96
112
  end
97
113
  end
@@ -0,0 +1,29 @@
1
+ require 'liquigen/handlers/base'
2
+
3
+ module Liquigen::Handlers
4
+ class ChangeType < Base
5
+ def initialize(props)
6
+ self.props = props
7
+ self.sets = []
8
+ self.id = build_id
9
+ end
10
+
11
+ def action_name
12
+ 'ChangeType'
13
+ end
14
+
15
+ def file_suffix
16
+ props.map do |item|
17
+ item.split(':')[0].camelize
18
+ end.uniq.join('And')
19
+ end
20
+
21
+ def build_one_changeset(set)
22
+ props.each do |kv|
23
+ kv = kv.split(':')
24
+ change = Liquigen::ModifyDataType.new(kv[0], kv[1], kv[2])
25
+ set.changes << change
26
+ end
27
+ end
28
+ end
29
+ end
@@ -6,18 +6,21 @@ module Liquigen::Handlers
6
6
  'CreateTable'
7
7
  end
8
8
 
9
- def build_change_sets
10
- set = Liquigen::ChangeSet.new
9
+ def build_one_changeset(set)
11
10
  change = Liquigen::CreateTable.new(table)
12
11
  set.changes << change
13
12
  props.each { |kv| change.columns << build_column(kv) }
14
- sets << set
15
13
  end
16
14
 
17
15
  def build_column(name_and_type)
18
16
  kv = name_and_type.split(':')
19
17
  column = Liquigen::Column.new(name: kv[0], type: kv[1])
20
18
 
19
+ unless constraints?
20
+ column.constraints = nil
21
+ return column
22
+ end
23
+
21
24
  if column.name == 'id'
22
25
  column.auto_increment = true
23
26
  column.constraints.primary_key = true
@@ -27,5 +30,9 @@ module Liquigen::Handlers
27
30
 
28
31
  column
29
32
  end
33
+
34
+ def constraints?
35
+ true
36
+ end
30
37
  end
31
38
  end
@@ -0,0 +1,26 @@
1
+ require 'liquigen/handlers/base'
2
+
3
+ module Liquigen::Handlers
4
+ class DropTable < Base
5
+ def initialize(props)
6
+ self.props = props
7
+ self.sets = []
8
+ self.id = build_id
9
+ end
10
+
11
+ def action_name
12
+ 'DropTable'
13
+ end
14
+
15
+ def file_suffix
16
+ props.map(&:camelize).join('And')
17
+ end
18
+
19
+ def build_one_changeset(set)
20
+ props.each do |tbl|
21
+ change = Liquigen::DropTable.new tbl
22
+ set.changes << change
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ require 'liquigen/handlers/base'
2
+
3
+ module Liquigen::Handlers
4
+ class RenameColumn < Base
5
+ def initialize(props)
6
+ self.props = props
7
+ self.sets = []
8
+ self.id = build_id
9
+ end
10
+
11
+ def action_name
12
+ 'RenameColumn'
13
+ end
14
+
15
+ def file_suffix
16
+ props.map do |item|
17
+ arr = item.split(':')
18
+ column_description = Array[arr[1].camelize, arr[2].camelize].join('To')
19
+ "#{arr[0].camelize}#{column_description}"
20
+ end.join('And')
21
+ end
22
+
23
+ def build_one_changeset(set)
24
+ props.each do |kv|
25
+ kv = kv.split(':')
26
+ change = Liquigen::RenameColumn.new(kv[0], kv[1], kv[2], kv[3])
27
+ set.changes << change
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ require 'liquigen/handlers/base'
2
+
3
+ module Liquigen::Handlers
4
+ class RenameTable < Base
5
+ def initialize(props)
6
+ self.props = props
7
+ self.sets = []
8
+ self.id = build_id
9
+ end
10
+
11
+ def action_name
12
+ 'RenameTable'
13
+ end
14
+
15
+ def file_suffix
16
+ props.map do |item|
17
+ item.split(':')[0].camelize
18
+ end.join('And')
19
+ end
20
+
21
+ def build_one_changeset(set)
22
+ props.each do |kv|
23
+ kv = kv.split(':')
24
+ change = Liquigen::RenameTable.new(kv[0], kv[1])
25
+ set.changes << change
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ require 'liquigen/handlers/base'
2
+
3
+ module Liquigen::Handlers
4
+ class Sql < Base
5
+ def initialize(props)
6
+ self.props = props
7
+ self.sets = []
8
+ self.id = build_id
9
+ end
10
+
11
+ def action_name
12
+ 'Sql'
13
+ end
14
+
15
+ def file_suffix
16
+ "With#{props.size}Clauses"
17
+ end
18
+
19
+ def build_one_changeset(set)
20
+ props.each do |sql|
21
+ change = Liquigen::Sql.new sql
22
+ set.changes << change
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ require 'liquigen/change'
2
+
3
+ module Liquigen
4
+ class ModifyDataType < Change
5
+ attr_accessor :table_name
6
+ attr_accessor :column_name
7
+ attr_accessor :new_date_type
8
+
9
+ def initialize(table, from, to)
10
+ self.table_name = table&.underscore&.pluralize
11
+ self.column_name = from
12
+ self.new_date_type = TypeMap.new(to).db_type || to
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ require 'liquigen/change'
2
+
3
+ module Liquigen
4
+ class RenameColumn < Change
5
+ attr_accessor :table_name
6
+ attr_accessor :old_column_name
7
+ attr_accessor :new_column_name
8
+ attr_accessor :column_data_type
9
+
10
+ def initialize(table, old_name, new_name, type)
11
+ self.table_name = table&.underscore&.pluralize
12
+ self.old_column_name = old_name
13
+ self.new_column_name = new_name
14
+ self.column_data_type = TypeMap.new(type).db_type || type
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ require 'liquigen/change'
2
+
3
+ module Liquigen
4
+ class RenameTable < Change
5
+ attr_accessor :old_table_name
6
+ attr_accessor :new_table_name
7
+
8
+ def initialize(old_name, new_name)
9
+ self.old_table_name = old_name&.underscore&.pluralize
10
+ self.new_table_name = new_name&.underscore&.pluralize
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require 'liquigen/change'
2
+
3
+ module Liquigen
4
+ class Sql < Change
5
+ attr_accessor :sql
6
+
7
+ def initialize(sql)
8
+ self.sql = sql
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Liquigen
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Liquigen::ChangeSet, type: :model do
4
+ let(:change_set) { described_class.new('test') }
5
+
6
+ describe '#current git user' do
7
+ let(:author) { 'Jeremy Cui' }
8
+ specify { expect(change_set.author).to eq author }
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Liquigen::Handlers::AddColumn, type: :model do
4
+ let(:props) { ['name:string', 'email:string'] }
5
+ let(:handler) { described_class.new 'user', props }
6
+
7
+ describe '#action_name' do
8
+ specify { expect(handler.action_name).to eq 'AddColumn' }
9
+ end
10
+
11
+ describe '#file_suffix' do
12
+ specify { expect(handler.file_suffix).to eq 'User_NameAndEmail' }
13
+ end
14
+
15
+ describe '#id' do
16
+ specify { expect(handler.id).not_to eq '' }
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Liquigen::Handlers::AddIndex, type: :model do
4
+ let(:props) { ['name:string', 'email:string'] }
5
+ let(:handler) { described_class.new 'user', props }
6
+
7
+ describe '#action_name' do
8
+ specify { expect(handler.action_name).to eq 'AddIndex' }
9
+ end
10
+
11
+ describe '#file_suffix' do
12
+ specify { expect(handler.file_suffix).to eq 'OnUser_NameAndEmail' }
13
+ end
14
+
15
+ describe '#id' do
16
+ specify { expect(handler.id).not_to eq '' }
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Liquigen::Handlers::ChangeType, type: :model do
4
+ let(:props) { ['user:id:string', 'customer:name:text'] }
5
+ let(:handler) { described_class.new props }
6
+
7
+ describe '#action_name' do
8
+ specify { expect(handler.action_name).to eq 'ChangeType' }
9
+ end
10
+
11
+ describe '#file_suffix' do
12
+ specify { expect(handler.file_suffix).to eq 'UserAndCustomer' }
13
+ end
14
+
15
+ describe '#id' do
16
+ specify { expect(handler.id).not_to eq '' }
17
+ end
18
+ end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe Liquigen::Handlers::Base, type: :model do
3
+ RSpec.describe Liquigen::Handlers::CreateTable, type: :model do
4
4
  let(:handler) { described_class.new 'User', 'id: integer' }
5
5
 
6
6
  let(:source) {
@@ -18,7 +18,6 @@ RSpec.describe Liquigen::Handlers::Base, type: :model do
18
18
  constraints:
19
19
  primary_key:
20
20
  nullable: false
21
-
22
21
  '
23
22
  }
24
23
  describe '#remove_empty' do
@@ -28,7 +27,28 @@ RSpec.describe Liquigen::Handlers::Base, type: :model do
28
27
  it 'should remove the lines which contains empty value' do
29
28
  ret = subject
30
29
 
31
- expect(ret.size).to eq 14
30
+ expect(ret.size).to eq 12
31
+ end
32
+
33
+ describe '#remove_empty_othercase' do
34
+ let(:source) {
35
+ 'databaseChangeLog:
36
+ - changeSet:
37
+ author: Jeremy Cui
38
+ changes:
39
+ - createIndex:
40
+ columns:
41
+ - column:
42
+ name: name
43
+ type: varchar(255)
44
+ constraints:
45
+ - column:
46
+ type: varchar(255)
47
+ constraints:
48
+ '
49
+ }
50
+
51
+ specify { expect(subject.join.include?('constraints')).to be_falsy }
32
52
  end
33
53
  end
34
54
 
@@ -38,7 +58,10 @@ RSpec.describe Liquigen::Handlers::Base, type: :model do
38
58
  id: 201811031330
39
59
  name: Jeremy
40
60
  changes:
41
- - CreateTable
61
+ - RenameTable:
62
+ old_table_name: users
63
+ new_table_name: s_users
64
+ - CreateTable:
42
65
  table_name: user
43
66
 
44
67
  '
@@ -49,5 +72,18 @@ RSpec.describe Liquigen::Handlers::Base, type: :model do
49
72
  ret = handler.send(:camelize_words, lines)
50
73
  expect(ret[ret.size - 1]).to eq ' tableName: user'
51
74
  end
75
+
76
+ describe 'Should only process the key part' do
77
+ let(:source) { ' new_table_name: s_users'}
78
+
79
+ it 'should only process the key part' do
80
+ ret = handler.send(:camelize_words, lines)
81
+ expect(ret[ret.size - 1]).to eq ' newTableName: s_users'
82
+ end
83
+ end
84
+ end
85
+
86
+ describe '#id' do
87
+ specify { expect(handler.id).not_to eq '' }
52
88
  end
53
89
  end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Liquigen::Handlers::DropTable, type: :model do
4
+ let(:props) { %w[user customer] }
5
+ let(:handler) { described_class.new props }
6
+
7
+ describe '#action_name' do
8
+ specify { expect(handler.action_name).to eq 'DropTable' }
9
+ end
10
+
11
+ describe '#file_suffix' do
12
+ specify { expect(handler.file_suffix).to eq 'UserAndCustomer' }
13
+ end
14
+
15
+ describe '#id' do
16
+ specify { expect(handler.id).not_to eq '' }
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Liquigen::Handlers::RenameColumn, type: :model do
4
+ let(:props) { ['user:status:status_ok:string', 'customer:status:status_ok:string'] }
5
+ let(:handler) { described_class.new props }
6
+
7
+ describe '#action_name' do
8
+ specify { expect(handler.action_name).to eq 'RenameColumn' }
9
+ end
10
+
11
+ describe '#file_suffix' do
12
+ specify { expect(handler.file_suffix).to eq 'UserStatusToStatusOkAndCustomerStatusToStatusOk' }
13
+ end
14
+
15
+ describe '#id' do
16
+ specify { expect(handler.id).not_to eq '' }
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Liquigen::Handlers::RenameTable, type: :model do
4
+ let(:props) { ['user:s_users', 'customer:s_customers'] }
5
+ let(:handler) { described_class.new props }
6
+
7
+ describe '#action_name' do
8
+ specify { expect(handler.action_name).to eq 'RenameTable' }
9
+ end
10
+
11
+ describe '#file_suffix' do
12
+ specify { expect(handler.file_suffix).to eq 'UserAndCustomer' }
13
+ end
14
+
15
+ describe '#id' do
16
+ specify { expect(handler.id).not_to eq '' }
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Liquigen::Handlers::Sql, type: :model do
4
+ let(:props) { ['update users set x = 1', 'update customers set x = 2'] }
5
+ let(:handler) { described_class.new props }
6
+
7
+ describe '#action_name' do
8
+ specify { expect(handler.action_name).to eq 'Sql' }
9
+ end
10
+
11
+ describe '#file_suffix' do
12
+ specify { expect(handler.file_suffix).to eq 'With2Clauses' }
13
+ end
14
+
15
+ describe '#id' do
16
+ specify { expect(handler.id).not_to eq '' }
17
+ end
18
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liquigen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Cui
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-20 00:00:00.000000000 Z
11
+ date: 2018-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -113,18 +113,40 @@ files:
113
113
  - features/step_definitions/liquigen_steps.rb
114
114
  - features/support/env.rb
115
115
  - lib/liquigen.rb
116
+ - lib/liquigen/add_column.rb
116
117
  - lib/liquigen/change.rb
117
118
  - lib/liquigen/change_set.rb
118
119
  - lib/liquigen/column.rb
119
120
  - lib/liquigen/constraint.rb
121
+ - lib/liquigen/create_index.rb
120
122
  - lib/liquigen/create_table.rb
123
+ - lib/liquigen/drop_table.rb
124
+ - lib/liquigen/handlers/add_column.rb
125
+ - lib/liquigen/handlers/add_index.rb
121
126
  - lib/liquigen/handlers/base.rb
127
+ - lib/liquigen/handlers/change_type.rb
122
128
  - lib/liquigen/handlers/create_table.rb
129
+ - lib/liquigen/handlers/drop_table.rb
130
+ - lib/liquigen/handlers/rename_column.rb
131
+ - lib/liquigen/handlers/rename_table.rb
132
+ - lib/liquigen/handlers/sql.rb
133
+ - lib/liquigen/modify_data_type.rb
134
+ - lib/liquigen/rename_column.rb
135
+ - lib/liquigen/rename_table.rb
136
+ - lib/liquigen/sql.rb
123
137
  - lib/liquigen/type_map.rb
124
138
  - lib/liquigen/version.rb
125
139
  - liquigen.gemspec
126
140
  - liquigen.rdoc
127
- - spec/liquigen/handler_spec.rb
141
+ - spec/liquigen/change_set_spec.rb
142
+ - spec/liquigen/handlers/add_column_spec.rb
143
+ - spec/liquigen/handlers/add_index_spec.rb
144
+ - spec/liquigen/handlers/change_type_spec.rb
145
+ - spec/liquigen/handlers/create_table_spec.rb
146
+ - spec/liquigen/handlers/drop_table_spec.rb
147
+ - spec/liquigen/handlers/rename_column_spec.rb
148
+ - spec/liquigen/handlers/rename_table_spec.rb
149
+ - spec/liquigen/handlers/sql_spec.rb
128
150
  - spec/spec_helper.rb
129
151
  homepage: http://github.com/jerecui/liquigen
130
152
  licenses: []