sql_migrations 2.1.1 → 2.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc164b4988352478c077939a9ed77564a15d52eb
4
- data.tar.gz: 7c8dd94fd00064b7888154aa374568131813d3e9
3
+ metadata.gz: d7eefab432a38d45308f4a3e59cc8516151a6ff7
4
+ data.tar.gz: acb7edb97f69f2b6d7447306bb024cacb024765f
5
5
  SHA512:
6
- metadata.gz: 43bfdee3da5cdd107dd72cd2eef9ef57a65a87abcb76c04b4735915b3ade80dae99e92880294eed5ac6c20a6abddab8c195085b0a44e03763ffed3517b8063e0
7
- data.tar.gz: 55bf6583e6875822d9bec006c6027001f111d235621210715a837607e515db9cea4de462828f918231c312f16412cfc021c346a518b2b5c877d56a1549d01604
6
+ metadata.gz: 0c83d6b1ee4783fd09d935be49c812323d331b0023a148a42eda184b9d85afc714aee7f047075c70a51c806e4c1af202df393addf96c7a5da7c4b269ab69b826
7
+ data.tar.gz: c134ae9d84e65f66d8bbe07273a63531bb2c94c3a8bab4def1ca678325a4461e28a77e4d6d2df8afe24c0fd1f37c9ef9b2b2a4241a3c71f204afcd9c491e7841
@@ -2,12 +2,12 @@ module SqlMigrations
2
2
  # Fixture class
3
3
  #
4
4
  class Fixture < SqlScript
5
- def self.find(db_name)
6
- super(db_name, :fixture)
5
+ def self.find(database_name)
6
+ super(database_name, :fixture)
7
7
  end
8
8
 
9
9
  def to_s
10
- "Fixture #{@name} for `#{@db_name}` database, datetime: #{@date + @time}"
10
+ "Fixture #{@name} for `#{@database_name}` database, datetime: #{@date + @time}"
11
11
  end
12
12
  end
13
13
  end
@@ -2,12 +2,12 @@ module SqlMigrations
2
2
  # Migration class
3
3
  #
4
4
  class Migration < SqlScript
5
- def self.find(db_name)
6
- super(db_name, :migration)
5
+ def self.find(database_name)
6
+ super(database_name, :migration)
7
7
  end
8
8
 
9
9
  def to_s
10
- "Migration #{@name} for `#{@db_name}` database, datetime: #{@date + @time}"
10
+ "Migration #{@name} for `#{@database_name}` database, datetime: #{@date + @time}"
11
11
  end
12
12
  end
13
13
  end
@@ -0,0 +1,59 @@
1
+ module SqlMigrations
2
+ # Class representing script file
3
+ #
4
+ class ScriptFile
5
+ attr_reader :name, :time, :date, :database_name, :path
6
+
7
+ def initialize(path, database_name, type)
8
+ @filename,
9
+ @base_directory,
10
+ @parent_directory = path_elements(path)
11
+ @path = path
12
+ @type = type.to_s
13
+ @database_name = database_name
14
+ @date, @time, @name = match_regexp(@filename) if @filename
15
+ end
16
+
17
+ def content
18
+ File.read(@path)
19
+ end
20
+
21
+ def valid?
22
+ (@name && @time && @date && @database_name && matches_directories?) ? true : false
23
+ end
24
+
25
+ private
26
+
27
+ def path_elements(path)
28
+ _filename, _base_directory, _parent_directory =
29
+ path.split(File::SEPARATOR).last(3).reverse
30
+ end
31
+
32
+ def match_regexp(filename)
33
+ _, date, time, name =
34
+ filename.match(/(\d{8})_(\d{6})_(.*)?\.sql/).to_a
35
+ [date, time, name]
36
+ end
37
+
38
+ def matches_directories?
39
+ if @database_name == :default
40
+ file_in_type_base_directory? || file_in_database_directory?
41
+ else
42
+ file_in_database_directory?
43
+ end
44
+ end
45
+
46
+ def file_in_type_base_directory?
47
+ @base_directory == "#{@type}s"
48
+ end
49
+
50
+ def file_in_type_parent_directory?
51
+ @parent_directory == "#{@type}s"
52
+ end
53
+
54
+ def file_in_database_directory?
55
+ file_in_type_parent_directory? &&
56
+ (@base_directory == @database_name.to_s)
57
+ end
58
+ end
59
+ end
@@ -2,12 +2,12 @@ module SqlMigrations
2
2
  # Seed class
3
3
  #
4
4
  class Seed < SqlScript
5
- def self.find(db_name)
6
- super(db_name, :seed)
5
+ def self.find(database_name)
6
+ super(database_name, :seed)
7
7
  end
8
8
 
9
9
  def to_s
10
- "Seed data #{@name} for `#{@db_name}` database, datetime: #{@date + @time}"
10
+ "Seed data #{@name} for `#{@database_name}` database, datetime: #{@date + @time}"
11
11
  end
12
12
  end
13
13
  end
@@ -1,17 +1,10 @@
1
1
  module SqlMigrations
2
2
  # SqlScript class
3
3
  #
4
- class SqlScript
5
- attr_reader :date, :time, :name
6
-
7
- def initialize(content, opts)
8
- @content = content
9
- @date = opts[:date]
10
- @time = opts[:time]
11
- @name = opts[:name]
12
- @db_name = opts[:db_name]
13
- @type = self.class.name.downcase.split('::').last
14
- @datetime = (@date + @time).to_i
4
+ class SqlScript < ScriptFile
5
+ def initialize(path, database_name, type)
6
+ super
7
+ @datetime = (@date.to_s + @time.to_s).to_i
15
8
  end
16
9
 
17
10
  def execute(db)
@@ -35,22 +28,20 @@ module SqlMigrations
35
28
  def statements
36
29
  separator = Config.options[:separator]
37
30
  if separator
38
- statements = @content.split(separator)
31
+ statements = content.split(separator)
39
32
  statements.collect!(&:strip)
40
33
  statements.reject(&:empty?)
41
34
  else
42
- [@content]
35
+ [content]
43
36
  end
44
37
  end
45
38
 
46
- def self.find(db_name, type)
47
- files = FindScripts.find(db_name, type)
48
- scripts = files.map do |parameters|
49
- path = parameters.delete(:path)
50
- content = File.read(path)
51
- new(content, parameters)
39
+ def self.find(database_name, type)
40
+ scripts = []
41
+ Find.find(Dir.pwd) do |path|
42
+ candidate = new(path, database_name, type)
43
+ scripts << candidate if candidate.valid?
52
44
  end
53
-
54
45
  scripts.sort_by { |file| (file.date + file.time).to_i }
55
46
  end
56
47
 
@@ -1,5 +1,5 @@
1
1
  # SqlMigrations
2
2
  #
3
3
  module SqlMigrations
4
- VERSION = '2.1.1'
4
+ VERSION = '2.2.1'
5
5
  end
@@ -8,7 +8,7 @@ require 'erb'
8
8
  require 'sql_migrations/version'
9
9
  require 'sql_migrations/database'
10
10
  require 'sql_migrations/config'
11
- require 'sql_migrations/find_scripts'
11
+ require 'sql_migrations/script_file'
12
12
  require 'sql_migrations/sql_script'
13
13
  require 'sql_migrations/migration'
14
14
  require 'sql_migrations/seed'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sql_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grzegorz Bizon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-18 00:00:00.000000000 Z
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -140,9 +140,9 @@ files:
140
140
  - lib/sql_migrations.rb
141
141
  - lib/sql_migrations/config.rb
142
142
  - lib/sql_migrations/database.rb
143
- - lib/sql_migrations/find_scripts.rb
144
143
  - lib/sql_migrations/fixture.rb
145
144
  - lib/sql_migrations/migration.rb
145
+ - lib/sql_migrations/script_file.rb
146
146
  - lib/sql_migrations/seed.rb
147
147
  - lib/sql_migrations/sql_script.rb
148
148
  - lib/sql_migrations/tasks/list.rake
@@ -1,66 +0,0 @@
1
- module SqlMigrations
2
- # Class for finding sql scripts
3
- #
4
- class FindScripts
5
- class << self
6
- def find(db_name, type)
7
- files = []
8
- Find.find(Dir.pwd) do |path|
9
- file_parameters = match_file(db_name, type, path)
10
- next unless file_parameters
11
- file_parameters.merge!(path: path)
12
- files << file_parameters
13
- end
14
- files
15
- end
16
-
17
- private
18
-
19
- def match_file(db_dir, type, path)
20
- # parent_dir/base_dir/filename_that_matches_regexp.sql
21
- _filename, base_dir, parent_dir = path.split(File::SEPARATOR).last(3).reverse
22
-
23
- # Only files that match agains regexp will do
24
- file_parameters = file_match_regexp(File.basename(path))
25
- return nil unless file_parameters
26
-
27
- # Only files that lay in specific directory structure will do
28
- file_database = file_match_directories(parent_dir, base_dir, db_dir, "#{type}s")
29
- return nil unless file_database
30
-
31
- { date: file_parameters[1], time: file_parameters[2],
32
- name: file_parameters[3], db_name: file_database }
33
- end
34
-
35
- def file_match_regexp(file_name)
36
- file_name.match(/(\d{8})_(\d{6})_(.*)?\.sql/)
37
- end
38
-
39
- def file_match_directories(parent_dir, base_dir, db_dir, type)
40
- # example: migrations/migration_file.sql
41
- file_in_type_base_dir = (base_dir == type.to_s)
42
- # example: migrations/some_database/migration_file.sql
43
- file_in_type_parent_dir = (parent_dir == type.to_s)
44
- # example migrations/some_database/migration_file.sql
45
- file_in_db_dir = (base_dir == db_dir.to_s)
46
-
47
- file_in_valid_db_dir = (file_in_type_parent_dir && file_in_db_dir)
48
-
49
- # If we are matching script assigned to :default database
50
- # scripts can be placed in base_dir or in parent dir indicating type
51
- # and in :default base_dir
52
- if db_dir == :default
53
- matches = file_in_type_base_dir || file_in_valid_db_dir
54
- file_database = :default
55
- else
56
- # Otherwise only files for specific type dir (migration/fixture/seed)
57
- # and specific database dir apply
58
- matches = file_in_valid_db_dir
59
- file_database = base_dir.to_sym
60
- end
61
-
62
- matches ? file_database : nil
63
- end
64
- end
65
- end
66
- end