volt-sql 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/sql/lib/field_updater.rb +7 -6
- data/app/sql/lib/migration.rb +12 -0
- data/app/sql/lib/migration_generator.rb +2 -2
- data/app/sql/lib/migration_runner.rb +11 -0
- data/app/sql/lib/reconcile.rb +10 -3
- data/app/sql/lib/table_reconcile.rb +4 -7
- data/lib/volt/sql/version.rb +1 -1
- data/spec/postgres/lib/table_reconcile_spec.rb +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7e548ec4a0ab1a1d1a6c20e2f49b1ee265cd9f5
|
4
|
+
data.tar.gz: a608aef5539926f5519b8e490c4a03107c002ad8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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)
|
data/app/sql/lib/migration.rb
CHANGED
@@ -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
|
@@ -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
|
data/app/sql/lib/reconcile.rb
CHANGED
@@ -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
|
-
|
29
|
-
|
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
|
-
@
|
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
|
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}")
|
data/lib/volt/sql/version.rb
CHANGED
@@ -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
|
|