seedgen 0.0.3
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +31 -0
- data/Rakefile +3 -0
- data/lib/seedgen/database/database.rb +15 -0
- data/lib/seedgen/faker.rb +51 -0
- data/lib/seedgen/railtie.rb +7 -0
- data/lib/seedgen/seed_file.rb +7 -0
- data/lib/seedgen/version.rb +3 -0
- data/lib/seedgen.rb +85 -0
- data/lib/tasks/seedgen_tasks.rake +15 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7c656032481489823a64542f0f864d64c8874d6156d1f17664f1770b2e945027
|
4
|
+
data.tar.gz: ff0b902ea7ef8a597732a957d214fa6f80d0a40b1993da7b7e4c994810336866
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cb9c89738160a21995a42560e070c4ed7ee8a5f3ef9173096217a3cefca5dc006ccdf2048a8d19a2e6baea572af896e892ad81c82d448692826558cb414b57a3
|
7
|
+
data.tar.gz: 988d86cacea68dd32c98415268f1f271fb8c5f5c3e863d44dace229ae02f25fb962b19d01aae5984183bd2e9669ccc1f728fd095a51213601fcb2387371411cb
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright Austin Wasson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# SeedGen
|
2
|
+
Generate seed files based on your schema.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem "seedgen", "~> 0.0.3"
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
SeedGen hooks in to your models and their relationships to build you a seed file. After installation,
|
13
|
+
run:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
SEED=1 rails db:schema:load
|
17
|
+
```
|
18
|
+
|
19
|
+
The above command generates `db/seedgen.rb` and seeds your database with the output of this new file based on your schema/models.
|
20
|
+
|
21
|
+
Note: You may notice SeedGen missing some of your models. If this is the case, it is because
|
22
|
+
your application needs to eager load its classes for SeedGen to see them.
|
23
|
+
In `config/application.rb`, add:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
config.after_initialize do
|
27
|
+
Rails.application.eager_load!
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
and rerun `SEED=1 rails db:schema:load`.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module SeedGen
|
2
|
+
module Database
|
3
|
+
def self.adapter
|
4
|
+
@adapter ||= ActiveRecord::Base.connection.adapter_name
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.models
|
8
|
+
ApplicationRecord.descendants
|
9
|
+
end
|
10
|
+
|
11
|
+
module MySQL; end
|
12
|
+
module PostgreSQL; end
|
13
|
+
module SQLite; end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'faker'
|
2
|
+
|
3
|
+
module SeedGen
|
4
|
+
module FakerData
|
5
|
+
class InvalidAttributeType < StandardError; end
|
6
|
+
|
7
|
+
def self.generate(model, column)
|
8
|
+
case column.sql_type_metadata.type
|
9
|
+
when :binary
|
10
|
+
Faker::Number.binary
|
11
|
+
when :boolean
|
12
|
+
# TODO
|
13
|
+
true
|
14
|
+
when :date
|
15
|
+
# TODO
|
16
|
+
Faker::Date.random
|
17
|
+
when :datetime
|
18
|
+
Faker::Date.random
|
19
|
+
when :decimal
|
20
|
+
Faker::Number.decimal
|
21
|
+
when :float
|
22
|
+
3.03
|
23
|
+
when :integer
|
24
|
+
# TODO
|
25
|
+
if enum?(model, column.name)
|
26
|
+
range = model.defined_enums[column.name].length - 1
|
27
|
+
return Faker::Number.between(from: 0, to: range)
|
28
|
+
end
|
29
|
+
Faker::Number.number(digits: 4)
|
30
|
+
when :bigint
|
31
|
+
Faker::Number.number(digits: 10)
|
32
|
+
when :string
|
33
|
+
Faker::Alphanumeric.alpha(number: 7)
|
34
|
+
when :text
|
35
|
+
Faker::Company.bs
|
36
|
+
when :time
|
37
|
+
# TODO
|
38
|
+
Faker::Date.time
|
39
|
+
when :timestamp
|
40
|
+
# TODO
|
41
|
+
Faker::Date.time
|
42
|
+
else
|
43
|
+
raise InvalidAttributeType
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.enum?(model, column)
|
48
|
+
model.defined_enums.key?(column)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/seedgen.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require "seedgen/database/database"
|
2
|
+
require "seedgen/faker"
|
3
|
+
require "seedgen/railtie"
|
4
|
+
require "seedgen/seed_file"
|
5
|
+
require "seedgen/version"
|
6
|
+
|
7
|
+
module SeedGen
|
8
|
+
SKIP_ATTRS = %w[ id created_at updated_at ]
|
9
|
+
|
10
|
+
def self.run
|
11
|
+
@models = Database.models || []
|
12
|
+
@seeded_models = []
|
13
|
+
|
14
|
+
@models.each do |model|
|
15
|
+
seed(model)
|
16
|
+
@seeded_models << model
|
17
|
+
end
|
18
|
+
|
19
|
+
puts database_seeded?
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.seed(model)
|
23
|
+
parents = parents(model)
|
24
|
+
if parents.empty?
|
25
|
+
create_record(model)
|
26
|
+
else
|
27
|
+
parents.each do |parent|
|
28
|
+
seed(parent)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# returns parents that have not been persisted
|
34
|
+
# TODO: refactor with filter
|
35
|
+
# Ignore this awful code 🤮
|
36
|
+
def self.parents(model)
|
37
|
+
parents = []
|
38
|
+
model.reflect_on_all_associations(:belongs_to) do |assoc|
|
39
|
+
parents << assoc.name.to_s.capitalize
|
40
|
+
end
|
41
|
+
nonpersisted_parents = []
|
42
|
+
|
43
|
+
parents.each do |parent|
|
44
|
+
next if parent.active_record.count > 0
|
45
|
+
nonpersisted_parents << parent.active_record
|
46
|
+
end
|
47
|
+
nonpersisted_parents
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.create_record(model)
|
51
|
+
puts "===================="
|
52
|
+
puts "Creating: #{model}"
|
53
|
+
attributes = attrs(model)
|
54
|
+
parents = []
|
55
|
+
model.reflect_on_all_associations(:belongs_to).each do |assoc|
|
56
|
+
parents << assoc.name.to_s
|
57
|
+
end
|
58
|
+
|
59
|
+
unless parents.empty?
|
60
|
+
parents.each do |parent|
|
61
|
+
attributes.merge!({ parent.to_sym => Object.const_get(parent.capitalize).first })
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
model.create!(attributes)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.attrs(model)
|
69
|
+
data = {}
|
70
|
+
|
71
|
+
columns = model.content_columns
|
72
|
+
columns.each do |column|
|
73
|
+
unless SKIP_ATTRS.include?(column.name)
|
74
|
+
data[column.name.to_sym] = FakerData.generate(model, column)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
data
|
79
|
+
end
|
80
|
+
|
81
|
+
# TODO: validate that all records have been created
|
82
|
+
def self.database_seeded?
|
83
|
+
@models == @seeded_models
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "seedgen"
|
2
|
+
|
3
|
+
namespace :seedgen do
|
4
|
+
desc "Seed database"
|
5
|
+
task :seed do
|
6
|
+
if ENV["SEED"] == "1"
|
7
|
+
# SeedGen.models
|
8
|
+
SeedGen.run
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
Rake.application["db:schema:load"].enhance do
|
13
|
+
Rake::Task["seedgen:seed"].invoke
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: seedgen
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Austin Wasson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-01-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faker
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '7.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '7.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- austinpwasson@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/seedgen.rb
|
52
|
+
- lib/seedgen/database/database.rb
|
53
|
+
- lib/seedgen/faker.rb
|
54
|
+
- lib/seedgen/railtie.rb
|
55
|
+
- lib/seedgen/seed_file.rb
|
56
|
+
- lib/seedgen/version.rb
|
57
|
+
- lib/tasks/seedgen_tasks.rake
|
58
|
+
homepage: https://github.com/wassson/seedgen.git
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata:
|
62
|
+
homepage_uri: https://github.com/wassson/seedgen.git
|
63
|
+
source_code_uri: https://github.com/wassson/seedgen.git
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubygems_version: 3.5.3
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Generate seed files from your schema.
|
83
|
+
test_files: []
|