data_loader 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,4 +3,5 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  .rvmrc
6
- bin/*
6
+ bin/*
7
+ *.tmproj
@@ -12,6 +12,7 @@ Features:
12
12
  * If table names are unspecified, they will be derived from the file name
13
13
  * Will prefix table names to avoid collisions (it overwrites existing tables)
14
14
  * Can run under a different connection, as defined in your database.yml
15
+ * Appends data structure to `data_loader.textile`. Put this file in version control to see the differences when your source CSV files change.
15
16
 
16
17
  ### Usage
17
18
 
@@ -31,12 +32,12 @@ Features:
31
32
 
32
33
  ### TODO
33
34
 
34
- * Write the column structure for each table to a text file. This file can be stored in Git, so that if the CSV files change, the diff will make it obvious what changed.
35
-
36
35
  * A task to clean up all these temporary tables when we're done.
37
36
 
38
37
  * Post-data load step in Migrator to NULLify 0000-00-00 dates, which is how MySQL reads empty strings in (integers would remain 0).
39
38
 
40
39
  * Broader support for Rubies, Databases, and ORM/tools for building the schema.
41
40
 
41
+ * More options for the log file (txt vs textile, filename).
42
+
42
43
  * Better tests!
@@ -17,18 +17,23 @@
17
17
  # extension to append if no file extension is specified
18
18
  # separator
19
19
  # a comma (,)
20
+ # log
21
+ # true/false
20
22
 
21
23
  module DataLoader
22
24
 
23
25
  class Loader
24
- attr_accessor :folder, :table_prefix, :default_ext, :inspect_rows, :connection, :separator
26
+ attr_accessor :folder, :table_prefix, :default_ext, :inspect_rows, :connection, :separator, :log
25
27
 
26
28
  def initialize(folder = '', separator = ',', table_prefix = 'load', connection = :root)
27
29
  @folder, @separator = folder, separator
28
30
  @table_prefix, @connection = table_prefix, connection
29
31
  @default_ext = 'csv'
30
32
  @inspect_rows = 10
33
+ @log = true
31
34
  yield(self) if block_given?
35
+ @logfile = File.expand_path(File.join(@folder, 'data_loader.textile'))
36
+ puts @logfile
32
37
  end
33
38
 
34
39
  def load(filename, table = nil)
@@ -37,9 +42,42 @@ module DataLoader
37
42
  table = Migrator.derive_table_name(filename) if table.nil?
38
43
  table = [@table_prefix, table].join('_') unless @table_prefix.blank?
39
44
  columns = Inspector.inspect_file(full_file, @separator, @inspect_rows)
45
+ log_columns(table, columns)
40
46
  Migrator.migrate(full_file, columns, table, @separator, @connection)
41
47
  table
42
48
  end
49
+
50
+ def log(text)
51
+ return unless @log
52
+
53
+ File.open(@logfile, 'a') do |file|
54
+ file << text
55
+ end
56
+ end
57
+
58
+ def clear_log
59
+ FileUtils.remove(@logfile) if File.exist?(@logfile)
60
+ end
61
+
62
+ private
63
+
64
+ def log_columns(table, columns)
65
+ return unless @log
66
+
67
+ File.open(@logfile, 'a') do |file|
68
+ file << "\ntable{width:80%}.\n|_\\2. #{table} |\n" # table header (textile)
69
+ columns.each_with_index do |column, index|
70
+ if index == 0
71
+ file << "|{width:50%}. #{column[:name]} |{width:50%}. :#{column[:type]} |\n" # 50% on first row
72
+ else
73
+ file << "| #{column[:name]} | :#{column[:type]} |\n" # table row (textile)
74
+ end
75
+ end
76
+ end
77
+ end
78
+
43
79
  end
44
-
45
80
  end
81
+
82
+
83
+
@@ -1,3 +1,3 @@
1
1
  module DataLoader
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_loader
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nathan Youngman
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-25 00:00:00 -06:00
18
+ date: 2011-04-05 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency