sack 1.0.2 → 1.1.3

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: c2490d6fa06201302120d44d10dcb76b33dddb82
4
- data.tar.gz: 86e72e9b5d1979e083b83df349fc2f724a225748
3
+ metadata.gz: 36631ab9960ab0352fc67299eb542d4c45ff7bea
4
+ data.tar.gz: 68295a9026aec98c51b35a6574608568510f22de
5
5
  SHA512:
6
- metadata.gz: be1234cb58d2e5ab49090118fb8a53f6841f9073bf2cbe8462226579674a97025ea679aeaa2cb0c7b875dd96a117681205b5e65c54f9b78c1ce2dd57c83a5299
7
- data.tar.gz: 7b58c7bbc84c2531e76f35b4b7fca2ff732db9443d027789b25186b5c94ac657a162ff06948a12cc0b526fa35db401f6078df9b0bf3c5a67999e3404acdab396
6
+ metadata.gz: eb2c612f125c841885c785f7ade20715d5afea212c3346a6b98c40499e758d2e2d145051ca57afc21d64f07eb5a1ab1ed2a3bec2a584cca5e941ad0609098d9a
7
+ data.tar.gz: 3580ba64a327c27972d694da5a7e7962967303a0507638b9286d689dc3fe3d768c3e2778d70df01bf0567d659fb20f86cab046b62c952f9c3532aaf9b443f6e1
data/README.md CHANGED
@@ -286,6 +286,20 @@ module Bork
286
286
  end
287
287
  ```
288
288
 
289
+ ### Schema Migrations
290
+
291
+ As your application evolves, your database schema may change over time.
292
+
293
+ Unlike many popular frameworks, the concept of 'migrations' does not exist with Sack, at least not in the 'traditional' sense where one would create migration files describing changes to be applied in sequence.
294
+
295
+ Sack will simply always try to keep your database up to date with your schema.
296
+ Upon initialization, Sack verifies the database against the schema defined by your application. The migration policy is simple: _anything that is not present gets added_.
297
+
298
+ New tables will be created, as well as new fields (columns) in already-existing tables.
299
+
300
+ *HOWEVER* it must be noted that things only get +added+. Tables and fields are NEVER removed. Existing fields are NEVER modified.
301
+ This keeps the concept of migration VERY simple.
302
+
289
303
  ## License
290
304
 
291
305
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -38,6 +38,14 @@ module Sack
38
38
  @db.execute "create table #{Sanitizer.table @schema, name} (#{fq});"
39
39
  end
40
40
 
41
+ # Alter Table:
42
+ # Adds missing fields to an already-existing table (ONLY adds, does not remove or modify existing fields).
43
+ # @param [Symbol] name Table name
44
+ # @param [Hash] fields A hash mapping of field names to type definition arrays (from _FTYPES_)
45
+ def alter_table name, fields
46
+ fields.each { |fname, ftype| @db.execute "alter table #{Sanitizer.table @schema, name} add #{Sanitizer.field_name fname} #{ftype.respond_to?(:each) ? ftype.collect { |e| FTYPES[Sanitizer.ftype e] }.join(' ') : FTYPES[Sanitizer.ftype ftype]};" rescue nil }
47
+ end
48
+
41
49
  # Count:
42
50
  # Counts the number of rows for a given table.
43
51
  # @param [Symbol] table Table name
data/lib/sack/database.rb CHANGED
@@ -72,14 +72,23 @@ module Sack
72
72
  # Verify Schema:
73
73
  # Verifies the supplied schema against the actual database & re-creates it if necessary.
74
74
  def verify_schema
75
- open { |data| rebuild_schema data unless @schema.keys.inject(true) { |a, table| a && (data.exec "select count(*) from #{table};" rescue nil) } }
75
+ open { |data| rebuild_schema data unless check_schema data }
76
+ end
77
+
78
+ # Check Schema:
79
+ # Verifies the supplied schema against the actual database.
80
+ # @param [Data] data Data access interface
81
+ # @return [Object] True if schema is valid, False otherwise
82
+ def check_schema data
83
+ @schema.keys.inject(true) { |a, table| a && (data.exec "select count(*) from #{table};" rescue nil) }
76
84
  end
77
85
 
78
86
  # Rebuild Schema:
79
87
  # Re-creates the database according to the supplied schema.
80
88
  # @param [Data] data Data access interface
81
89
  def rebuild_schema data
82
- @schema.each { |table, fields| data.create_table table, fields }
90
+ @schema.each { |table, fields| data.create_table table, fields rescue data.alter_table table, fields }
91
+ raise "Rebuilding Schema Failed - Check Database & Access Permissions" unless check_schema data
83
92
  end
84
93
 
85
94
  # Method Missing:
data/lib/sack/version.rb CHANGED
@@ -5,5 +5,5 @@
5
5
  module Sack
6
6
 
7
7
  # Version
8
- VERSION = '1.0.2'
8
+ VERSION = '1.1.3'
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eresse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-29 00:00:00.000000000 Z
11
+ date: 2017-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler