1
# frozen_string_literal: true
2

3 4
require "yaml"
4 4
require "json"
5

6
##
7
# Extension based upon Sequel::Migration and Sequel::Migrator
8
#
9
# Adds the Sequel::Seed module and the Sequel::Seed::Base and Sequel::Seeder
10
# classes, which allow the user to easily group entity changes and seed/fixture
11
# the database to a newer version only (unlike migrations, seeds are not
12
# directional).
13
#
14
# To load the extension:
15
#
16
#   Sequel.extension :seed
17
#
18
# It is also important to set the environment:
19
#
20
#   Sequel::Seed.setup(:development)
21

22 4
module Sequel
23 4
  class << self
24
    ##
25
    # Creates a Seed subclass according to the given +block+.
26
    #
27
    # The +env_labels+ lists on which environments the seed should be applicable.
28
    # If the current environment is not applicable, the seed is ignored. On the
29
    # other hand, if it is applicable, it will be listed in Seed.descendants and
30
    # subject to application (if it was not applied yet).
31
    #
32
    # Expected seed call:
33
    #
34
    #   Sequel.seed(:test) do # seed is only applicable to the test environment
35
    #     def run
36
    #       Entity.create attribute: value
37
    #     end
38
    #   end
39
    #
40
    # Wildcard seed:
41
    #
42
    #   Sequel.seed do # seed is applicable to every environment, or no environment
43
    #     def run
44
    #       Entity.create attribute: value
45
    #     end
46
    #   end
47
    #
48

49 4
    def seed(*env_labels, &block)
50 4
      return if env_labels.length > 0 && !env_labels.map(&:to_sym).include?(Seed.environment)
51

52 4
      seed = Class.new(Seed::Base)
53 4
      seed.class_eval(&block) if block_given?
54 4
      Seed::Base.inherited(seed) unless Seed::Base.descendants.include?(seed)
55 4
      seed
56
    end
57
  end
58

59 4
  module Seed
60 4
    class Error < Sequel::Error
61
    end
62

63 4
    class << self
64 4
      attr_reader :environment
65

66
      ##
67
      # Sets the Sequel::Seed"s environment to +env+ over which the Seeds should be applied
68 4
      def setup(env, opts = {})
69 4
        @environment = env.to_sym
70 4
        @options ||= {}
71 4
        @options[:disable_warning] ||= opts[:disable_warning] || false
72
      end
73

74
      ##
75
      # Keep backward compatibility on how to setup the Sequel::Seed environment
76
      #
77
      # Sets the environment +env+ over which the Seeds should be applied
78 4
      def environment=(env)
79 4
        setup(env)
80
      end
81

82
      ##
83
      # Keep backward compatibility on how to get Sequel::Seed::Base class descendants
84 4
      def descendants
85 4
        Base.descendants
86
      end
87

88
      ##
89
      # Keep backward compatibility on how to append a Sequel::Seed::Base descendant class
90 4
      def inherited(base)
91 4
        Base.inherited(base)
92
      end
93
    end
94

95
    ##
96
    # Helper methods for the Sequel::Seed project.
97

98 4
    module Helpers
99 4
      class << self
100 4
        def camelize(term, uppercase_first_letter = true)
101 4
          string = term.to_s
102 4
          if uppercase_first_letter
103 4
            string.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
104
          else
105 0
            string.first + camelize(string)[1..-1]
106
          end
107
        end
108
      end
109
    end
110

111 4
    module SeedDescriptor
112 4
      def apply_seed_descriptor(seed_descriptor)
113 4
        case seed_descriptor
114
        when Hash
115 4
          apply_seed_hash(seed_descriptor)
116
        when Array
117 4
          seed_descriptor.each { |seed_hash| apply_seed_hash(seed_hash) }
118
        end
119
      end
120

121 4
      private
122

123 4
      def apply_seed_hash(seed_hash)
124 4
        return unless seed_hash.class <= Hash
125 4
        if seed_hash.has_key?("environment")
126 4
          case seed_hash["environment"]
127
          when String, Symbol
128 4
            return if seed_hash["environment"].to_sym != Seed.environment
129
          when Array
130 0
            return unless seed_hash["environment"].map(&:to_sym).include?(Seed.environment)
131
          end
132
        end
133

134 4
        keys = seed_hash.keys
135 4
        keys.delete("environment")
136 4
        keys.each do |key|
137 4
          key_hash = seed_hash[key]
138 4
          entries = nil
139 4
          class_name = if key_hash.has_key?("class")
140 4
            entries = key_hash["entries"]
141 4
            key_hash["class"]
142
          else
143 4
            Helpers.camelize(key)
144
          end
145
          # It will raise an error if the class name is not defined
146 4
          class_const = Kernel.const_get(class_name)
147 4
          if entries
148 4
            entries.each { |hash| create_model(class_const, hash) }
