volt-sql 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 918bb2e8b15738d1e2d2b80cba4c09cfa24f26a8
4
- data.tar.gz: a52245cae20149dec9fa0033c92ca382ef800eb9
3
+ metadata.gz: 5b04ceed71a848a60ef9725efe1347ee8f9c51de
4
+ data.tar.gz: fca4338445f775418f3734f4b8e147a4e665f0a5
5
5
  SHA512:
6
- metadata.gz: 1383480587bfd8b8eed8ebcde8c56ebd68163366622fe9fb63c7d61afa52cea5bad104c6f0ff217bcaaecbd7782f7b5dbcdc77b9b10af66f46f981df6b1d182d
7
- data.tar.gz: e52a71cfb0f1a3941fbd67f9ae62ba01e934b90a27f98440c57c27655dbdb95212af6f99764254f4d776aac4e11cbfb88a59bb1863939becb6e81f85e46b32c8
6
+ metadata.gz: fe9bca98cc32a62cdac847ed4ad9d3e6927230e3bf859e4eff8c31b6686202788a6097646d8b0ec5123a9502fb363dbd7bc71ccf3f0aefe316a6058291510039
7
+ data.tar.gz: 5993391dfc9ea07dbdd04d1f0754b0797e81d1649d3ae18044ca65fce6ab6d80e8740ad773ff6527addc7b9de21ef38790cb13f81852177ea71edeb43cdb9fcf
@@ -32,6 +32,15 @@ module Volt
32
32
  up_code = []
33
33
  down_code = []
34
34
 
35
+ # First remove the default values
36
+ db_default = db_opts.delete(:default)
37
+ sequel_default = sequel_opts.delete(:default)
38
+
39
+ if db_default != sequel_default
40
+ up_code << "set_column_default #{table_name.inspect}, #{column_name.inspect}, #{sequel_default.inspect}"
41
+ down_code << "set_column_default #{table_name.inspect}, #{column_name.inspect}, #{db_default.inspect}"
42
+ end
43
+
35
44
  if db_opts != sequel_opts
36
45
  # Fetch allow_null, keeping in mind it defaults to true
37
46
  db_null = db_opts.fetch(:allow_null, true)
@@ -2,7 +2,7 @@
2
2
  module Volt
3
3
  class Migration
4
4
  def initialize(db=nil)
5
- @db = db || Volt.current_app.database.db
5
+ @db ||= Volt.current_app.database.raw_db
6
6
  end
7
7
 
8
8
  def add_column(table_name, column_name, klasses, options={})
@@ -0,0 +1,35 @@
1
+ # We can't use Volt::Model until after the reconcile step has happened, so
2
+ # these methods work directly with the database.
3
+
4
+ module Volt
5
+ class MigrationRunner
6
+ def raw_db
7
+ @raw_db ||= Volt.current_app.database.raw_db
8
+ end
9
+
10
+ def ensure_migration_versions_table
11
+ if !raw_db.tables || !raw_db.tables.include?(:migration_versions)
12
+ raw_db.create_table(:migration_versions) do
13
+ primary_key :id
14
+ Fixnum :version, unique: true
15
+ end
16
+ end
17
+ end
18
+
19
+ def add_version(version)
20
+ raw_db[:migration_versions].insert(version: version)
21
+ end
22
+
23
+ def has_version?(version)
24
+ raw_db.from(:migration_versions).where(version: version).count > 0
25
+ end
26
+
27
+ def remove_version(version)
28
+ raw_db.from(:migration_versions).where(version: version).delete
29
+ end
30
+
31
+ def all_versions
32
+ raw_db.from(:migration_versions).all.map {|v| v[:version] }
33
+ end
34
+ end
35
+ end
@@ -22,6 +22,9 @@ 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
+ # Make sure the migrations are up to date first.
26
+ Volt::MigrationRunner.new(@db).run
27
+
25
28
  Volt::RootModels.model_classes.each do |model_class|
