tapsoob 0.8.5-java → 0.8.6-java
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 +4 -4
- data/lib/tapsoob/schema.rb +33 -10
- data/lib/tapsoob/version.rb +1 -1
- data/spec/integration/postgres_spec.rb +12 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 507d0809e6ce3577c927ca3403a84702cd81737987535709ea1ac9d946001727
|
|
4
|
+
data.tar.gz: 42eac9c4f181ddcb29d046267715d367ba05b65cdef9c6a427eb8181b37b7190
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d4a69103da5d43b36acc62bbf186dcc0d3864eeea09ed74fba8c9cbf9807676cdac8ea3bd0bbd66a681661ae9fdc072abad07029f50d2ccb1519cc1a9679f24b
|
|
7
|
+
data.tar.gz: 191cb27f4048d18c3df96013f7c3275965e348c7a169a928b605901e6d296d6fcd0689717dcd788c9b2539ec6138380ddca33bab916f9484016280b17b5c074b
|
data/lib/tapsoob/schema.rb
CHANGED
|
@@ -11,9 +11,9 @@ module Tapsoob
|
|
|
11
11
|
extend self
|
|
12
12
|
|
|
13
13
|
def dump(database_url, options = {})
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
Sequel.connect(database_url) do |db|
|
|
15
|
+
db.extension :schema_dumper
|
|
16
|
+
template = ERB.new <<-END_MIG
|
|
17
17
|
Class.new(Sequel::Migration) do
|
|
18
18
|
def up
|
|
19
19
|
<% db.send(:sort_dumped_tables, db.tables, {}).each do |table| %>
|
|
@@ -29,7 +29,8 @@ Class.new(Sequel::Migration) do
|
|
|
29
29
|
end
|
|
30
30
|
END_MIG
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
template.result(binding)
|
|
33
|
+
end
|
|
33
34
|
end
|
|
34
35
|
|
|
35
36
|
def dump_table(database_url_or_db, table, options)
|
|
@@ -68,15 +69,17 @@ END_MIG
|
|
|
68
69
|
end
|
|
69
70
|
|
|
70
71
|
def foreign_keys(database_url)
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
Sequel.connect(database_url) do |db|
|
|
73
|
+
db.extension :schema_dumper
|
|
74
|
+
db.dump_foreign_key_migration
|
|
75
|
+
end
|
|
74
76
|
end
|
|
75
77
|
|
|
76
78
|
def indexes(database_url)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
79
|
+
Sequel.connect(database_url) do |db|
|
|
80
|
+
db.extension :schema_dumper
|
|
81
|
+
db.dump_indexes_migration
|
|
82
|
+
end
|
|
80
83
|
end
|
|
81
84
|
|
|
82
85
|
def indexes_individual(database_url)
|
|
@@ -105,6 +108,7 @@ END_MIG
|
|
|
105
108
|
end
|
|
106
109
|
|
|
107
110
|
def load(database_url_or_db, schema, options = { drop: false })
|
|
111
|
+
schema = rewrite_non_integer_primary_keys(schema)
|
|
108
112
|
# Accept either a database URL or an existing connection object
|
|
109
113
|
if database_url_or_db.is_a?(Sequel::Database)
|
|
110
114
|
db = database_url_or_db
|
|
@@ -158,6 +162,25 @@ END_MIG
|
|
|
158
162
|
end
|
|
159
163
|
end
|
|
160
164
|
|
|
165
|
+
NON_INTEGER_PK_PATTERN = /^(\s*)primary_key\s+(:?\w+),\s*:type=>"([^"]+)"(.*)$/
|
|
166
|
+
INTEGER_DB_TYPES = /\A(?:int(?:eger|\d+)?|bigint|smallint|serial|bigserial|smallserial)/i
|
|
167
|
+
|
|
168
|
+
# On PG 10+, Sequel's CreateTableGenerator injects `identity: true` into
|
|
169
|
+
# every primary_key call via serial_primary_key_options. PG rejects IDENTITY
|
|
170
|
+
# on non-integer types. Rewrite `primary_key :col, :type=>"varchar..."` to
|
|
171
|
+
# `column :col, "varchar...", primary_key: true, null: false` which bypasses
|
|
172
|
+
# that code path entirely.
|
|
173
|
+
def rewrite_non_integer_primary_keys(schema_str)
|
|
174
|
+
schema_str.gsub(NON_INTEGER_PK_PATTERN) do
|
|
175
|
+
indent, col, db_type, rest = $1, $2, $3, $4
|
|
176
|
+
if db_type =~ INTEGER_DB_TYPES
|
|
177
|
+
"#{indent}primary_key #{col}, :type=>\"#{db_type}\"#{rest}"
|
|
178
|
+
else
|
|
179
|
+
"#{indent}column #{col}, \"#{db_type}\", primary_key: true, null: false#{rest}"
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
|
|
161
184
|
def reset_db_sequences(database_url)
|
|
162
185
|
Sequel.connect(database_url) do |db|
|
|
163
186
|
db.extension :schema_dumper
|
data/lib/tapsoob/version.rb
CHANGED
|
@@ -105,12 +105,24 @@ RSpec.describe 'PostgreSQL round-trip', :integration do
|
|
|
105
105
|
|
|
106
106
|
after(:each) do
|
|
107
107
|
@src_db.run("DROP TABLE IF EXISTS varchar_pk_table")
|
|
108
|
+
@dst_db.run("DROP TABLE IF EXISTS varchar_pk_table")
|
|
108
109
|
end
|
|
109
110
|
|
|
110
111
|
it 'skips reset without logging a warning (no sequence attached to varchar PK)' do
|
|
111
112
|
expect(Tapsoob.log).not_to receive(:warn)
|
|
112
113
|
Tapsoob::Schema.reset_db_sequences(@src_url)
|
|
113
114
|
end
|
|
115
|
+
|
|
116
|
+
it 'round-trips the table without identity column errors' do
|
|
117
|
+
dump_dir = Dir.mktmpdir
|
|
118
|
+
begin
|
|
119
|
+
pull(src_url, dump_dir)
|
|
120
|
+
expect { push(dst_url, dump_dir) }.not_to raise_error
|
|
121
|
+
expect(dst_db[:varchar_pk_table].where(id: 'abc-123').count).to eq(1)
|
|
122
|
+
ensure
|
|
123
|
+
FileUtils.rm_rf(dump_dir)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
114
126
|
end
|
|
115
127
|
|
|
116
128
|
context 'when reset_primary_key_sequence raises a DatabaseError' do
|