tachyon 0.1.5 → 0.2.0

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: 7e508417f45cc19402918a4a554f8928cd43919d
4
- data.tar.gz: 0f97c7354af65aebbd184ee4f85eb735eb841a01
3
+ metadata.gz: 020224e8c886048d69c0c929aa937d2bb6939dff
4
+ data.tar.gz: bc97180405d1c796355ab0d4fcec8bcefdd43d6b
5
5
  SHA512:
6
- metadata.gz: 4e1a77ae53773d6c6e7d0aaa81765bf8ede459dfca2cd06120402c8314cade4e7f73e33c4e85149913c5e7304a41bc7a630f6f2ce51a69d74a28287686f0f8dc
7
- data.tar.gz: 177c88d41f2809cfd356d4ada6acd192101c28943425d5431d1d337dce1d688c6338a9862ac9ab318a53af9d66bb02d7d7d8dbc63f43fc45c4284ef501386465
6
+ metadata.gz: 1ab76163499e5b2407e07a6f71d4cc2630bb46fcdaa9b4cae843f4480068cdfbcd526ccb4b79946726d1150a08f42674270b9f02753d0a16fc511dc516fca4ac
7
+ data.tar.gz: d34249d947078919f4c76b74ac0cda7bf7ab0d5005fb3b1bd47be35643d8b7c99a1e142c6976808ad70ecf1dde5378e95e08ec285bef00cfa3251181e0677dba
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Tachyon is a simple library designed to insert rows into any DB managed by ActiveRecord as fast as possible. Tachyon does not do validations, and only does minimal typescasting. Tachyon simply gets records into the DB as fast as possible. This is very useful in the case when you need to bulk-insert data for some reason, but it really shouldn't be used to replace your normal ActiveRecord DB operations.
4
4
 
5
+ An ideal use-case for Tachyon (and the reason it was developed) is to dump/insert data from the DB as part of the setup step for large test cases.
6
+
5
7
  ## How fast is it?
6
8
 
7
9
  Tachyon is as fast (or faster) than executing raw SQL via `ActiveRecord::Base.connection`:
@@ -9,22 +11,22 @@ Tachyon is as fast (or faster) than executing raw SQL via `ActiveRecord::Base.co
9
11
  ```
10
12
  Benchmark Results (Inserting 10,000 rows):
11
13
  ------------------------------------------------
12
- User.create(Hash) : 7.72 seconds
13
- Raw SQL (w/ string interpolation) : 1.01 seconds
14
- Tachyon.insert(User, Hash) : 1.00 seconds
14
+ User.create(Hash) : 8.14 seconds
15
+ Raw SQL (w/ string interpolation) : 1.17 seconds
16
+ Tachyon.insert(User, Hash) : 1.03 seconds
15
17
  ```
16
18
 
17
19
  ## Features
18
20
 
19
21
  * As fast (or faster!) than generating SQL via string interpolation, but with a much nicer syntax!
20
22
  * Suppresses duplicate key errors, which makes life easier when doing bulk data imports
21
- * Allows you to dump records from the database in a useful format via `Tachyon.dump_record`
23
+ * Allows you to dump records from the database in a useful format via `Tachyon.dump`
22
24
  * Compatible with MySQL, PostgreSQL and SQLite3
23
25
 
24
26
  ## Caveats
25
27
 
26
28
  * Tachyon does not perform validations
27
- * Tachyon only does minimal typecasting
29
+ * Tachyon only does minimal typecasting (ie: inserting a `Time` object does not work, use `Time.to_s(:db)` instead)
28
30
 
29
31
  ## Typecasting
30
32
 
@@ -32,12 +34,22 @@ Tachyon does extremely minimal typecasting. Integers and Floats are passed throu
32
34
 
33
35
  ## Usage
34
36
 
35
- Simply supply the model class along with a hash of attributes:
37
+ To insert simply supply the model class along with a hash of attributes. It's important to note that the keys of the hash must be symbols, not strings:
36
38
 
37
39
  ```ruby
38
40
  Tachyon.insert(Article, id: 13, title: "Brand new article")