26
29
  TableReconcile.new(@adaptor, @db, model_class).run
27
30
  end
@@ -5,6 +5,7 @@ require 'fileutils'
5
5
  require 'thread'
6
6
  require 'sql/lib/migration'
7
7
  require 'sql/lib/migration_generator'
8
+ require 'sql/lib/migration_runner'
8
9
  require 'sql/lib/reconcile'
9
10
  require 'sql/lib/sql_logger'
10
11
  require 'sql/lib/helper'
@@ -16,7 +17,7 @@ module Volt
16
17
  class SqlAdaptorServer < BaseAdaptorServer
17
18
  include Volt::Sql::SqlLogger
18
19
 
19
- attr_reader :db, :sql_db, :adaptor_name
20
+ attr_reader :sql_db, :adaptor_name
20
21
 
21
22
  # :reconcile_complete is set to true after the initial load and reconcile.
22
23
  # Any models created after this point will attempt to be auto-reconciled.
@@ -127,6 +128,11 @@ module Volt
127
128
  @db
128
129
  end
129
130
 
131
+ # Raw access to the sequel connection without auto-migrating.
132
+ def raw_db
133
+ @db
134
+ end
135
+
130
136
  # @param - a string URI, or a Hash of options
131
137
  # @param - the adaptor name
132
138
  def connect_uri_or_options
@@ -249,7 +255,7 @@ module Volt
249
255
  end
250
256
 
251
257
  def query(collection, query)
252
- allowed_methods = %w(where where_with_block offset limit count)
258
+ allowed_methods = %w(where where_with_block offset order limit count)
253
259
 
254
260
  result = db.from(collection.to_sym)
255
261
 
@@ -279,6 +285,21 @@ module Volt
279
285
  result = result.where(*args) do |ident|
280
286
  Sql::WhereCall.new(ident).call(block_ast)
281
287
  end
288
+ elsif method_name == :order
289
+ op = args[0]
290
+ unless op.is_a?(Hash)
291
+ raise ".order(..) should be passed a hash of field: :desc or field: :asc"
292
+ end
293
+
294
+ ops = op.map do |field, asc_desc|
295
+ if asc_desc == :asc
296
+ field
297
+ else
298
+ Sequel.desc(field)
299
+ end
300
+ end
301
+
302
+ result = result.order(*ops)
282
303
  else
283
304
  args = self.class.move_to_db_types(args)
284
305
  result = result.send(method_name, *args)
@@ -313,7 +334,7 @@ module Volt
313
334
  RootModels.clear_temporary
314
335
 
315
336
  # Drop all tables
316
- db(true).drop_table(*db.tables)
337
+ raw_db.drop_table(*db.tables)
317
338
 
318
339
  RootModels.model_classes.each do |model_klass|
319
340
  model_klass.reconciled = false
@@ -85,7 +85,7 @@ module Volt
85
85
  if (orphan_fields.size == 1 && new_fields.size == 1)
86
86
  from_name = orphan_fields[0]
87
87
  to_name = new_fields[0]
88
- auto_migrate_field_rename(table_name, from_name, to_name)
88
+ @field_updater.auto_migrate_field_rename(table_name, from_name, to_name)
89
89
 
90
90
  # Move in start fields
91
91
  db_fields[to_name] = db_fields.delete(from_name)
@@ -1,5 +1,5 @@
1
1
  module Volt
2
2
  module Sql
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: volt-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Stout
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-03 00:00:00.000000000 Z
11
+ date: 2015-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -72,6 +72,7 @@ files:
72
72
  - app/sql/lib/index_updater.rb
73
73
  - app/sql/lib/migration.rb
74
74
  - app/sql/lib/migration_generator.rb
75
+ - app/sql/lib/migration_runner.rb
75
76
  - app/sql/lib/reconcile.rb
76
77
  - app/sql/lib/sql_adaptor_client.rb
77
78
  - app/sql/lib/sql_adaptor_server.rb