volt-sql 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a834868fd701ac0a4f7b4b35a54e1bcd2f543a2
4
- data.tar.gz: 3d5efb82730d25ab1ebd6e13e6176f456e4f3c4e
3
+ metadata.gz: e7e548ec4a0ab1a1d1a6c20e2f49b1ee265cd9f5
4
+ data.tar.gz: a608aef5539926f5519b8e490c4a03107c002ad8
5
5
  SHA512:
6
- metadata.gz: bdea5818bf692546f6e79531e463ab7f9d1ed7cac9024704b083c7cc1de7b2d389dd7d2ea1ec09e70fa9a260bc317e84e340b389d3850516c54839f7b465e1b3
7
- data.tar.gz: ab2e2d872e64e381fac7a3cf5848cfbe02444bb2a08efc8f3bf497e014973a9b096729526e265971129e6c4ad0448ced0dae758e9a3f87dc00964489a1052bad
6
+ metadata.gz: aa240c8a9145fc364d7c3ca8aeecd2366363156d509cd4a46bf6bbdef848d6b1c8c09c225151918bc13693fbc8bc2eb1ed14d2ce359a2ad88ae2a48639900523
7
+ data.tar.gz: 360549f4778a1efb9a3dd3116ff7c01a7e4470ae36cbeef20ef160018322f3b460989b64555e989d9a301946e951e4e967a153886e16002ff48eb62200934ba2
@@ -37,7 +37,7 @@ module Volt
37
37
  sequel_default = sequel_opts.delete(:default)
38
38
 
39
39
  if db_default != sequel_default
40
- up_code << "set_column_default #{table_name.inspect}, #{column_name.inspect}, #{sequel_default.inspect}"
40
+ up_code << "if column_exists?(#{table_name.inspect}, #{column_name.inspect})\n set_column_default #{table_name.inspect}, #{column_name.inspect}, #{sequel_default.inspect}\nend"
41
41
  down_code << "set_column_default #{table_name.inspect}, #{column_name.inspect}, #{db_default.inspect}"
42
42
  end
43
43
 
@@ -49,10 +49,10 @@ module Volt
49
49
  if db_null != sequel_null
50
50
  # allow null changed
51
51
  if sequel_null
52
- up_code << "set_column_allow_null #{table_name.inspect}, #{column_name.inspect}"
52
+ up_code << "if column_exists?(#{table_name.inspect}, #{column_name.inspect})\n set_column_allow_null #{table_name.inspect}, #{column_name.inspect}\nend"
53
53
  down_code << "set_column_not_null #{table_name.inspect}, #{column_name.inspect}"
54
54
  else
55
- up_code << "set_column_not_null #{table_name.inspect}, #{column_name.inspect}"
55
+ up_code << "if column_exists?(#{table_name.inspect}, #{column_name.inspect})\n set_column_not_null #{table_name.inspect}, #{column_name.inspect}\nend"
56
56
  down_code << "set_column_allow_null #{table_name.inspect}, #{column_name.inspect}"
57
57
  end
58
58
 
@@ -63,7 +63,7 @@ module Volt
63
63
 
64
64
 
65
65
  if db_class != sequel_class || db_opts != sequel_opts
66
- up_code << "set_column_type #{table_name.inspect}, #{column_name.inspect}, #{sequel_class}, #{sequel_opts.inspect}"
66
+ up_code << "if column_exists?(#{table_name.inspect}, #{column_name.inspect})\n set_column_type #{table_name.inspect}, #{column_name.inspect}, #{sequel_class}, #{sequel_opts.inspect}\nend"
67
67
  down_code << "set_column_type #{table_name.inspect}, #{column_name.inspect}, #{db_class}, #{db_opts.inspect}"
68
68
  end
69
69
 
@@ -84,7 +84,7 @@ module Volt
84
84
  log("Rename #{from_name} to #{to_name} on table #{table_name}")
85
85
 
86
86
  name = "rename_#{table_name}_#{from_name}_to_#{to_name}"
87
- up_code = "rename_column #{table_name.inspect}, #{from_name.inspect}, #{to_name.inspect}"
87
+ up_code = "if column_exists?(#{table_name.inspect}, #{from_name.inspect})\n rename_column #{table_name.inspect}, #{from_name.inspect}, #{to_name.inspect}\nend"
88
88
  down_code = "rename_column #{table_name.inspect}, #{to_name.inspect}, #{from_name.inspect}"
89
89
  generate_and_run(name, up_code, down_code)
90
90
  end
@@ -93,7 +93,7 @@ module Volt
93
93
  log("Remove #{column_name} from table #{table_name}")
94
94
 
95
95
  name = "remove_#{table_name}_#{column_name}"
96
- up_code = "drop_column #{table_name.inspect}, #{column_name.inspect}"
96
+ up_code = "if column_exists?(#{table_name.inspect}, #{column_name.inspect})\n drop_column #{table_name.inspect}, #{column_name.inspect}\nend"
97
97
 
98
98
  sequel_class, sequel_options = @table_reconcile.sequel_class_and_opts_from_db(db_field)
99
99
  down_code = "add_column #{table_name.inspect}, #{column_name.inspect}, #{sequel_class}, #{sequel_options.inspect}"
@@ -102,6 +102,7 @@ module Volt
102
102
 
103
103
  # private
104
104
  def generate_and_run(name, up_code, down_code)
105
+ puts "GAR: #{up_code.inspect}"
105
106
  path = Volt::Sql::MigrationGenerator.create_migration(name, up_code, down_code)
106
107
 
107
108
  Volt::MigrationRunner.new(@db).run_migration(path, :up)
@@ -48,5 +48,17 @@ module Volt
48
48
  set_column_default(column_name, default)
49
49
  end
50
50
  end
51
+
52
+ def table_exists?(table_name)
53
+ @db.tables && @db.tables.include?(table_name)
54
+ end
55
+
56
+ def column_exists?(table_name, column_name)
57
+ if table_exists?(table_name)
58
+ @db[table_name].columns.include?(column_name)
59
+ else
60
+ false
61
+ end
62
+ end
51
63
  end
52
64
  end
@@ -33,9 +33,9 @@ module Volt
33
33
  def self.indent_string(string, count)
34
34
  string.split("\n").map.with_index do |line,index|
35
35
  if index == 0
36
- string
36
+ line
37
37
  else
38
- (' ' * count) + string
38
+ (' ' * count) + line
39
39
  end
40
40
  end.join("\n")
41
41
  end
@@ -3,6 +3,17 @@
3
3
 
4
4
  module Volt
5
5
  class MigrationRunner
6
+ alias_method :__run__, :run
7
+
8
+ def run(direction=:up, until_version=nil)
9
+ # reconcile after run in production
10
+ # if Volt.env.production?
11
+ # Volt.current_app.database.reconcile!
12
+ # end
13
+
14
+ __run__(direction, until_version)
15
+ end
16
+
6
17
  def raw_db
7
18
  @raw_db ||= Volt.current_app.database.raw_db
8
19
  end
@@ -22,12 +22,19 @@ module Volt
22
22
  # reconcile takes the database from its current state to the state defined
23
23
  # in the model classes with the field helper
24
24
  def reconcile!
25
+ table_reconciles = []
26
+ Volt::RootModels.model_classes.each do |model_class|
27
+ table_reconciles << TableReconcile.new(@adaptor, @db, model_class)
28
+ end
29
+
30
+ # Make any missing tables
31
+ table_reconciles.each(&:ensure_table)
32
+
25
33
  # Make sure the migrations are up to date first.
26
34
  Volt::MigrationRunner.new(@db).run
27
35
 
28
- Volt::RootModels.model_classes.each do |model_class|
29
- TableReconcile.new(@adaptor, @db, model_class).run
30
- end
36
+ # After the migrations, reconcile tables
37
+ table_reconciles.each(&:run)
31
38
 
32
39
  # After the initial reconcile!, we add a listener for any new models
33
40
  # created, so we can reconcile them (in specs mostly)
@@ -9,20 +9,17 @@ module Volt
9
9
  class TableReconcile
10
10
  include SqlLogger
11
11
 
12
- attr_reader :field_updater
12
+ attr_reader :table_name, :field_updater
13
13
 
14
14
  def initialize(adaptor, db, model_class)
15
15
  @model_class = model_class
16
16
  @adaptor = adaptor
17
17
  @db = db
18
- @field_updater = FieldUpdater.new(@db, self)
18
+ @table_name = @model_class.collection_name
19
+ @field_updater ||= FieldUpdater.new(@db, self)
19
20
  end
20
21
 
21
22
  def run
22
- table_name = @model_class.collection_name
23
-
24
- ensure_table(table_name)
25
-
26
23
  update_fields(@model_class, table_name)
27
24
 
28
25
  IndexUpdater.new(@db, @model_class, table_name)
@@ -31,7 +28,7 @@ module Volt
31
28
  end
32
29
 
33
30
  # Create an empty table if one does not exist
34
- def ensure_table(table_name)
31
+ def ensure_table
35
32
  # Check if table exists
36
33
  if !@db.tables || !@db.tables.include?(table_name)
37
34
  log("Creating Table #{table_name}")
@@ -1,5 +1,5 @@
1
1
  module Volt
2
2
  module Sql
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
5
5
  end
@@ -108,7 +108,7 @@ describe Volt::Sql::TableReconcile do
108
108
  allow(reconcile.field_updater).to receive(:generate_and_run)
109
109
  .with(
110
110
  "column_change_sample_model3s_some_num",
111
- "set_column_type :sample_model3s, :some_num, String, {:allow_null=>true, :text=>true}",
111
+ "if column_exists?(:sample_model3s, :some_num)\n set_column_type :sample_model3s, :some_num, String, {:allow_null=>true, :text=>true}\nend",
112
112
  "set_column_type :sample_model3s, :some_num, Fixnum, {:allow_null=>true}"
113
113
  ).and_return(nil)
114
114
 
@@ -138,7 +138,7 @@ describe Volt::Sql::TableReconcile do
138
138
  expect(reconcile.field_updater).to receive(:generate_and_run)
139
139
  .with(
140
140
  "remove_sample_model4s_some_num",
141
- "drop_column :sample_model4s, :some_num",
141
+ "if column_exists?(:sample_model4s, :some_num)\n drop_column :sample_model4s, :some_num\nend",
142
142
  "add_column :sample_model4s, :some_num, Fixnum, {:allow_null=>true}"
143
143
  )
144
144
 
@@ -169,7 +169,7 @@ describe Volt::Sql::TableReconcile do
169
169
  expect(reconcile.field_updater).to receive(:generate_and_run)
170
170
  .with(
171
171
  "column_change_sample_model5s_some_num",
172
- "set_column_not_null :sample_model5s, :some_num",
172
+ "if column_exists?(:sample_model5s, :some_num)\n set_column_not_null :sample_model5s, :some_num\nend",
173
173
  "set_column_allow_null :sample_model5s, :some_num"
174
174
  )
175
175
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: volt-sql
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
  - Ryan Stout