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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3851833bd5308ba174d2d70c217b6fc73c7d583b
4
- data.tar.gz: 95071a258ce6202862e38a64d0d57fee8a0cfea9
3
+ metadata.gz: 1d4608edc6072606f0aaaa72a11f1ba7a25814ea
4
+ data.tar.gz: 3c8b040331ea7dcbea8b89aa72c1712150517e20
5
5
  SHA512:
6
- metadata.gz: 424334e2045a9cf08bdf6fedddbc8710c05f1d101ac2bd22350191e71e8dccc201118bb3511590ff56d67610c80494e6c7aed98d2d2e263b582343f910179567
7
- data.tar.gz: cac4af5351c520d8610a98c54e57d7c93a884bcc0f0b33e953eedb45291e43e9cfd5fccfef47ee791e3ab07dcde72a546c6c3f0d22959c3acdf8f9b24fe1f1c6
6
+ metadata.gz: f6050054f09f58c312fc744b1e6ca5cb6ddbc4d52b2908b63d0ba48b2720309e4a85bcaf0399451ccd97e01c4404c0215ee181dede439f98c40bda290c5e9068
7
+ data.tar.gz: c98ce906a1bca09e5a3d17a8aa9d0283978246bfa2756bc5eec8e57c89792d6b32a57296e73747a0a6a8883bee11615ecccc7e75ef158bf48a5615700082284f
data/README.md CHANGED
@@ -53,7 +53,7 @@ But you would lose it even with manual migrations.
53
53
  Add this line to your application's Gemfile:
54
54
 
55
55
  ``` ruby
56
- gem 'db_schema', '~> 0.2.0'
56
+ gem 'db_schema', '~> 0.2.1'
57
57
  ```
58
58
 
59
59
  And then execute:
@@ -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(
@@ -1,13 +1,9 @@
1
1
  module DbSchema
2
2
  module Reader
3
3
  class << self
4
- def read_schema
5
- adapter.read_schema
6
- end
4
+ extend Forwardable
7
5
 
8
- def read_table(table_name)
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
- tables = DbSchema.connection.tables.map do |table_name|
124
- read_table(table_name)
125
- end
126
-
127
- enums = DbSchema.connection[ENUMS_QUERY].map do |enum_data|
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
@@ -1,3 +1,3 @@
1
1
  module DbSchema
2
- VERSION = '0.2'
2
+ VERSION = '0.2.1'
3
3
  end
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
- normalize(desired_schema)
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: '0.2'
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-02 00:00:00.000000000 Z
11
+ date: 2016-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel