chrono_model 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -184,11 +184,11 @@ them in your output, use `rake VERBOSE=true`.
184
184
 
185
185
  * The rules and temporal indexes cannot be saved in schema.rb. The AR
186
186
  schema dumper is quite basic, and it isn't (currently) extensible.
187
- As we're using many database-specific features, you'll better off with
188
- the SQL schema dumper (`config.active_record.schema_format = :sql` in
189
- `config/application.rb`). Be sure to add [these
190
- files](https://gist.github.com/4548844) in your `lib/tasks` if you want
191
- `rake db:setup` to work.
187
+ As we're using many database-specific features, Chronomodel forces the
188
+ usage of the `:sql` schema dumper, and included rake tasks override
189
+ `db:schema:dump` and `db:schema:load` to do `db:structure:dump` and
190
+ `db:structure:load`. Two helper tasks are also added, `db:data:dump`
191
+ and `db:data:load`.
192
192
 
193
193
  * `.includes` still doesn't work, but it'll fixed.
194
194
 
@@ -432,10 +432,10 @@ module ChronoModel
432
432
  end
433
433
 
434
434
  TableCache = (Class.new(HashWithIndifferentAccess) do
435
- def all ; keys; ; end
436
- def add! table ; self[table] = true ; end
437
- def del! table ; self[table] = nil ; end
438
- def fetch table ; self[table] ||= yield ; end
435
+ def all ; keys; ; end
436
+ def add! table ; self[table.to_s] = true ; end
437
+ def del! table ; self[table.to_s] = nil ; end
438
+ def fetch table ; self[table.to_s] ||= yield ; end
439
439
  end).new
440
440
 
441
441
  # Returns true if the given name references a temporal table.
@@ -5,15 +5,15 @@ module ChronoModel
5
5
  end
6
6
 
7
7
  rake_tasks do
8
+ load 'chrono_model/schema_format.rake'
8
9
 
9
- namespace :db do
10
- namespace :chrono do
11
- task :create_schemas do
12
- puts 'create schemas'
13
- ActiveRecord::Base.connection.chrono_create_schemas!
14
- end
15
- end
16
- end
10
+ namespace :db do
11
+ namespace :chrono do
12
+ task :create_schemas do
13
+ ActiveRecord::Base.connection.chrono_create_schemas!
14
+ end
15
+ end
16
+ end
17
17
 
18
18
  task 'db:schema:load' => 'db:chrono:create_schemas'
19
19
  end
@@ -0,0 +1,50 @@
1
+ load File.expand_path(File.dirname(__FILE__) + '/schema_format.rb')
2
+
3
+ namespace :db do
4
+ namespace :structure do
5
+ desc "desc 'Dump the database structure to db/structure.sql. Specify another file with DB_STRUCTURE=db/my_structure.sql"
6
+ task :dump => :environment do
7
+ config = PG.config!
8
+ target = ENV['DB_STRUCTURE'] || Rails.root.join('db', 'structure.sql')
9
+ schema = config[:schema_search_path] || 'public'
10
+
11
+ PG.make_dump target, *config.values_at(:username, :database),
12
+ '-s', '-O', '-n', schema,
13
+ '-n', ChronoModel::Adapter::TEMPORAL_SCHEMA,
14
+ '-n', ChronoModel::Adapter::HISTORY_SCHEMA
15
+
16
+ # Add migration information, after resetting the schema to the default one
17
+ File.open(target, 'a') do |f|
18
+ f.puts "SET search_path = #{schema}, pg_catalog;"
19
+ f.puts ActiveRecord::Base.connection.dump_schema_information
20
+ end
21
+ end
22
+
23
+
24
+ desc "Load structure.sql file into the current environment's database"
25
+ task :load => :environment do
26
+ # Loads the db/structure.sql file into current environment's database.
27
+ #
28
+ source = ENV['DB_STRUCTURE'] || Rails.root.join('db', 'structure.sql')
29
+
30
+ PG.load_dump source, *PG.config!.values_at(:username, :database, :template)
31
+ end
32
+ end
33
+
34
+ namespace :data do
35
+ desc "Save a dump of the database in ENV['DUMP'] or db/data.NOW.sql"
36
+ task :dump => :environment do
37
+ target = ENV['DUMP'] || Rails.root.join('db', "data.#{Time.now.to_f}.sql")
38
+
39
+ PG.make_dump target, *PG.config!.values_at(:username, :database), '-c'
40
+ end
41
+
42
+ desc "Load a dump of the database from ENV['DUMP']"
43
+ task :load => :environment do
44
+ source = ENV['DUMP'].presence or
45
+ raise ArgumentError, "Invoke as rake db:data:load DUMP=/path/to/data.sql"
46
+
47
+ PG.load_dump source, *PG.config!.values_at(:username, :database)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,78 @@
1
+ # h/t http://stackoverflow.com/questions/4698467
2
+ #
3
+ Rails.application.config.active_record.schema_format = :sql
4
+
5
+ # Clear Rails' ones
6
+ %w( db:structure:dump db:structure:load ).each {|t| Rake::Task[t].clear }
7
+
8
+ # Make schema:dump and schema:load invoke structure:dump and structure:load
9
+ Rake::Task['db:schema:dump'].clear.enhance(['environment']) do
10
+ Rake::Task['db:structure:dump'].invoke
11
+ end
12
+
13
+ Rake::Task['db:schema:load'].clear.enhance(['environment']) do
14
+ Rake::Task['db:structure:load'].invoke
15
+ end
16
+
17
+ # PG utilities
18
+ #
19
+ module PG
20
+ extend self
21
+
22
+ def config!
23
+ ActiveRecord::Base.connection_pool.spec.config.tap do |config|
24
+ ENV['PGHOST'] = config[:host].to_s if config.key?(:host)
25
+ ENV['PGPORT'] = config[:port].to_s if config.key?(:port)
26
+ ENV['PGPASSWORD'] = config[:password].to_s if config.key?(:password)
27
+ end
28
+ end
29
+
30
+ def make_dump(target, username, database, *options)
31
+ exec 'pg_dump', '-f', target, '-U', username, database, *options
32
+ end
33
+
34
+ def load_dump(source, username, database, *options)
35
+ exec 'psql', '-U', username, '-f', source, database, *options
36
+ end
37
+
38
+ def exec(*argv)
39
+ argv = argv.compact.map(&:to_s)
40
+
41
+ print "> \033[1m#{argv.join(' ')}\033[0m... "
42
+ logger.info "PG: exec #{argv.join(' ')}"
43
+
44
+ stdout, stdout_w = IO.pipe
45
+ stderr, stderr_w = IO.pipe
46
+ pid = fork {
47
+ stdout.close; STDOUT.reopen(stdout_w)
48
+ stderr.close; STDERR.reopen(stderr_w)
49
+
50
+ Kernel.exec *argv
51
+ }
52
+ stdout_w.close; stderr_w.close
53
+ pid, status = Process.wait2
54
+
55
+ puts "exit #{status.exitstatus}"
56
+ logger.info "PG: exit #{status.exitstatus}"
57
+
58
+ out, err = stdout.read.chomp, stderr.read.chomp
59
+ stdout.close; stderr.close
60
+
61
+ if out.present?
62
+ logger.info "PG: -----8<----- stdout ----->8-----"
63
+ logger.info out
64
+ logger.info "PG: ----8<---- end stdout ---->8----"
65
+ end
66
+
67
+ if err.present?
68
+ logger.info "PG: -----8<----- stderr ----->8-----"
69
+ logger.info err
70
+ logger.info "PG: ----8<---- end stderr ---->8----"
71
+ end
72
+ end
73
+
74
+ def logger
75
+ ActiveRecord::Base.logger
76
+ end
77
+
78
+ end
@@ -1,3 +1,3 @@
1
1
  module ChronoModel
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chrono_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-22 00:00:00.000000000 Z
12
+ date: 2013-05-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &69702880 !ruby/object:Gem::Requirement
16
+ requirement: &70273330 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *69702880
24
+ version_requirements: *70273330
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: pg
27
- requirement: &69701390 !ruby/object:Gem::Requirement
27
+ requirement: &70273120 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *69701390
35
+ version_requirements: *70273120
36
36
  description: Give your models as-of date temporal extensions. Built entirely for PostgreSQL
37
37
  >= 9.0
38
38
  email:
@@ -55,6 +55,8 @@ files:
55
55
  - lib/chrono_model/compatibility.rb
56
56
  - lib/chrono_model/patches.rb
57
57
  - lib/chrono_model/railtie.rb
58
+ - lib/chrono_model/schema_format.rake
59
+ - lib/chrono_model/schema_format.rb
58
60
  - lib/chrono_model/time_gate.rb
59
61
  - lib/chrono_model/time_machine.rb
60
62
  - lib/chrono_model/utils.rb
@@ -90,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
92
  version: '0'
91
93
  requirements: []
92
94
  rubyforge_project:
93
- rubygems_version: 1.8.10
95
+ rubygems_version: 1.8.11
94
96
  signing_key:
95
97
  specification_version: 3
96
98
  summary: Temporal extensions (SCD Type II) for Active Record