149
          else
150 4
            create_model(class_const, key_hash)
151
          end
152
        end
153
      end
154

155 4
      def create_model(class_const, hash)
156 4
        object_instance = class_const.new
157 4
        object_instance_attr = hash.each do |attr, value|
158 4
          object_instance.set({attr.to_sym => value})
159
        end
160 4
        raise(Error, "Attempt to create invalid model instance of #{class_name}") unless object_instance.valid?
161 4
        object_instance.save
162
      end
163
    end
164

165 4
    class Base
166 4
      class << self
167 4
        def apply
168 4
          new.run
169
        end
170

171 4
        def descendants
172 4
          @descendants ||= []
173
        end
174

175 4
        def inherited(base)
176 4
          descendants << base
177
        end
178
      end
179

180 4
      def run
181
      end
182
    end
183

184
    ##
185
    # Class resposible for applying all the seeds related to the current environment,
186
    # if and only if they were not previously applied.
187
    #
188
    # To apply the seeds/fixtures:
189
    #
190
    #   Sequel::Seeder.apply(db, directory)
191
    #
192
    # +db+ holds the Sequel database connection
193
    #
194
    # +directory+ the path to the seeds/fixtures files
195
  end
196

197 4
  class Seeder
198 4
    SEED_FILE_PATTERN = /\A(\d+)_.+\.(rb|json|yml|yaml)\z/i.freeze
199 4
    RUBY_SEED_FILE_PATTERN = /\A(\d+)_.+\.(rb)\z/i.freeze
200 4
    YAML_SEED_FILE_PATTERN = /\A(\d+)_.+\.(yml|yaml)\z/i.freeze
201 4
    JSON_SEED_FILE_PATTERN = /\A(\d+)_.+\.(json)\z/i.freeze
202 4
    SEED_SPLITTER = "_".freeze
203 4
    MINIMUM_TIMESTAMP = 20000101
204

205 4
    Error = Seed::Error
206

207 4
    def self.apply(db, directory, opts = {})
208 4
      seeder_class(directory).new(db, directory, opts).run
209
    end
210

211 4
    def self.seeder_class(directory)
212 4
      if self.equal?(Seeder)
213 4
        Dir.new(directory).each do |file|
214 4
          next unless SEED_FILE_PATTERN.match(file)
215 4
          return TimestampSeeder if file.split(SEED_SPLITTER, 2).first.to_i > MINIMUM_TIMESTAMP
216
        end
217 4
        raise(Error, "seeder not available for files; please check the configured seed directory \"#{directory}\". Also ensure seed files are in YYYYMMDD_seed_file.rb format.")
218
      else
219 0
        self
220
      end
221
    end
222

223 4
    attr_reader :column
224

225 4
    attr_reader :db
226

227 4
    attr_reader :directory
228

229 4
    attr_reader :ds
230

231 4
    attr_reader :files
232

233 4
    attr_reader :table
234

235 4
    def initialize(db, directory, opts = {})
236 4
      raise(Error, "Must supply a valid seed path") unless File.directory?(directory)
237 4
      @db = db
238 4
      @directory = directory
239 4
      @allow_missing_seed_files = opts[:allow_missing_seed_files]
240 4
      @files = get_seed_files
241 4
      schema, table = @db.send(:schema_and_table, opts[:table]  || self.class.const_get(:DEFAULT_SCHEMA_TABLE))
242 4
      @table = schema ? Sequel::SQL::QualifiedIdentifier.new(schema, table) : table
243 4
      @column = opts[:column] || self.class.const_get(:DEFAULT_SCHEMA_COLUMN)
244 4
      @ds = schema_dataset
245 4
      @use_transactions = opts[:use_transactions]
246
    end
247

248 4
    private
249

250 4
    def checked_transaction(seed, &block)
251 4
      use_trans = if @use_transactions.nil?
252 4
        @db.supports_transactional_ddl?
253
      else
254 0
        @use_transactions
255
      end
256

257 4
      if use_trans
258 4
        db.transaction(&block)
259
      else
260 4
        yield
261
      end
262
    end
263

264 4
    def remove_seed_classes
265 4
      Seed::Base.descendants.each do |c|
266 4
        Object.send(:remove_const, c.to_s) rescue nil
267
      end
268 4
      Seed::Base.descendants.clear
269
    end
270

271 4
    def seed_version_from_file(filename)
272 0
      filename.split(SEED_SPLITTER, 2).first.to_i
273
    end
274
  end
275

276
  ##
277
  # A Seeder subclass to apply timestamped seeds/fixtures files.
278
  # It follows the same syntax & semantics for the Seeder superclass.
279
  #
280
  # To apply the seeds/fixtures:
281
  #
282
  #   Sequel::TimestampSeeder.apply(db, directory)
283
  #
284
  # +db+ holds the Sequel database connection
285
  #
286
  # +directory+ the path to the seeds/fixtures files
287

288 4
  class TimestampSeeder < Seeder
289 4
    DEFAULT_SCHEMA_COLUMN = :filename
290 4
    DEFAULT_SCHEMA_TABLE = :schema_seeds
291

292 4
    Error = Seed::Error
293

294 4
    attr_reader :applied_seeds
295

296 4
    attr_reader :seed_tuples
297

298 4
    def initialize(db, directory, opts = {})
299 4
      super
300 4
      @applied_seeds = get_applied_seeds
301 4
      @seed_tuples = get_seed_tuples
302
    end
303

304 4
    def run
305 4
      seed_tuples.each do |s, f|
306 4
        t = Time.now
307 4
        db.log_info("Applying seed file `#{f}`")
308 4
        checked_transaction(s) do
309 4
          s.apply
310 4
          fi = f.downcase
311 4
          ds.insert(column => fi)
312
        end
313 4
        db.log_info("Seed file `#{f}` applied, it took #{sprintf("%0.6f", Time.now - t)} seconds")
314
      end
315
      nil
316
    end
317

318 4
    private
319

320 4
    def get_applied_seeds
321 4
      am = ds.select_order_map(column)
322 4
      missing_seed_files = am - files.map { |f| File.basename(f).downcase }
323 4
      if missing_seed_files.length > 0 && !@allow_missing_seed_files
324 0
        raise(Error, "Seed files not in file system: #{missing_seed_files.join(", ")}")
325
      end
326 4
      am
327
    end
328

329 4
    def get_seed_files
330 4
      files = []
331 4
      Dir.new(directory).each do |file|
332 4
        next unless SEED_FILE_PATTERN.match(file)
333 4
        files << File.join(directory, file)
334
      end
335 4
      files.sort_by { |f| SEED_FILE_PATTERN.match(File.basename(f))[1].to_i }
336
    end
337

338 4
    def get_seed_tuples
339 4
      remove_seed_classes
340 4
      seeds = []
341 4
      ms = Seed::Base.descendants
342 4
      files.each do |path|
343 4
        f = File.basename(path)
344 4
        fi = f.downcase
345 4
        if !applied_seeds.include?(fi)
346
          #begin
347 4
          load(path) if RUBY_SEED_FILE_PATTERN.match(f)
348 4
          create_yaml_seed(path) if YAML_SEED_FILE_PATTERN.match(f)
349 4
          create_json_seed(path) if JSON_SEED_FILE_PATTERN.match(f)
350
          #rescue Exception => e
351
            #raise(Error, "error while processing seed file #{path}: #{e.inspect}")
352
          #end
353 4
          el = [ms.last, f]
354 4
          next if ms.last.nil?
355 4
          if ms.last < Seed::Base && !seeds.include?(el)
356 4
            seeds << el
357
          end
358
        end
359
      end
360 4
      seeds
361
    end
362

363 4
    def create_yaml_seed(path)
364 4
      seed_descriptor = YAML::load(File.open(path))
365 4
      seed = Class.new(Seed::Base)
366 4
      seed.const_set "YAML_SEED", seed_descriptor
367 4
      seed.class_eval do
368 4
        include Seed::SeedDescriptor
369

370 4
        def run
371 4
          seed_descriptor = self.class.const_get "YAML_SEED"
372 4
          raise(Error, "YAML seed improperly defined") if seed_descriptor.nil?
373 4
          self.apply_seed_descriptor(seed_descriptor)
374
        end
375
      end
376 4
      Seed::Base.inherited(seed) unless Seed::Base.descendants.include?(seed)
377 4
      seed
378
    end
379

380 4
    def create_json_seed(path)
381 4
      seed_descriptor = JSON.parse(File.read(path))
382 4
      seed = Class.new(Seed::Base)
383 4
      seed.const_set "JSON_SEED", seed_descriptor
384 4
      seed.class_eval do
385 4
        include Seed::SeedDescriptor
386

387 4
        def run
388 4
          seed_descriptor = self.class.const_get "JSON_SEED"
389 4
          raise(Error, "JSON seed improperly defined") if seed_descriptor.nil?
390 4
          self.apply_seed_descriptor(seed_descriptor)
391
        end
392
      end
393 4
      Seed::Base.inherited(seed) unless Seed::Base.descendants.include?(seed)
394 4
      seed
395
    end
396

397 4
    def schema_dataset
398 4
      c = column
399 4
      ds = db.from(table)
400 4
      if !db.table_exists?(table)
401 4
        db.create_table(table) { String c, primary_key: true }
402 3
      elsif !ds.columns.include?(c)
403 0
        raise(Error, "Seeder table \"#{table}\" does not contain column \"#{c}\"")
404
      end
405 4
      ds
406
    end
407
  end
408
end

Read our documentation on viewing source code .

Loading