static_db 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 +4 -4
- data/lib/static_db/dump.rb +133 -0
- data/lib/static_db/engine.rb +34 -0
- data/lib/static_db/initializer.rb +35 -0
- data/lib/static_db/load.rb +53 -0
- data/lib/static_db/tasks/static.rake +25 -0
- data/lib/static_db/version.rb +1 -1
- data/lib/static_db.rb +3 -1
- metadata +21 -3
- data/lib/static_db/railtie.rb +0 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 479d7410cc5f976ed22e42d910e249055c741df4e4c6cdf8267b01bea16f8217
|
|
4
|
+
data.tar.gz: 7b9e03429fa960d457e60a49055eacdabdd55d987e73243a8beb56d72f7437de
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0e6ef11395d226a4c53bd953ca08c103509dae4c85330c797b7620b28360366815e32942a2284430be8437220f6433766a0ed0206582b56d823e80779cc00bde
|
|
7
|
+
data.tar.gz: d7bef38d14c21fcbbbf321756ec4d9319b90ef7d45ccd408de7bc80c596eb4c88f0a83b98b0f4b92e9fa0875973d45086b57745ea2a2782196001e3300f12a1e
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module StaticDb
|
|
4
|
+
class Dump
|
|
5
|
+
|
|
6
|
+
attr_reader :fixture_path
|
|
7
|
+
|
|
8
|
+
def initialize(fixture_path:)
|
|
9
|
+
@fixture_path = Pathname.new(fixture_path)
|
|
10
|
+
@models_to_be_saved = models
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def perform
|
|
14
|
+
exit 1 if $skip_active_fixtures_dump
|
|
15
|
+
|
|
16
|
+
reenable_rake_tasks!
|
|
17
|
+
validate_records!
|
|
18
|
+
|
|
19
|
+
puts green("Dumping fixtures ...")
|
|
20
|
+
|
|
21
|
+
dump_fixtures!
|
|
22
|
+
Rake::Task["db:drop"].invoke
|
|
23
|
+
|
|
24
|
+
puts green("Done!")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def reenable_rake_tasks!
|
|
30
|
+
["db:drop"].each do |task_name|
|
|
31
|
+
Rake::Task[task_name].reenable
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def validate_records!
|
|
36
|
+
puts green("Validating records ...")
|
|
37
|
+
|
|
38
|
+
errors = []
|
|
39
|
+
|
|
40
|
+
ActiveRecord::Base.descendants.each do |model|
|
|
41
|
+
next if model.abstract_class?
|
|
42
|
+
|
|
43
|
+
model.find_each do |record|
|
|
44
|
+
unless record.valid?
|
|
45
|
+
errors << { model: model.name, slug: record.slug, errors: record.errors.full_messages }
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
if errors.any?
|
|
51
|
+
puts red("Found #{errors.length} invalid records!")
|
|
52
|
+
errors.each do |error|
|
|
53
|
+
puts "- #{error[:model]} #{error[:slug]}: #{error[:errors].join(", ")}"
|
|
54
|
+
end
|
|
55
|
+
if ARGV.first == "build"
|
|
56
|
+
puts red("Build failed!")
|
|
57
|
+
exit 1
|
|
58
|
+
else
|
|
59
|
+
puts red("Please fix the invalid records before creating a PR!")
|
|
60
|
+
end
|
|
61
|
+
else
|
|
62
|
+
puts green("Done!")
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def dump_fixtures!
|
|
67
|
+
reset_data_directory!
|
|
68
|
+
@models_to_be_saved.each { |model| format_and_write_yaml_file!(model) }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def reset_data_directory!
|
|
72
|
+
FileUtils.rm_rf(fixture_path)
|
|
73
|
+
FileUtils.mkdir_p(fixture_path)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def format_and_write_yaml_file!(model)
|
|
77
|
+
instances = fetch_model_instances(model)
|
|
78
|
+
output = format_instances(model: model, instances: instances)
|
|
79
|
+
write_yaml_file!(model: model, data: output)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def fetch_model_instances(model)
|
|
83
|
+
model.unscoped.all.order('id ASC')
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# TODO: check against old implementation!
|
|
87
|
+
def format_instances(model:, instances:)
|
|
88
|
+
output = {}
|
|
89
|
+
|
|
90
|
+
instances.each.with_index(1) do |instance, index|
|
|
91
|
+
attrs = {}
|
|
92
|
+
|
|
93
|
+
model.columns.each do |column|
|
|
94
|
+
value = instance.read_attribute_before_type_cast(column.name)
|
|
95
|
+
attrs[column.name] = value unless value.nil?
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
output["#{model}_#{index}"] = attrs
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
output
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def write_yaml_file!(model:, data:)
|
|
105
|
+
File.open(yaml_file_path(model), 'w') { |file| file << data.to_yaml }
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def models
|
|
109
|
+
Rails.application.eager_load!
|
|
110
|
+
models = ActiveRecord::Base.descendants
|
|
111
|
+
models.select! { |model| model.table_exists? && model.any? }
|
|
112
|
+
models.delete(ActiveRecord::SchemaMigration)
|
|
113
|
+
models
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def yaml_file_path(model)
|
|
117
|
+
File.join(fixture_path, generate_file_name(model) + '.yml')
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def generate_file_name(model)
|
|
121
|
+
model.table_name
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def green(text)
|
|
125
|
+
"\e[32m#{text}\e[0m"
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def red(text)
|
|
129
|
+
"\e[31m#{text}\e[0m"
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
end
|
|
133
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "static_db/initializer"
|
|
4
|
+
|
|
5
|
+
module StaticDb # :nodoc:
|
|
6
|
+
class Engine < ::Rails::Engine # :nodoc:
|
|
7
|
+
isolate_namespace StaticDb
|
|
8
|
+
|
|
9
|
+
rake_tasks do
|
|
10
|
+
load File.expand_path("./tasks/static.rake", __dir__)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
initializer "static_db.configure" do |app|
|
|
14
|
+
if defined?(Rails::Server) || defined?(Rails::Console) || ARGV.first == "build"
|
|
15
|
+
Rails.application.config.after_initialize do
|
|
16
|
+
unless Object.const_defined?("Rake::Task") && Rake::Task.task_defined?("static:load")
|
|
17
|
+
Rails.application.load_tasks
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
Rake::Task["static:load"].invoke
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
at_exit do
|
|
24
|
+
unless Object.const_defined?("Rake::Task") && Rake::Task.task_defined?("static:dump")
|
|
25
|
+
Rails.application.load_tasks
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
Rake::Task["static:dump"].invoke
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ostruct"
|
|
4
|
+
|
|
5
|
+
module StaticDb
|
|
6
|
+
# Configuration and setup utility. The engine automatically requires this
|
|
7
|
+
# file during Rails initialization, so applications can simply call
|
|
8
|
+
# `StaticDb.configure` from **any** initializer (or even the engine's own
|
|
9
|
+
# initializer). It behaves exactly like an initializer placed in
|
|
10
|
+
# `config/initializers` of a Rails project.
|
|
11
|
+
#
|
|
12
|
+
# Example usage from a host application:
|
|
13
|
+
#
|
|
14
|
+
# # config/initializers/static_db.rb
|
|
15
|
+
# StaticDb.configure do |config|
|
|
16
|
+
# config.dump_path = Rails.root.join("content", "data")
|
|
17
|
+
# #
|
|
18
|
+
# # The rake tasks also accept an explicit path argument which is passed to
|
|
19
|
+
# # the `StaticDb::Dump`/`Load` constructors; the configuration value is
|
|
20
|
+
# # used when the argument is omitted.
|
|
21
|
+
# end
|
|
22
|
+
#
|
|
23
|
+
# The configuration object is a plain OpenStruct and may be extended by
|
|
24
|
+
# the application or other libraries.
|
|
25
|
+
def self.configure
|
|
26
|
+
@config ||= OpenStruct.new
|
|
27
|
+
yield @config if block_given?
|
|
28
|
+
@config.fixture_path ||= Rails.root.join("content", "data")
|
|
29
|
+
@config
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.config
|
|
33
|
+
@config || configure
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module StaticDb
|
|
4
|
+
class Load
|
|
5
|
+
|
|
6
|
+
attr_reader :fixture_path
|
|
7
|
+
|
|
8
|
+
def initialize(fixture_path:)
|
|
9
|
+
@fixture_path = Pathname.new(fixture_path)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def perform
|
|
13
|
+
puts green("Loading fixtures ...")
|
|
14
|
+
|
|
15
|
+
reenable_rake_tasks!
|
|
16
|
+
Rake::Task["db:create"].invoke
|
|
17
|
+
Rake::Task["db:schema:load"].invoke
|
|
18
|
+
load_fixtures!
|
|
19
|
+
|
|
20
|
+
puts green("Done!")
|
|
21
|
+
rescue => e
|
|
22
|
+
puts red("Failed to load fixtures: #{e.message}")
|
|
23
|
+
puts red("Exiting and skipping active_fixtures:dump!")
|
|
24
|
+
$skip_active_fixtures_dump = true
|
|
25
|
+
exit 1
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def reenable_rake_tasks!
|
|
31
|
+
["db:create", "db:schema:load"].each do |task_name|
|
|
32
|
+
Rake::Task[task_name].reenable
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def load_fixtures!
|
|
37
|
+
base_names = Dir.glob(File.join(fixture_path, "*.yml")).map do |file|
|
|
38
|
+
File.basename(file, ".*")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
ActiveRecord::FixtureSet.create_fixtures(fixture_path, base_names)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def green(text)
|
|
45
|
+
"\e[32m#{text}\e[0m"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def red(text)
|
|
49
|
+
"\e[31m#{text}\e[0m"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
namespace :static do
|
|
4
|
+
desc "Create fixtures from database; accepts optional path argument"
|
|
5
|
+
task :dump, [:path] => :environment do |t, args|
|
|
6
|
+
path = if args[:path].blank?
|
|
7
|
+
StaticDb.config.fixture_path
|
|
8
|
+
else
|
|
9
|
+
Rails.root.join(args[:path])
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
StaticDb::Dump.new(fixture_path: path).perform
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
desc "Create database from fixtures; accepts optional path argument"
|
|
16
|
+
task :load, [:path] => :environment do |t, args|
|
|
17
|
+
path = if args[:path].blank?
|
|
18
|
+
StaticDb.config.fixture_path
|
|
19
|
+
else
|
|
20
|
+
Rails.root.join(args[:path])
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
StaticDb::Load.new(fixture_path: path).perform
|
|
24
|
+
end
|
|
25
|
+
end
|
data/lib/static_db/version.rb
CHANGED
data/lib/static_db.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: static_db
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Klaus Weidinger
|
|
@@ -9,6 +9,20 @@ bindir: bin
|
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rails
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '7.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '7.0'
|
|
12
26
|
- !ruby/object:Gem::Dependency
|
|
13
27
|
name: bundler
|
|
14
28
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -76,7 +90,11 @@ files:
|
|
|
76
90
|
- LICENSE.txt
|
|
77
91
|
- README.md
|
|
78
92
|
- lib/static_db.rb
|
|
79
|
-
- lib/static_db/
|
|
93
|
+
- lib/static_db/dump.rb
|
|
94
|
+
- lib/static_db/engine.rb
|
|
95
|
+
- lib/static_db/initializer.rb
|
|
96
|
+
- lib/static_db/load.rb
|
|
97
|
+
- lib/static_db/tasks/static.rake
|
|
80
98
|
- lib/static_db/version.rb
|
|
81
99
|
homepage: https://github.com/dunkelziffer/static_db
|
|
82
100
|
licenses:
|
|
@@ -95,7 +113,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
95
113
|
requirements:
|
|
96
114
|
- - ">="
|
|
97
115
|
- !ruby/object:Gem::Version
|
|
98
|
-
version: '2
|
|
116
|
+
version: '3.2'
|
|
99
117
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
118
|
requirements:
|
|
101
119
|
- - ">="
|