databasion 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010 Brian Jones (Istpika)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,144 @@
1
+ # Databasion
2
+
3
+ ## Google Spreadsheet/Excel -> YAML -> Ruby Migration -> Database Management Tool
4
+
5
+ If we were all part of a hive mind, we wouldn't need management anything. Databases would get built, the correct columns would get used, programmers would align their ORMs, and keeping it all together wouldn't be some kind of management nightmare.
6
+
7
+ Fortunately we aren't a hive mind (and if we were I would be just as confused as I am any other day, our office's main language is Japanese). Still, even though my coworkers can't read my mind, we've come up with a tool that allows top end planners to describe system data in a spreadsheet, programmers to fudge in the column types, sizes, and database relationships, and everyone to export it out into YAML and Ruby migration scripts which update your infrastructure. Ideally the fully automated suite is used in your test environment so your planner can quickly test changes, and a step by step process used if you are updating production machines.
8
+
9
+ ## Requirements
10
+
11
+ ### Ruby
12
+
13
+ * Ruby >= 1.8.7
14
+
15
+ ### Gems
16
+
17
+ * ActiveRecord >= 2.3.5
18
+ * ActiveSupport >= 2.3.5
19
+ * Google Spreadsheet >= 0.1.1
20
+ * Spreadsheet >= 0.6.4.1
21
+
22
+ ## Installation
23
+
24
+ ### Install
25
+
26
+ sudo gem install databasion
27
+
28
+ ## Spreadsheet Conventions
29
+
30
+ None of this would really work if there weren't some conventions in place. The following explains how the worksheet needs to be formatted, how the data spreadsheets themselves needs to be formatted, what fields are required, and what fields can be ignored.
31
+
32
+ At the highest level there needs to be a worksheet named _Database_. This is simply a master list of related spreadsheets, and what database they correspond to (for split table designs). The column names are required.
33
+
34
+ ### Database
35
+
36
+ | spreadsheet | dbname| database| username| password| adapter| host | port| options
37
+ |:------------|:------|:--------|:--------|:--------|:-------|:---------|:----|:-------
38
+ | superheroes | db1 | db_test | dbuser | dbuser | mysql | 127.0.0.1| |
39
+
40
+ The options column currently support's _force_, which tells the database to drop and recreate the table.
41
+
42
+ Next we define the actual table spreadsheets.
43
+
44
+ ### Superheroes
45
+
46
+ | column0 | | |
47
+ |:---------|:------------|:-------------|:-------------------
48
+ | ignore | | |
49
+ | comment | | |
50
+ | table | superheroes | |
51
+ | field | id | name | power
52
+ | type | integer | string, 20 | string, 20, Wimp
53
+ | | 1 | Brian Jones | Ruby Hacker
54
+ | | 2 | Superman | Invincible
55
+ | | 3 | Batman | Rich
56
+
57
+ ### Keywords
58
+
59
+ * ignore - Anything written in this column will cause this column and it's data to be ignored.
60
+ * table - The name of the table, and an optional comma delimited 'false' if the table name should not be auto-pluralized.
61
+ * field - The name of the table column.
62
+ * type - A comma delimited list giving the type of the column (using Ruby migration terms), optional size, and optional default value.
63
+
64
+ Note: If an 'id' column is specified, then it is assumed the id's are supplied by hand. Auto-incrementation is disabled, and 'id' is the primary key.
65
+
66
+ __Ruby Migration Types__
67
+
68
+ * binary
69
+ * boolean
70
+ * date
71
+ * datetime
72
+ * decimal
73
+ * float
74
+ * integer
75
+ * string
76
+ * text
77
+ * time
78
+ * timestamp
79
+
80
+ ### Columns
81
+
82
+ Currently column0 is reserved for keywords and comments.
83
+
84
+ If something besides a keyword is written in column0, that row is ignored and will not be used. This is useful if you need to edit out some data.
85
+
86
+ ### Rows
87
+
88
+ Row0 isn't technically reserved, but should ideally be saved for use with the _ignore_ flag. If any text is written in a column (with the exception of column0), that column will be ignored. This is useful for editing out columns that one doesn't currently want in the database.
89
+
90
+ ## Usage
91
+
92
+ Setup the project space.
93
+
94
+ databasion --create project
95
+ cd project
96
+
97
+ Edit _config/google.yml_. Then run the scripts.
98
+
99
+ databasion --system google
100
+ databasion --migrate
101
+
102
+ Or run them both in order.
103
+
104
+ databasion --system google --migrate
105
+
106
+ You can supply a different config path as well.
107
+
108
+ databasion -s google -m --config config/my.other.config.yml
109
+
110
+ ### YAML Configuration
111
+
112
+ #### Google
113
+
114
+ * _login_: A valid Google username and password.
115
+ * _sheets_: A list of the keys gleaned from the Google Docs URL, and a human readable name.
116
+ * _output_: Where to output the relevant data.
117
+
118
+ ## Roadmap
119
+
120
+ __0.0.2__
121
+
122
+ * Add ability to read existing tables, and make relative alter table migration scripts.
123
+
124
+ __0.0.1__
125
+
126
+ * <del>Write this documentation.</del>
127
+ * <del>Add table name specification.</del>
128
+ * <del>Add database level table relationships.</del>
129
+ * <del>Spit out ActiveRecord Models for migrations.</del>
130
+ * <del>Create Ruby migration script.</del> <del>Still needs polished.</del>
131
+ * <del>Add ability to read and update existing data.</del>
132
+ * <del>Build a command line tool.</del>
133
+ * <del>Add logging.</del>
134
+
135
+ ## Testing
136
+
137
+ Currently Databasion uses the cucumber test suite. Any patches or pull requests must have a corresponding Feature, and all tests must pass. Feature branches get bonus points.
138
+
139
+ ## Author
140
+
141
+ __Brian Jones__ - Server Engineer, [Istpika](http://www.istpika.com)
142
+
143
+ * Work: <brian.jones@istpika.com>
144
+ * Personal: <mojobojo@gmail.com>
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'jeweler'
4
+
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "databasion"
7
+ gem.summary = %Q{A Google Spreadsheet/Excel -> YAML -> Ruby Migration Tool}
8
+ gem.email = "mojobojo@gmail.com"
9
+ gem.homepage = "http://github.com/boj/databasion"
10
+ gem.authors = ["Brian Jones", "Istpika"]
11
+ gem.version = "0.0.1"
12
+
13
+ gem.add_dependency('activerecord', '>= 2.3.5')
14
+ gem.add_dependency('activesupport', '>= 2.3.5')
15
+ gem.add_dependency('google-spreadsheet-ruby', '>= 0.1.1')
16
+ gem.add_dependency('spreadsheet', '>= 0.6.4.1')
17
+ end
18
+
19
+ Jeweler::GemcutterTasks.new
20
+
21
+ Dir['lib/tasks/**/*.rake'].each { |rake| load rake }
22
+
23
+ task :default => :test
data/bin/databasion ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib') unless $:.include?(File.dirname(__FILE__) + '/../lib')
3
+
4
+ require 'databasion'
5
+
6
+ Databasion::Applcize.run
@@ -0,0 +1,13 @@
1
+ login:
2
+ username: fakeuser
3
+ password: fakepass
4
+
5
+ sheets:
6
+ - name: non_existant_spreadsheet
7
+ key: key_google_doc_key
8
+
9
+ output:
10
+ yaml_path: 'data'
11
+ migrations:
12
+ path: 'migrations'
13
+ models: 'models'
@@ -0,0 +1,78 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{databasion}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brian Jones", "Istpika"]
12
+ s.date = %q{2010-05-19}
13
+ s.default_executable = %q{databasion}
14
+ s.email = %q{mojobojo@gmail.com}
15
+ s.executables = ["databasion"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ "LICENSE",
22
+ "README.md",
23
+ "Rakefile",
24
+ "bin/databasion",
25
+ "config/example.google.yml",
26
+ "databasion.gemspec",
27
+ "features/databasion.feature",
28
+ "features/databasion/googlize.feature",
29
+ "features/databasion/migitize.feature",
30
+ "features/databasion/step_definitions/googlize_steps.rb",
31
+ "features/databasion/step_definitions/migitize_steps.rb",
32
+ "features/databasion/step_definitions/yamalize_steps.rb",
33
+ "features/databasion/yamalize.feature",
34
+ "features/step_definitions/databasion_steps.rb",
35
+ "lib/databasion.rb",
36
+ "lib/databasion/applcize.rb",
37
+ "lib/databasion/csvilize.rb",
38
+ "lib/databasion/excelize.rb",
39
+ "lib/databasion/googlize.rb",
40
+ "lib/databasion/migitize.rb",
41
+ "lib/databasion/yamalize.rb",
42
+ "lib/migration_helpers/MIT-LICENSE",
43
+ "lib/migration_helpers/README.markdown",
44
+ "lib/migration_helpers/init.rb",
45
+ "lib/migration_helpers/lib/migration_helper.rb",
46
+ "lib/tasks/databasion.rake",
47
+ "lib/tasks/test.rake",
48
+ "lib/trollop.rb"
49
+ ]
50
+ s.homepage = %q{http://github.com/boj/databasion}
51
+ s.rdoc_options = ["--charset=UTF-8"]
52
+ s.require_paths = ["lib"]
53
+ s.rubygems_version = %q{1.3.7}
54
+ s.summary = %q{A Google Spreadsheet/Excel -> YAML -> Ruby Migration Tool}
55
+
56
+ if s.respond_to? :specification_version then
57
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
58
+ s.specification_version = 3
59
+
60
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
61
+ s.add_runtime_dependency(%q<activerecord>, [">= 2.3.5"])
62
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.5"])
63
+ s.add_runtime_dependency(%q<google-spreadsheet-ruby>, [">= 0.1.1"])
64
+ s.add_runtime_dependency(%q<spreadsheet>, [">= 0.6.4.1"])
65
+ else
66
+ s.add_dependency(%q<activerecord>, [">= 2.3.5"])
67
+ s.add_dependency(%q<activesupport>, [">= 2.3.5"])
68
+ s.add_dependency(%q<google-spreadsheet-ruby>, [">= 0.1.1"])
69
+ s.add_dependency(%q<spreadsheet>, [">= 0.6.4.1"])
70
+ end
71
+ else
72
+ s.add_dependency(%q<activerecord>, [">= 2.3.5"])
73
+ s.add_dependency(%q<activesupport>, [">= 2.3.5"])
74
+ s.add_dependency(%q<google-spreadsheet-ruby>, [">= 0.1.1"])
75
+ s.add_dependency(%q<spreadsheet>, [">= 0.6.4.1"])
76
+ end
77
+ end
78
+
@@ -0,0 +1,13 @@
1
+ Feature: Use Googlize
2
+ In order for a user to access google
3
+ They need to connect first
4
+
5
+ Scenario: Log into Google
6
+ Given a google account
7
+ When Googlize logs in
8
+ Then we should have a GoogleSpreadsheet session
9
+
10
+ Scenario: Run system with config
11
+ Given a google yaml config
12
+ When Googlize starts up
13
+ Then Googlize should not fail
@@ -0,0 +1,8 @@
1
+ Feature: Parse YAML into Ruby migrations
2
+ In order to run Ruby migrations
3
+ We must build the migrations from YAML
4
+
5
+ Scenario: Build Ruby migrations
6
+ Given a complete set of YAML definitions
7
+ When the YAML files are parsed
8
+ Then the result is Ruby migration files
@@ -0,0 +1,32 @@
1
+ require 'lib/databasion'
2
+
3
+ Given /a google account/ do
4
+ @config = YAML.load(File.open('config/google.yml'))
5
+ end
6
+
7
+ When /Googlize logs in/ do
8
+ Databasion::Googlize.config = @config
9
+ Databasion::Googlize.login
10
+ end
11
+
12
+ Then /we should have a GoogleSpreadsheet session/ do
13
+ Databasion::Googlize.session.should be_kind_of(GoogleSpreadsheet::Session)
14
+ end
15
+
16
+ Given /a google yaml config/ do
17
+ @config = YAML.load(File.open('config/google.yml'))
18
+ end
19
+
20
+ When /Googlize starts up/ do
21
+ Databasion::Googlize.config = @config
22
+ begin
23
+ Databasion::Googlize.googlebate
24
+ @error = false
25
+ rescue
26
+ @error = true
27
+ end
28
+ end
29
+
30
+ Then /Googlize should not fail/ do
31
+ @error.should == false
32
+ end
@@ -0,0 +1,26 @@
1
+ require 'lib/databasion'
2
+
3
+ Given /a complete set of YAML definitions/ do
4
+ @config = YAML.load(File.open('config/google.yml'))
5
+
6
+ @parse_data = {
7
+ 'name' => 'mock',
8
+ 'plural' => true,
9
+ 'fields' => ["id", "name", "power"],
10
+ 'types' => ["integer", "string, 20", "string, 40"],
11
+ 'data' => [[1, "Brian Jones", "Super Intelligence"], [2, "Superman", "Invincible"], [3, "Batman", "Strength"]],
12
+ 'ignore_cols' => [2],
13
+ 'connection' => [{'database' => 'moon'}]
14
+ }
15
+ Databasion::Yamalize.yamlbate(@parse_data, @config['output']['yaml_path'])
16
+
17
+ @files = Dir["%s/%s.yml" % [@config['output']['yaml_path'], @parse_data['name']]]
18
+ end
19
+
20
+ When /the YAML files are parsed/ do
21
+ Databasion::Migitize.migrabate(@files, @config)
22
+ end
23
+
24
+ Then /the result is Ruby migration files/ do
25
+ File.exist?("%s/100_%s.rb" % [@config['output']['migrations']['path'], @parse_data['name']]).should == true
26
+ end
@@ -0,0 +1,29 @@
1
+ require 'lib/databasion'
2
+
3
+ Given /a chunk of (.*) data/ do |name|
4
+ @config = YAML.load(File.open('config/google.yml'))
5
+ @parse_data = {
6
+ 'name' => 'mock',
7
+ 'plural' => true,
8
+ 'fields' => ["id", "name", "power"],
9
+ 'types' => ["integer", "string, 20", "string, 40"],
10
+ 'data' => [[1, "Brian Jones", "Super Intelligence"], [2, "Superman", "Invincible"], [3, "Batman", "Strength"]],
11
+ 'ignore_cols' => [2],
12
+ 'connection' => [{'database' => 'moon'}]
13
+ }
14
+ end
15
+
16
+ When /we parse it/ do
17
+ Databasion::Yamalize.yamlbate(@parse_data, @config['output']['yaml_path'])
18
+ end
19
+
20
+ Then /it should create a relevant YAML file/ do
21
+ File.exist?("%s/%s.yml" % [@config['output']['yaml_path'], @parse_data['name']]).should == true
22
+ end
23
+
24
+ And /should contain the correct data/ do
25
+ data = YAML.load(File.open("%s/%s.yml" % [@config['output']['yaml_path'], @parse_data['name']]))
26
+ data.should include('meta')
27
+ data.should include('data')
28
+ data['meta'].should include('connection')
29
+ end
@@ -0,0 +1,9 @@
1
+ Feature: Convert parsed hash into YAML
2
+ In order to convert the higher level into YAML
3
+ We need to process it and write it out to files
4
+
5
+ Scenario: Google passes data
6
+ Given a chunk of google data
7
+ When we parse it
8
+ Then it should create a relevant YAML file
9
+ And should contain the correct data
@@ -0,0 +1,23 @@
1
+ Feature: Run System
2
+ In order for the user to choose what they want to do
3
+ The user should be able to pass in configuration settings
4
+
5
+ Scenario: User calls google databate with config
6
+ Given there is an actual yaml config file for google
7
+ When the user runs databate for google
8
+ Then Databasion should not fail for google
9
+
10
+ Scenario: User calls google databate without config
11
+ Given there is no config file for google
12
+ When the user calls databate without config for google
13
+ Then Databasion should fail for google
14
+
15
+ Scenario: User calls excel databate with config
16
+ Given there is an actual yaml config file for excel
17
+ When the user runs databate for excel
18
+ Then Databasion should not fail for excel
19
+
20
+ Scenario: User calls excel databate without config
21
+ Given there is no config file for excel
22
+ When the user calls databate without config for excel
23
+ Then Databasion should fail for excel
@@ -0,0 +1,35 @@
1
+ require 'lib/databasion'
2
+
3
+ Given /there is an actual yaml config file for (.*)/ do |name|
4
+ @config = 'config/google.yml'
5
+ end
6
+
7
+ When /the user runs databate for (.*)/ do |name|
8
+ begin
9
+ Databasion.databate(name, @config)
10
+ @error = false
11
+ rescue
12
+ @error = true
13
+ end
14
+ end
15
+
16
+ Then /Databasion should not fail for (.*)/ do |name|
17
+ @error.should == false
18
+ end
19
+
20
+ Given /there is no config file for (.*)/ do |name|
21
+ @config = nil
22
+ end
23
+
24
+ When /the user calls databate without config for (.*)/ do |name|
25
+ begin
26
+ Databasion.databate(name, @config)
27
+ @error = false
28
+ rescue
29
+ @error = true
30
+ end
31
+ end
32
+
33
+ Then /Databasion should fail for (.*)/ do |name|
34
+ @error.should == true
35
+ end
@@ -0,0 +1,86 @@
1
+ require 'trollop'
2
+
3
+ module Databasion
4
+
5
+ class Applcize
6
+
7
+ def self.run
8
+ opts = Trollop::options do
9
+ banner <<-EOS
10
+ Databasion - A Google Spreadsheet/Excel -> YAML -> Ruby Migration Database Tool
11
+
12
+ Usage:
13
+ databasion [options]
14
+ where [options] are:
15
+ EOS
16
+ opt :create, "Create a base deploy directory", :type => String
17
+ opt :config, "Path to YAML config. Looks for config/google.yml by default", :type => String
18
+ opt :system, "google, excel, migrate, update", :type => String
19
+ opt :migrate, "Migrate after Googlizing or Excelizing"
20
+ opt :update, "Load parsed YAML into migrated database"
21
+ end
22
+ if opts[:config].nil? and opts[:create].nil?
23
+ config = "config/google.yml"
24
+ puts Dir.pwd
25
+ if File.exist?(Dir.pwd + "/" + config)
26
+ opts[:config] = config
27
+ else
28
+ Trollop::die :config, "A YAML config must be specified"
29
+ end
30
+ end
31
+ Trollop::die :system, "System requires a parameter" if opts[:system].nil? and opts[:create].nil?
32
+
33
+ if opts[:create]
34
+ create_project(opts)
35
+ else
36
+ execute_databasion(opts)
37
+ end
38
+ end
39
+
40
+ def self.execute_databasion(opts)
41
+ if opts[:system] and opts[:config]
42
+ Databasion.databate(opts[:system], opts[:config])
43
+ if opts[:migrate] and opts[:system] != 'migrate'
44
+ Databasion.databate('migrate', opts[:config])
45
+ end
46
+ if opts[:update] and opts[:system] != 'update'
47
+ Databasion.databate('update', opts[:config])
48
+ end
49
+ end
50
+ end
51
+
52
+ def self.create_project(opts)
53
+ dir = Dir.pwd
54
+ if File.exist?(dir + "/" + opts[:create])
55
+ Databasion::LOGGER.info "A directory with the name %s already exists" % opts[:create]
56
+ else
57
+ Databasion::LOGGER.info "Creating new project directory..."
58
+ create_base(dir, opts[:create])
59
+ create_config(dir, opts[:create])
60
+ copy_config(dir, opts[:create])
61
+ Databasion::LOGGER.info "Done."
62
+ end
63
+ end
64
+
65
+ def self.create_base(dir, name)
66
+ path = dir + "/" + name
67
+ FileUtils.mkdir path
68
+ Databasion::LOGGER.info "created: %s" % path
69
+ end
70
+
71
+ def self.create_config(dir, name)
72
+ path = dir + "/" + "%s/config" % name
73
+ FileUtils.mkdir path
74
+ Databasion::LOGGER.info "created: %s" % path
75
+ end
76
+
77
+ def self.copy_config(dir, name)
78
+ base = File.dirname(File.expand_path(__FILE__)) + "/../../" + "config/example.google.yml"
79
+ path = dir + "/" + "%s/config/google.yml" % name
80
+ FileUtils.cp base, path
81
+ Databasion::LOGGER.info "copied: %s" % path
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,11 @@
1
+ module Databasion
2
+
3
+ class Csvilize
4
+
5
+ def self.csvbate
6
+
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,11 @@
1
+ module Databasion
2
+
3
+ class Excelize
4
+
5
+ def self.excelbate
6
+
7
+ end
8
+
9
+ end
10
+
11
+ end