db_schema 0.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/db_schema/normalizer.rb +37 -6
- data/lib/db_schema/reader.rb +25 -19
- data/lib/db_schema/version.rb +1 -1
- data/lib/db_schema.rb +1 -13
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d4608edc6072606f0aaaa72a11f1ba7a25814ea
|
4
|
+
data.tar.gz: 3c8b040331ea7dcbea8b89aa72c1712150517e20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6050054f09f58c312fc744b1e6ca5cb6ddbc4d52b2908b63d0ba48b2720309e4a85bcaf0399451ccd97e01c4404c0215ee181dede439f98c40bda290c5e9068
|
7
|
+
data.tar.gz: c98ce906a1bca09e5a3d17a8aa9d0283978246bfa2756bc5eec8e57c89792d6b32a57296e73747a0a6a8883bee11615ecccc7e75ef158bf48a5615700082284f
|
data/README.md
CHANGED
data/lib/db_schema/normalizer.rb
CHANGED
@@ -4,6 +4,43 @@ module DbSchema
|
|
4
4
|
class Normalizer
|
5
5
|
attr_reader :table
|
6
6
|
|
7
|
+
class << self
|
8
|
+
def normalize_tables(schema)
|
9
|
+
DbSchema.connection.transaction do
|
10
|
+
create_extensions!(schema.extensions)
|
11
|
+
create_enums!(schema.enums)
|
12
|
+
|
13
|
+
schema.tables = schema.tables.map do |table|
|
14
|
+
if table.has_expressions?
|
15
|
+
new(table).normalized_table
|
16
|
+
else
|
17
|
+
table
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
raise Sequel::Rollback
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
def create_extensions!(extensions)
|
27
|
+
(extensions - DbSchema::Reader.read_extensions).each do |extension|
|
28
|
+
operation = DbSchema::Changes::CreateExtension.new(extension.name)
|
29
|
+
DbSchema::Runner.new([operation]).run!
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_enums!(enums)
|
34
|
+
existing_enums_names = DbSchema::Reader.read_enums.map(&:name)
|
35
|
+
enums.each do |enum|
|
36
|
+
next if existing_enums_names.include?(enum.name)
|
37
|
+
|
38
|
+
operation = DbSchema::Changes::CreateEnum.new(enum.name, enum.values)
|
39
|
+
DbSchema::Runner.new([operation]).run!
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
7
44
|
def initialize(table)
|
8
45
|
@table = table
|
9
46
|
end
|
@@ -11,8 +48,6 @@ module DbSchema
|
|
11
48
|
def normalized_table
|
12
49
|
create_temporary_table!
|
13
50
|
read_temporary_table
|
14
|
-
ensure
|
15
|
-
cleanup!
|
16
51
|
end
|
17
52
|
|
18
53
|
private
|
@@ -39,10 +74,6 @@ module DbSchema
|
|
39
74
|
)
|
40
75
|
end
|
41
76
|
|
42
|
-
def cleanup!
|
43
|
-
DbSchema.connection.drop_table(temporary_table_name, if_exists: true)
|
44
|
-
end
|
45
|
-
|
46
77
|
def rename_indices(indices)
|
47
78
|
indices.map do |index|
|
48
79
|
Definitions::Index.new(
|
data/lib/db_schema/reader.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
1
|
module DbSchema
|
2
2
|
module Reader
|
3
3
|
class << self
|
4
|
-
|
5
|
-
adapter.read_schema
|
6
|
-
end
|
4
|
+
extend Forwardable
|
7
5
|
|
8
|
-
|
9
|
-
adapter.read_table(table_name)
|
10
|
-
end
|
6
|
+
def_delegators :adapter, :read_schema, :read_table, :read_enums, :read_extensions
|
11
7
|
|
12
8
|
def adapter
|
13
9
|
adapter_name = DbSchema.configuration.adapter
|
@@ -120,19 +116,11 @@ SELECT extname
|
|
120
116
|
|
121
117
|
class << self
|
122
118
|
def read_schema
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
Definitions::Enum.new(enum_data[:name].to_sym, enum_data[:values].map(&:to_sym))
|
129
|
-
end
|
130
|
-
|
131
|
-
extensions = DbSchema.connection[EXTENSIONS_QUERY].map do |extension_data|
|
132
|
-
Definitions::Extension.new(extension_data[:extname].to_sym)
|
133
|
-
end
|
134
|
-
|
135
|
-
Definitions::Schema.new(tables: tables, enums: enums, extensions: extensions)
|
119
|
+
Definitions::Schema.new(
|
120
|
+
tables: read_tables,
|
121
|
+
enums: read_enums,
|
122
|
+
extensions: read_extensions
|
123
|
+
)
|
136
124
|
end
|
137
125
|
|
138
126
|
def read_table(table_name)
|
@@ -208,6 +196,24 @@ SELECT extname
|
|
208
196
|
end
|
209
197
|
end
|
210
198
|
|
199
|
+
def read_tables
|
200
|
+
DbSchema.connection.tables.map do |table_name|
|
201
|
+
read_table(table_name)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def read_enums
|
206
|
+
DbSchema.connection[ENUMS_QUERY].map do |enum_data|
|
207
|
+
Definitions::Enum.new(enum_data[:name].to_sym, enum_data[:values].map(&:to_sym))
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
def read_extensions
|
212
|
+
DbSchema.connection[EXTENSIONS_QUERY].map do |extension_data|
|
213
|
+
Definitions::Extension.new(extension_data[:extname].to_sym)
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
211
217
|
private
|
212
218
|
def index_expressions_data(indices_data)
|
213
219
|
all_positions, max_position = {}, 0
|
data/lib/db_schema/version.rb
CHANGED
data/lib/db_schema.rb
CHANGED
@@ -18,7 +18,7 @@ module DbSchema
|
|
18
18
|
def describe(&block)
|
19
19
|
desired_schema = DSL.new(block).schema
|
20
20
|
validate(desired_schema)
|
21
|
-
|
21
|
+
Normalizer.normalize_tables(desired_schema)
|
22
22
|
|
23
23
|
actual_schema = Reader.read_schema
|
24
24
|
changes = Changes.between(desired_schema, actual_schema)
|
@@ -90,18 +90,6 @@ module DbSchema
|
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
|
-
def normalize(schema)
|
94
|
-
normalized_tables = schema.tables.map do |table|
|
95
|
-
if table.has_expressions?
|
96
|
-
Normalizer.new(table).normalized_table
|
97
|
-
else
|
98
|
-
table
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
schema.tables = normalized_tables
|
103
|
-
end
|
104
|
-
|
105
93
|
def log_changes(changes)
|
106
94
|
return if changes.empty?
|
107
95
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vsevolod Romashov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|