db_stager 0.2.0 → 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: 5596cc698905e2cb5c03fbcebee2a272403360f4
4
- data.tar.gz: 2e27f20781268cc10f257c000cec3307d8b043cf
3
+ metadata.gz: 7144864a030786eac584c002945d972aebf2c2f9
4
+ data.tar.gz: 5322fb8b234927617f4696ae6d4d5dfbcd3c5fa1
5
5
  SHA512:
6
- metadata.gz: ecd312cb600acc15779f8336443da10cd65997fd80a18a08744a5d3f0d7a88fde9aa99c253d972b128ab7779633b44b29654d2ca1830eefb44d69865cd9c3226
7
- data.tar.gz: 9acec0d9ffaf4eaff6dcbd7b034b00828a93c0c31e2c2a1a8b53f3b24cd629dcf213b0e8af4efad56c93c0dc2ea65d015d52a932e07a46627bc6ae4601e668a6
6
+ metadata.gz: 3102262cec9d51155b1e83cfd56649b552e700c872ab2f5a23fe70f8cf641d14ec35b77ca5d1df351dc8d07f535cdb83e7fa027cf53654e179d1ab6289577bef
7
+ data.tar.gz: fe5e70470b372f04ed99a6cd2ba0d3a77f6fd285805dfa5c7619160e0ff7254ce71f89c9724a83129c95b375dc29c1b2334a037e89af2d89ab4394567c406511
data/Gemfile.lock CHANGED
@@ -1,8 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- db_stager (0.2.0)
4
+ db_stager (0.2.1)
5
5
  dotenv (~> 2.2)
6
+ faker (~> 1.9)
7
+ hashie (~> 3.5)
6
8
  sequel (~> 4.41.0)
7
9
  thor (~> 0.19)
8
10
 
@@ -10,8 +12,14 @@ GEM
10
12
  remote: https://rubygems.org/
11
13
  specs:
12
14
  coderay (1.1.2)
15
+ concurrent-ruby (1.0.5)
13
16
  diff-lcs (1.3)
14
17
  dotenv (2.5.0)
18
+ faker (1.9.1)
19
+ i18n (>= 0.7)
20
+ hashie (3.5.7)
21
+ i18n (1.1.0)
22
+ concurrent-ruby (~> 1.0)
15
23
  method_source (0.9.0)
16
24
  pry (0.11.3)
17
25
  coderay (~> 1.1.0)
data/db_stager.gemspec CHANGED
@@ -29,4 +29,6 @@ Gem::Specification.new do |spec|
29
29
  spec.add_runtime_dependency "dotenv", "~> 2.2"
30
30
  spec.add_runtime_dependency "thor", "~> 0.19"
31
31
  spec.add_runtime_dependency "sequel", "~> 4.41.0"
32
+ spec.add_runtime_dependency "hashie", "~> 3.5"
33
+ spec.add_runtime_dependency "faker", "~> 1.9"
32
34
  end
data/lib/db_stager/cli.rb CHANGED
@@ -2,7 +2,8 @@ require 'thor'
2
2
  require 'sequel'
3
3
  require 'yaml'
4
4
  require 'erb'
5
- require 'erb'
5
+ require 'hashie'
6
+ require 'faker'
6
7
  require 'dotenv/load'
7
8
 
8
9
  module DbStager
@@ -18,18 +19,28 @@ module DbStager
18
19
  end
19
20
 
20
21
  method_option :environment, type: :string, aliases: '-e'
21
- method_option :file, type: :string, aliases: '-f'
22
22
  desc "capture", "grab a new stager copy of your db"
23
23
  def capture(dir=".")
24
24
  Dir.chdir(dir)
25
25
 
26
- sh 'rake db:structure:dump'
26
+ run 'rake db:structure:dump'
27
27
 
28
- capture_file = options[:file] || File.join(Dir.pwd, "db", "#{environment}_capture.sql")
29
- File.open(capture_file, "w") do |file|
28
+ capture_file = File.join(Dir.pwd, "db", "structure.sql")
29
+ File.open(capture_file, "a") do |file|
30
30
  db.tables.each do |table_name|
