Module: Souls::Utils
- Included in:
- Souls
- Defined in:
- lib/souls/utils/index.rb
Instance Method Summary collapse
- #check_schema(class_name: "user") ⇒ Object
- #get_api_path ⇒ Object
- #get_col_name_and_type(class_name: "user", file_path: "db/migrate/20210816094410_add_column_to_users.rb", action: "add") ⇒ Object
- #get_columns_num(class_name: "user") ⇒ Object
- #get_columns_num_no_timestamp(class_name: "user") ⇒ Object
- #get_create_migration_type(class_name: "user") ⇒ Object
- #get_latest_version_txt(service_name: "api") ⇒ Object
- #get_migration_type(class_name: "user", action: "add") ⇒ Object
- #get_mother_path ⇒ Object
- #get_relation_params(class_name: "user", col: "") ⇒ Object
- #get_tables ⇒ Object
- #get_test_type(type) ⇒ Object
- #get_type(type) ⇒ Object
- #get_type_and_name(line) ⇒ Object
- #rbs_type_check(type) ⇒ Object
- #table_check(line: "", class_name: "") ⇒ Object
- #type_check(type) ⇒ Object
- #version_detector(current_ver: [0, 0, 1], update_kind: "patch") ⇒ Object
Instance Method Details
#check_schema(class_name: "user") ⇒ Object
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/souls/utils/index.rb', line 232 def check_schema(class_name: "user") schema_data = get_columns_num(class_name: class_name) create_migration_data = get_create_migration_type(class_name: class_name) add_migration_data = get_migration_type(class_name: class_name, action: "add") remove_migration_data = get_migration_type(class_name: class_name, action: "remove") migration_data = create_migration_data + add_migration_data - remove_migration_data return "Already Up to date!" if schema_data.size == migration_data.size schema_data - migration_data end |
#get_api_path ⇒ Object
7 8 9 |
# File 'lib/souls/utils/index.rb', line 7 def get_api_path Dir.pwd.split(Souls.configuration.app)[0] + Souls.configuration.app + "/apps/api" end |
#get_col_name_and_type(class_name: "user", file_path: "db/migrate/20210816094410_add_column_to_users.rb", action: "add") ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/souls/utils/index.rb', line 209 def get_col_name_and_type( class_name: "user", file_path: "db/migrate/20210816094410_add_column_to_users.rb", action: "add" ) pluralized_class_name = class_name.pluralize response = [] File.open(file_path) do |line| line.each_line do |file_line| next unless file_line.include?("#{action}_column") array = file_line.include?("array: true") types = file_line.split(",").map(&:strip) types.map { |n| n.gsub!(":", "") } types[0].gsub!("#{action}_column ", "") unless types[0].to_s == pluralized_class_name raise(StandardError, "Wrong class_name!Please Check your migration file!") end response << { column_name: types[1], type: types[2], array: array } end end response end |
#get_columns_num(class_name: "user") ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/souls/utils/index.rb', line 132 def get_columns_num(class_name: "user") pluralized_class_name = class_name.pluralize file_path = "./db/schema.rb" class_check_flag = false cols = [] File.open(file_path, "r") do |f| f.each_line.with_index do |line, _i| class_check_flag = true if line.include?("create_table") && line.include?(pluralized_class_name) if class_check_flag == true && !line.include?("create_table") return cols if line.include?("t.index") || line.strip == "end" types = Souls.get_type_and_name(line) array = line.include?("array: true") cols << { column_name: types[1], type: types[0], array: array } end end end cols end |
#get_columns_num_no_timestamp(class_name: "user") ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/souls/utils/index.rb', line 152 def (class_name: "user") pluralized_class_name = class_name.pluralize file_path = "./db/schema.rb" class_check_flag = false cols = [] File.open(file_path, "r") do |f| f.each_line.with_index do |line, _i| class_check_flag = true if line.include?("create_table") && line.include?(pluralized_class_name) if class_check_flag == true && !line.include?("create_table") return cols if line.include?("t.index") || line.strip == "end" types = Souls.get_type_and_name(line) array = line.include?("array: true") cols << { column_name: types[1], type: types[0], array: array } unless %w[ created_at updated_at ].include?(types[1]) end end end cols end |
#get_create_migration_type(class_name: "user") ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/souls/utils/index.rb', line 175 def get_create_migration_type(class_name: "user") pluralized_class_name = class_name.pluralize file_path = Dir["db/migrate/*_create_#{pluralized_class_name}.rb"][0] class_check_flag = false response = [ { column_name: "created_at", type: "datetime", array: false }, { column_name: "updated_at", type: "datetime", array: false } ] File.open(file_path) do |f| f.each_line do |line| class_check_flag = true if line.include?("create_table") next unless class_check_flag == true && !line.include?("create_table") return response if line.include?("t.timestamps") || line.strip == "end" types = Souls.get_type_and_name(line) types.map { |n| n.gsub!(":", "") } array = line.include?("array: true") response << { column_name: types[1], type: types[0], array: array } end end end |
#get_latest_version_txt(service_name: "api") ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/souls/utils/index.rb', line 100 def get_latest_version_txt(service_name: "api") case service_name when "gem" return Souls::VERSION.split(".").map(&:to_i) when "api", "worker", "console", "admin", "media" file_path = "#{Gem.dir}/gems/souls-#{Souls::VERSION}/lib/souls/versions/.souls_#{service_name}_version" else raise(StandardError, "You are at wrong directory!") end File.open(file_path, "r") do |f| f.readlines[0].strip.split(".").map(&:to_i) end end |
#get_migration_type(class_name: "user", action: "add") ⇒ Object
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/souls/utils/index.rb', line 198 def get_migration_type(class_name: "user", action: "add") pluralized_class_name = class_name.pluralize file_paths = Dir["db/migrate/*_#{action}_column_to_#{pluralized_class_name}.rb"] new_columns = file_paths.map do |file_path| get_col_name_and_type(class_name: class_name, file_path: file_path, action: action) end new_columns.flatten end |
#get_mother_path ⇒ Object
3 4 5 |
# File 'lib/souls/utils/index.rb', line 3 def get_mother_path Dir.pwd.split(Souls.configuration.app)[0] + Souls.configuration.app end |
#get_relation_params(class_name: "user", col: "") ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/souls/utils/index.rb', line 114 def get_relation_params(class_name: "user", col: "") Dir.chdir(Souls.get_api_path.to_s) do cols = if col == "mutation" (class_name: class_name) else get_columns_num(class_name: class_name) end relation_params = cols.select { |n| n[:column_name].match?(/_id$/) } user_check = relation_params.map do |param| param[:column_name] == "user_id" end user_exist = user_check.include?(true) return { user_exist: user_exist, params: cols, relation_params: relation_params } end end |
#get_tables ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/souls/utils/index.rb', line 67 def get_tables path = "./db/schema.rb" tables = [] File.open(path, "r") do |f| f.each_line.with_index do |line, _i| tables << line.split("\"")[1] if line.include?("create_table") end end tables end |
#get_test_type(type) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/souls/utils/index.rb', line 54 def get_test_type(type) { bigint: "rand(1..10)", float: 4.2, string: '"MyString"', text: '"MyString"', datetime: "Time.now", date: "Time.now.strftime('%F')", boolean: false, integer: "rand(1..10)" }[type.to_sym] end |
#get_type(type) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/souls/utils/index.rb', line 41 def get_type(type) { bigint: "Integer", string: "String", float: "Float", text: "String", datetime: "GraphQL::Types::ISO8601DateTime", date: "GraphQL::Types::ISO8601DateTime", boolean: "Boolean", integer: "Integer" }[type.to_sym] end |
#get_type_and_name(line) ⇒ Object
37 38 39 |
# File 'lib/souls/utils/index.rb', line 37 def get_type_and_name(line) line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0] end |
#rbs_type_check(type) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/souls/utils/index.rb', line 24 def rbs_type_check(type) { bigint: "Integer", string: "String", float: "Float", text: "String", datetime: "String", date: "String", boolean: "bool", integer: "Integer" }[type.to_sym] end |
#table_check(line: "", class_name: "") ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/souls/utils/index.rb', line 78 def table_check(line: "", class_name: "") if line.include?("create_table") && (line.split[1].gsub("\"", "").gsub(",", "") == class_name.pluralize.to_s) return true end false end |
#type_check(type) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/souls/utils/index.rb', line 11 def type_check(type) { bigint: "Integer", string: "String", float: "Float", text: "String", datetime: "String", date: "String", boolean: "Boolean", integer: "Integer" }[type.to_sym] end |
#version_detector(current_ver: [0, 0, 1], update_kind: "patch") ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/souls/utils/index.rb', line 87 def version_detector(current_ver: [0, 0, 1], update_kind: "patch") case update_kind when "patch" "#{current_ver[0]}.#{current_ver[1]}.#{current_ver[2] + 1}" when "minor" "#{current_ver[0]}.#{current_ver[1] + 1}.0" when "major" "#{current_ver[0] + 1}.0.0" else raise(StandardError, "Wrong version!") end end |