sequel-from_csv 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/README.md +55 -0
- data/Rakefile +2 -0
- data/lib/sequel/extensions/from_csv.rb +28 -0
- data/lib/sequel/plugins/from_csv.rb +50 -0
- data/sequel-from_csv.gemspec +24 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2828e85c1caf38af3b32a722a4973f5eb7c5ae94
|
4
|
+
data.tar.gz: c1590dbd1f44605cf34d1898a1c331b041e26466
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 88da935334c894c73f53378bb2f78fc551944e55dcb176df5e767e72b7695580ef1ad73200319542cab444a3ecb74f89bc8ecb6c0b1a2f660c117909cafcd26c
|
7
|
+
data.tar.gz: 9af9c728634971508cf77c282a0c19bd611d0413d447da827197b248b17a075c4fa13d2f2351e04483f96d29beb0fed809f8431b282e2996e083a4f022274f58
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Sequel - From CSV
|
2
|
+
|
3
|
+
Provides a simple way to seed and synchronize table data using CSV files.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```bash
|
8
|
+
$ gem install sequel-from_csv
|
9
|
+
```
|
10
|
+
|
11
|
+
Or add this line to your application's Gemfile then execute `bundle install`:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'sequel-from_csv'
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
To seed data for an individual model:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# Load the plugin
|
23
|
+
Sequel::Model.plugin :from_csv
|
24
|
+
|
25
|
+
# Sync an individual model
|
26
|
+
class Country < Sequel::Model; end;
|
27
|
+
Country.seed_from_csv "app/models/country.csv"
|
28
|
+
```
|
29
|
+
|
30
|
+
To seed all models with CSV files present:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# Load the extension
|
34
|
+
Sequel::Database.extension :from_csv
|
35
|
+
|
36
|
+
# Sync all models with CSV files recursively
|
37
|
+
DB.seed_from_csv "app/models/"
|
38
|
+
```
|
39
|
+
## To Do
|
40
|
+
|
41
|
+
- [ ] Add tests
|
42
|
+
- [ ] Remove the `activesupport` dependency
|
43
|
+
- [ ] Allow custom primary key field names
|
44
|
+
- [ ] Remove PostgreSQL-specific code
|
45
|
+
- [ ] Document optional arguments
|
46
|
+
|
47
|
+
## Development
|
48
|
+
|
49
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
50
|
+
|
51
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kenaniah/sequel-from_csv.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Extensions
|
3
|
+
module FromCsv
|
4
|
+
|
5
|
+
# Finds all CSV files in a directory and synchronizes with their respective DB tables
|
6
|
+
def seed_from_csv directory, **opts
|
7
|
+
|
8
|
+
Dir.glob("#{directory}/**/*.csv").each do |filename|
|
9
|
+
|
10
|
+
klass = filename.sub "#{directory}", ""
|
11
|
+
klass = klass[1..-1] if klass.starts_with? "/"
|
12
|
+
klass = klass.split(".")[0]
|
13
|
+
|
14
|
+
model = klass.classify.constantize
|
15
|
+
model.plugin :from_csv
|
16
|
+
model.seed_from_csv filename, **opts
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
nil
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
Database.register_extension(:from_csv, Extensions::FromCsv)
|
28
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Plugins
|
5
|
+
module FromCsv
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
# Synchronizes a table's data with a CSV file
|
10
|
+
def seed_from_csv csv_path, delete: false
|
11
|
+
|
12
|
+
# Read the source CSV file
|
13
|
+
data = CSV.table csv_path
|
14
|
+
|
15
|
+
# Ensure the ID column exists
|
16
|
+
unless data.first[:id]
|
17
|
+
raise "CSV file #{csv_path} must contain an id column"
|
18
|
+
end
|
19
|
+
|
20
|
+
self.db.transaction do
|
21
|
+
|
22
|
+
# UPSERT
|
23
|
+
data.each do |row|
|
24
|
+
row = row.to_h # Convert CSV::Row to Hash
|
25
|
+
self.dataset.insert_conflict(target: :id, update: row).insert row
|
26
|
+
end
|
27
|
+
|
28
|
+
# DELETE old rows
|
29
|
+
if delete
|
30
|
+
self.exclude("id IN ?", data.map{|row| row[:id]}).delete
|
31
|
+
end
|
32
|
+
|
33
|
+
# Update the table's sequence
|
34
|
+
self.db.run <<~SQL
|
35
|
+
SELECT
|
36
|
+
setval(pg_get_serial_sequence('#{self.simple_table}', 'id'), coalesce(max(id), 1), true)
|
37
|
+
FROM
|
38
|
+
#{self.simple_table}
|
39
|
+
SQL
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# lib = File.expand_path('../lib', __FILE__)
|
3
|
+
# $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "sequel-from_csv"
|
7
|
+
s.version = "0.1.0"
|
8
|
+
s.authors = ["Kenaniah Cerny"]
|
9
|
+
s.email = ["kenaniah@gmail.com"]
|
10
|
+
|
11
|
+
s.summary = "A simple way to seed and synchronize table data using CSV files"
|
12
|
+
s.homepage = "https://github.com/kenaniah/sequel-from_csv"
|
13
|
+
|
14
|
+
s.files = `git ls-files -z`.split("\x0").reject do |f|
|
15
|
+
f.match(%r{^(test|spec|bin)/})
|
16
|
+
end
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_dependency "activesupport" # For String#classify and String#constantize
|
20
|
+
s.add_dependency "sequel"
|
21
|
+
|
22
|
+
s.add_development_dependency "bundler", "~> 1.13"
|
23
|
+
s.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-from_csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kenaniah Cerny
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
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: sequel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- kenaniah@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- lib/sequel/extensions/from_csv.rb
|
81
|
+
- lib/sequel/plugins/from_csv.rb
|
82
|
+
- sequel-from_csv.gemspec
|
83
|
+
homepage: https://github.com/kenaniah/sequel-from_csv
|
84
|
+
licenses: []
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.6.8
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: A simple way to seed and synchronize table data using CSV files
|
106
|
+
test_files: []
|