31
+ next if excluded_tables.include?(table_name)
31
32
  items = db[table_name]
32
33
  items.each do |item|
34
+ if fields[table_name]
35
+ fields[table_name].each do |field_name, task|
36
+ if task
37
+ item[field_name.to_sym] = send(task)
38
+ else
39
+ item[field_name.to_sym] = nil
40
+ end
41
+ end
42
+ item
43
+ end
33
44
  file.puts "#{items.insert_sql(item)};"
34
45
  file.puts
35
46
  end
@@ -38,8 +49,8 @@ module DbStager
38
49
  end
39
50
 
40
51
 
41
- desc "upload", "upload your most recent capture to S3"
42
- desc "download", "download your most recent capture from S3"
52
+ #desc "upload", "upload your most recent capture to S3"
53
+ #desc "download", "download your most recent capture from S3"
43
54
 
44
55
  method_option :environment, type: :string, aliases: '-e'
45
56
  method_option :structure_file, type: :string, aliases: '-s'
@@ -47,19 +58,48 @@ module DbStager
47
58
  desc "load", "overwrite the current db with the most recently downloaded stager db"
48
59
  def load(dir=".")
49
60
  Dir.chdir(dir)
50
- structure_file = options[:structure_file] || File.join(Dir.pwd, "db", "structure.sql")
51
- db.run File.read(struture_file)
52
- capture_file = options[:file] || File.join(Dir.pwd, "db", "#{environment}_capture.sql")
53
- db.run File.read(capture_file)
61
+ run 'rake db:structure:load'
54
62
  end
55
- end
56
63
 
57
- protected
58
- def environment
59
- @environment ||= options[:environment] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
60
- end
64
+ protected
65
+ def environment
66
+ @environment ||= options[:environment] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
67
+ end
68
+
69
+ def db
70
+ @db ||= Sequel.connect(YAML.load(ERB.new(File.read(File.join(Dir.pwd, 'config', 'database.yml'))).result)[environment])
71
+ end
72
+
73
+ def config_file
74
+ return @config_file if @config_file
75
+ config_file_path = File.join(Dir.pwd, 'config', 'stage_hand.yml')
76
+ if File.exists?(config_file_path)
77
+ @config_file = Hashie::Mash.load(ERB.new(File.read(config_file_path)))
78
+ else
79
+ @config_file = {}
80
+ end
81
+ end
82
+
83
+ def excluded_tables
84
+ return @excluded_tables if @excluded_tables
85
+ @excluded_tables = config_file[:excluded_tables] || []
86
+ @excluded_tables.collect! { |t| t.to_sym }
87
+ # always skip schema_migrations, they get set by the structure file
88
+ @excluded_tables = @excluded_tables | [:schema_migrations]
89
+ return @excluded_tables
90
+ end
91
+
92
+ def fields
93
+ return @fields if @fields
94
+ # prototype is people: [{name: "Faker::Name.unique.name"}, {email: false}]
95
+ # key is table name
96
+ # fake can be any executable faker string
97
+ # if fake is not set, no edit is made
98
+ # skip defaults to false if not set
99
+ # you should set either fake or skip, not both... duh
100
+ @fields = config_file[:fields] || {}
101
+ return @fields
102
+ end
61
103
 
62
- def db
63
- @db ||= Sequel.connect(YAML.load(ERB.new(File.read(File.join(Dir.pwd, 'config', 'database.yml'))).result)[environment])
64
104
  end
65
105
  end
@@ -1,3 +1,3 @@
1
1
  module DbStager
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db_stager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Kaufman
@@ -108,6 +108,34 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: 4.41.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: hashie
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.5'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.5'
125
+ - !ruby/object:Gem::Dependency
126
+ name: faker
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.9'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.9'
111
139
  description: Perform backups and restores of databases, scrubbing records along the
112
140
  way. This allows taking limited snapshots of large databases and keeps customer
113
141
  info out of your staging site.