39
41
  ```
40
42
 
43
+ And to dump a record simply call `Tachyon.dump`:
44
+
45
+ ```ruby
46
+ article = Article.find(13)
47
+ Tachyon.dump(article)
48
+ => { id: 13, title: "Brand new article", created_at: "2016-06-30 03:32:49", updated_at: "2016-06-30 03:32:49" }
49
+ ```
50
+
51
+ Note that Tachyon is dumping the data in a way that is directly compatible with the DB, the data won't require much in the way of typecasting to insert it back into the DB.
52
+
41
53
  ## Installation
42
54
 
43
55
  Add this line to your application's Gemfile:
@@ -1,67 +1,70 @@
1
1
  require "tachyon/version"
2
2
 
3
3
  class Tachyon
4
+ class << self
4
5
 
5
- @@sql_cache = {}
6
- @@connection_cache = {}
6
+ @@sql_cache = {}
7
+ @@connection_cache = {}
7
8
 
8
- def self.insert(klass, data)
9
- self.connection_for(klass).execute(self.sql_for(klass, data))
10
- rescue ActiveRecord::RecordNotUnique
11
- # NO OP
12
- end
9
+ def insert(klass, data)
10
+ connection_for(klass).execute(sql_for(klass, data))
11
+ rescue ActiveRecord::RecordNotUnique
12
+ # NO OP
13
+ end
13
14
 
14
- def self.connection_for(klass)
15
- return @@connection_cache[klass] if @@connection_cache.has_key?(klass)
16
- @@connection_cache[klass] = klass.connection
17
- end
15
+ def connection_for(klass)
16
+ return @@connection_cache[klass] if @@connection_cache.has_key?(klass)
17
+ @@connection_cache[klass] = klass.connection
18
+ end
18
19
 
19
- def self.sql_for(klass, data)
20
- self.sql_template_for(klass) % self.quote_data(data)
21
- rescue KeyError => e
22
- raise "Data was not supplied for all columns - " + e.to_s
23
- end
20
+ def sql_for(klass, data)
21
+ sql_template_for(klass) % quote_data(data)
22
+ rescue KeyError => e
23
+ raise "Data was not supplied for all columns - " + e.to_s
24
+ end
24
25
 
25
- def self.sql_template_for(klass)
26
- return @@sql_cache[klass] if @@sql_cache.has_key?(klass)
26
+ def sql_template_for(klass)
27
+ return @@sql_cache[klass] if @@sql_cache.has_key?(klass)
27
28
 
28
- columns = klass.columns.map(&:name)
29
- table_name = klass.table_name
30
- columns_string = columns.map {|x| "`#{x}`" }.join(", ")
31
- values_string = columns.map {|x| "%{#{x}}" }.join(", ")
29
+ columns = klass.columns.map(&:name)
30
+ table_name = klass.table_name
31
+ columns_string = columns.map {|x| "`#{x}`" }.join(", ")
32
+ values_string = columns.map {|x| "%{#{x}}" }.join(", ")
32
33
 
33
- sql = "INSERT INTO `#{table_name}` (#{columns_string}) VALUES (#{values_string})"
34
+ sql = "INSERT INTO `#{table_name}` (#{columns_string}) VALUES (#{values_string})"
34
35
 
35
- @@sql_cache[klass] = sql
36
- end
36
+ @@sql_cache[klass] = sql
37
+ end
37
38
 
38
- def self.quote_data(data)
39
- data.map do |key, value|
40
- [key, self.quote_value(value)]
41
- end.to_h
42
- end
39
+ def quote_data(data)
40
+ data.map do |key, value|
41
+ [key, quote_value(value)]
42
+ end.to_h
43
+ end
43
44
 
44
- def self.quote_value(value)
45
- case value
46
- when String then "'#{value.gsub("'", "''")}'"
47
- when NilClass then "NULL"
48
- else value
45
+ def quote_value(value)
46
+ case value
47
+ when String then "'#{value.gsub("'", "''")}'"
48
+ when NilClass then "NULL"
49
+ else value
50
+ end
49
51
  end
50
- end
51
52
 
52
- def self.dump_record(record)
53
- record.attributes_before_type_cast.map do |key, value|
54
- [key.to_sym, self.dump_attribute(value)]
55
- end.to_h
56
- end
53
+ def dump(record)
54
+ record.attributes_before_type_cast.map do |key, value|
55
+ [key.to_sym, dump_attribute(value)]
56
+ end.to_h
57
+ end
57
58
 
58
- def self.dump_attribute(attribute)
59
- case attribute
60
- when Time then attribute.to_s(:db)
61
- when Date then attribute.to_s(:db)
62
- when TrueClass then 1
63
- when FalseClass then 0
64
- else attribute
59
+ def dump_attribute(attribute)
60
+ case attribute
61
+ when Time then attribute.to_s(:db)
62
+ when Date then attribute.to_s(:db)
63
+ when TrueClass then 1
64
+ when FalseClass then 0
65
+ else attribute
66
+ end
65
67
  end
68
+
66
69
  end
67
70
  end
@@ -1,3 +1,3 @@
1
1
  class Tachyon
2
- VERSION = "0.1.5"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tachyon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Gough