legacy_data 0.1.9 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +88 -0
- data/VERSION +1 -1
- data/examples/README +11 -0
- data/examples/generated/blog_mysql/comment.rb +1 -3
- data/examples/generated/blog_mysql/post.rb +1 -3
- data/examples/generated/blog_mysql/tag.rb +1 -3
- data/examples/generated/blog_sqlite3/comment.rb +1 -3
- data/examples/generated/blog_sqlite3/post.rb +1 -3
- data/examples/generated/blog_sqlite3/tag.rb +1 -3
- data/examples/generated/drupal_mysql/access.rb +1 -1
- data/examples/generated/drupal_mysql/action.rb +1 -2
- data/examples/generated/drupal_mysql/actions_aid.rb +1 -1
- data/examples/generated/drupal_mysql/authmap.rb +1 -1
- data/examples/generated/drupal_mysql/batch.rb +1 -1
- data/examples/generated/drupal_mysql/block.rb +1 -2
- data/examples/generated/drupal_mysql/blocks_role.rb +1 -2
- data/examples/generated/drupal_mysql/box.rb +1 -2
- data/examples/generated/drupal_mysql/cache.rb +1 -1
- data/examples/generated/drupal_mysql/cache_block.rb +1 -1
- data/examples/generated/drupal_mysql/cache_filter.rb +1 -1
- data/examples/generated/drupal_mysql/cache_form.rb +1 -1
- data/examples/generated/drupal_mysql/cache_menu.rb +1 -1
- data/examples/generated/drupal_mysql/cache_page.rb +1 -1
- data/examples/generated/drupal_mysql/cache_update.rb +1 -1
- data/examples/generated/drupal_mysql/comment.rb +1 -2
- data/examples/generated/drupal_mysql/filter.rb +1 -2
- data/examples/generated/drupal_mysql/filter_format.rb +1 -2
- data/examples/generated/drupal_mysql/flood.rb +1 -1
- data/examples/generated/drupal_mysql/history.rb +1 -1
- data/examples/generated/drupal_mysql/menu_custom.rb +1 -1
- data/examples/generated/drupal_mysql/menu_link.rb +1 -2
- data/examples/generated/drupal_mysql/menu_router.rb +1 -1
- data/examples/generated/drupal_mysql/node.rb +1 -1
- data/examples/generated/drupal_mysql/node_access.rb +1 -1
- data/examples/generated/drupal_mysql/node_comment_statistic.rb +1 -2
- data/examples/generated/drupal_mysql/node_counter.rb +1 -1
- data/examples/generated/drupal_mysql/node_revision.rb +1 -2
- data/examples/generated/drupal_mysql/node_type.rb +1 -1
- data/examples/generated/drupal_mysql/permission.rb +1 -1
- data/examples/generated/drupal_mysql/role.rb +1 -1
- data/examples/generated/drupal_mysql/session.rb +1 -2
- data/examples/generated/drupal_mysql/system.rb +1 -1
- data/examples/generated/drupal_mysql/term_data.rb +1 -1
- data/examples/generated/drupal_mysql/term_hierarchy.rb +1 -1
- data/examples/generated/drupal_mysql/term_node.rb +1 -1
- data/examples/generated/drupal_mysql/term_relation.rb +1 -1
- data/examples/generated/drupal_mysql/term_synonym.rb +1 -1
- data/examples/generated/drupal_mysql/uploaded_files.rb +1 -1
- data/examples/generated/drupal_mysql/url_alias.rb +1 -1
- data/examples/generated/drupal_mysql/user.rb +1 -2
- data/examples/generated/drupal_mysql/users_role.rb +1 -2
- data/examples/generated/drupal_mysql/variable.rb +1 -1
- data/examples/generated/drupal_mysql/vocabulary.rb +1 -1
- data/examples/generated/drupal_mysql/vocabulary_node_type.rb +1 -2
- data/examples/generated/drupal_mysql/watchdog.rb +1 -1
- data/examples/generated/j2ee_petstore_mysql/address.rb +1 -1
- data/examples/generated/j2ee_petstore_mysql/category.rb +1 -1
- data/examples/generated/j2ee_petstore_mysql/id_gen.rb +1 -1
- data/examples/generated/j2ee_petstore_mysql/item.rb +1 -1
- data/examples/generated/j2ee_petstore_mysql/product.rb +1 -1
- data/examples/generated/j2ee_petstore_mysql/seller_contact_info.rb +1 -1
- data/examples/generated/j2ee_petstore_mysql/tag.rb +1 -1
- data/examples/generated/j2ee_petstore_mysql/tag_item.rb +0 -1
- data/examples/generated/j2ee_petstore_mysql/ziplocation.rb +1 -1
- data/examples/generated/j2ee_petstore_oracle/address.rb +1 -1
- data/examples/generated/j2ee_petstore_oracle/category.rb +1 -1
- data/examples/generated/j2ee_petstore_oracle/id_gen.rb +1 -1
- data/examples/generated/j2ee_petstore_oracle/item.rb +1 -1
- data/examples/generated/j2ee_petstore_oracle/product.rb +1 -1
- data/examples/generated/j2ee_petstore_oracle/sellercontactinfo.rb +1 -1
- data/examples/generated/j2ee_petstore_oracle/tag.rb +1 -1
- data/examples/generated/j2ee_petstore_oracle/ziplocation.rb +1 -1
- data/examples/generated/j2ee_petstore_sqlite3/address.rb +1 -1
- data/examples/generated/j2ee_petstore_sqlite3/category.rb +1 -1
- data/examples/generated/j2ee_petstore_sqlite3/id_gen.rb +1 -1
- data/examples/generated/j2ee_petstore_sqlite3/item.rb +1 -1
- data/examples/generated/j2ee_petstore_sqlite3/product.rb +1 -1
- data/examples/generated/j2ee_petstore_sqlite3/seller_contact_info.rb +1 -1
- data/examples/generated/j2ee_petstore_sqlite3/tag.rb +1 -1
- data/examples/generated/j2ee_petstore_sqlite3/ziplocation.rb +1 -1
- data/generators/models_from_tables/templates/model.rb +3 -3
- data/legacy_data.gemspec +5 -4
- data/lib/legacy_data/table_class_name_mapper.rb +2 -1
- data/spec/expected/post.rb +1 -3
- metadata +5 -4
- data/README.rdoc +0 -49
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Legacy Data
|
2
|
+
|
3
|
+
Getting started on a Rails project with a large existing database can be daunting. How to you extract all the information that's
|
4
|
+
encoded in the database? Do you have to understand the entire data model before you get started? The `models_from_tables` generator
|
5
|
+
in the `legacy_data` gem can help! This generator looks into your existing database and generates ActiveRecord models based on the
|
6
|
+
information encoded in it.
|
7
|
+
|
8
|
+
|
9
|
+
## How to use it
|
10
|
+
|
11
|
+
- To generate an ActiveRecord model for each table in the database just type
|
12
|
+
|
13
|
+
`script/generate script/generate models_from_tables`
|
14
|
+
|
15
|
+
- If you don't want all tables in the database tell it which table to model
|
16
|
+
|
17
|
+
`script/generate script/generate models_from_tables --table-name comments`
|
18
|
+
|
19
|
+
This uses any foreign_key constraints in the database to spider the database and model the comments table and all associated tables.
|
20
|
+
|
21
|
+
- If you *really* only want the comments table tell it not to follow any foreign_keys
|
22
|
+
|
23
|
+
`script/generate script/generate models_from_tables --table-name comments --skip-associated`
|
24
|
+
|
25
|
+
- If you use [factory girl](http://github.com/thoughtbot/factory_girl) it will generate a simple factory for each model it generates
|
26
|
+
|
27
|
+
`script/generate script/generate models_from_tables --table-name comments --with-factories`
|
28
|
+
|
29
|
+
(You do need to install the plugin `gem install legacy_data` as long as http://gemcutter.org is one of your gem sources)
|
30
|
+
|
31
|
+
### Examples
|
32
|
+
|
33
|
+
Several examples come with the gem source in the [examples](http://github.com/alexrothenberg/legacy_data/tree/master/examples/) folder. These include
|
34
|
+
|
35
|
+
- A simple blog database tested with MySQL and Sqlite3
|
36
|
+
- The Drupal 6.14 database tested with MySQL
|
37
|
+
- The J2EE Petstore example tested with MySQL, Sqlite3 and Oracle
|
38
|
+
|
39
|
+
## What kind of information can it extract from the database?
|
40
|
+
|
41
|
+
### Associations
|
42
|
+
|
43
|
+
If the database contains foreign_key constraints it uses them to build `has_many` or `belongs_to` associations
|
44
|
+
in your ActiveRecord models
|
45
|
+
|
46
|
+
### Validation constraints
|
47
|
+
|
48
|
+
It will generate the following types of validation constraints in your models
|
49
|
+
|
50
|
+
- **validates_uniqueness_of** - For columns where the database has an index that enforces uniqueness
|
51
|
+
- **validates_presence_of** - When the database column is non-nullable
|
52
|
+
- **validates_inclusion_of** - For non-nullable boolean columns and custom constraints with a SQL rule "flag IN ('Y', 'N')"
|
53
|
+
- **validates_numericality_of** - For integer columns (nullable and non-nullable)
|
54
|
+
- **custom validation** - For custom SQL validation rules in the database it puts a placeholder in your model with the original SQL for you to translate into Ruby
|
55
|
+
|
56
|
+
###Non-Rails naming conventions
|
57
|
+
|
58
|
+
Since the database is existing it's likely that it doesn't follow Rails naming conventions. Not to worry as the generator will
|
59
|
+
put the non-standard name into the generated models if it needs to.
|
60
|
+
|
61
|
+
What kinds of non-standard names can it generate?
|
62
|
+
|
63
|
+
Let's look at a sample output
|
64
|
+
|
65
|
+
<pre><code>
|
66
|
+
class Post < ActiveRecord::Base
|
67
|
+
|
68
|
+
set_table_name :tbpost
|
69
|
+
set_primary_key :postid
|
70
|
+
|
71
|
+
# Relationships
|
72
|
+
has_many :comments, :foreign_key => :postid
|
73
|
+
|
74
|
+
# Constraints
|
75
|
+
validates_presence_of :title, :body
|
76
|
+
|
77
|
+
end
|
78
|
+
</code></pre>
|
79
|
+
|
80
|
+
- **Class Names** - It named the model *Post* instead of the Rails convention *Tbpost*. The generator could not do this itself but knowing the conventions will often not apply to legacy databases it pauses after spidering the database giving you a chance to override the table to class name mapping. It generates a yaml file `app/models/table_mappings.yml` where you can verify or change any class name before proceeding to generate the models.
|
81
|
+
- **Table Names** - It overrode the table name since the actual name *tbpost* does not match the Rails naming convention *posts*
|
82
|
+
- **Primary Keys** - It overrode the primary key since the actual column *postid* does not match the Rails naming convention *id*
|
83
|
+
- **Foreign Keys** - It overrode the foreign key on the comment table to be *postid* instead of the Rails naming convention *id*
|
84
|
+
|
85
|
+
|
86
|
+
# Copyright
|
87
|
+
|
88
|
+
Copyright (c) 2009 Alex Rothenberg. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.10
|
data/examples/README
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
This directory contains examples showing what the generator can do.
|
2
|
+
The generator is validated against these examples when its functional test suite is run.
|
3
|
+
|
4
|
+
There are 3 examples
|
5
|
+
|
6
|
+
* A simple blog database tested with MySQL and Sqlite3
|
7
|
+
* The Drupal 6.14 database tested with MySQL
|
8
|
+
* The J2EE Petstore example tested with MySQL, Sqlite3 and Oracle
|
9
|
+
|
10
|
+
They have been tested with MySQL, Sqlite3 and Oracle and the pregenerated output is available in the
|
11
|
+
generated directory with the example and database name part of the subdirectory name.
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class <%= definition.class_name -%> < ActiveRecord::Base
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
<%= " set_table_name #{definition.table_name.downcase.to_sym.inspect}\n" if definition.unconventional_table_name? -%>
|
3
|
+
<%= " set_primary_key #{definition.primary_key.to_sym.inspect}\n" if definition.unconventional_primary_key? -%>
|
4
|
+
|
5
5
|
# Relationships
|
6
6
|
<%= definition.relationships_to_s %>
|
7
7
|
|
data/legacy_data.gemspec
CHANGED
@@ -5,24 +5,25 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{legacy_data}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.10"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alex Rothenberg"]
|
12
|
-
s.date = %q{2009-
|
12
|
+
s.date = %q{2009-11-05}
|
13
13
|
s.description = %q{Create ActiveRecord models from an existing database}
|
14
14
|
s.email = %q{alex@alexrothenberg.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
"README.
|
17
|
+
"README.md"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
22
|
"LICENSE",
|
23
|
-
"README.
|
23
|
+
"README.md",
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
|
+
"examples/README",
|
26
27
|
"examples/blog_migration.rb",
|
27
28
|
"examples/create_j2ee_petstore.sql",
|
28
29
|
"examples/delete_j2ee_petstore.sql",
|
@@ -34,7 +34,8 @@ module LegacyData
|
|
34
34
|
save_dictionary
|
35
35
|
self.class.log <<-MSG
|
36
36
|
Done analyzing the tables.
|
37
|
-
|
37
|
+
Automatic class names written to '#{LegacyData::TableClassNameMapper.dictionary_file_name}'
|
38
|
+
Since the database probably does not follow Rails naming conventions you should take a look at the class names and update them in that file.
|
38
39
|
Once you're done hit <enter> to continue generating the models"
|
39
40
|
MSG
|
40
41
|
gets
|
data/spec/expected/post.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: legacy_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Rothenberg
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-11-05 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -50,14 +50,15 @@ extensions: []
|
|
50
50
|
|
51
51
|
extra_rdoc_files:
|
52
52
|
- LICENSE
|
53
|
-
- README.
|
53
|
+
- README.md
|
54
54
|
files:
|
55
55
|
- .document
|
56
56
|
- .gitignore
|
57
57
|
- LICENSE
|
58
|
-
- README.
|
58
|
+
- README.md
|
59
59
|
- Rakefile
|
60
60
|
- VERSION
|
61
|
+
- examples/README
|
61
62
|
- examples/blog_migration.rb
|
62
63
|
- examples/create_j2ee_petstore.sql
|
63
64
|
- examples/delete_j2ee_petstore.sql
|
data/README.rdoc
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
= legacy_data
|
2
|
-
|
3
|
-
How can you start a rails project on top of a large legacy database ... use the legacy_data plugin.
|
4
|
-
|
5
|
-
What if the database doesn't follow Rails/ActiveRecord naming conventions? ... not a problem
|
6
|
-
What if there are a lot of tables? ... not a problem
|
7
|
-
What about all the information contained in the database already (relationships, constraints, etc) ... they are extracted into your models
|
8
|
-
|
9
|
-
Usage: script/generate models_from_tables [options]
|
10
|
-
table-name [ARG] Only generate models for tables starting with
|
11
|
-
table-naming-convention [ARG]
|
12
|
-
Naming convention for tables in the database. Only the wildcard is used when generating the model name
|
13
|
-
|
14
|
-
Examples:
|
15
|
-
* script/generate script/generate models_from_tables --table-naming-convention=tb*
|
16
|
-
Generate a model for each table in the database. The tables all start with a tb (i.e. table 'tbperson' will generate the model 'person')
|
17
|
-
* script/generate script/generate models_from_tables --table-name tbperson
|
18
|
-
Generate a model for the 'tbperson' table in the database and all associated tables. has_many and belongs_to will be generated in the models
|
19
|
-
|
20
|
-
== Usage
|
21
|
-
|
22
|
-
* Create a new rails project with the admin data plugin
|
23
|
-
rails my_application -m http://gist.github.com/188172.txt
|
24
|
-
* Configure your application to connect to your existing Oracle database. Get the connect connect string from your dba (something like: user/password@server.example.com:1541/sid.world) and edit your config/database.yml
|
25
|
-
development:
|
26
|
-
adapter: oracle_enhanced
|
27
|
-
database: my.oracle.server.com:1541/my_database.world
|
28
|
-
username: user_name
|
29
|
-
password: my_secret_password
|
30
|
-
* Generate models for all existing tables
|
31
|
-
script/generate models_from_tables
|
32
|
-
* Take a look
|
33
|
-
script/server
|
34
|
-
http://localhost:3000/admin_data
|
35
|
-
|
36
|
-
== Note on Patches/Pull Requests
|
37
|
-
|
38
|
-
* Fork the project.
|
39
|
-
* Make your feature addition or bug fix.
|
40
|
-
* Add tests for it. This is important so I don't break it in a
|
41
|
-
future version unintentionally.
|
42
|
-
* Commit, do not mess with rakefile, version, or history.
|
43
|
-
(if you want to have your own version, that is fine but
|
44
|
-
bump version in a commit by itself I can ignore when I pull)
|
45
|
-
* Send me a pull request. Bonus points for topic branches.
|
46
|
-
|
47
|
-
== Copyright
|
48
|
-
|
49
|
-
Copyright (c) 2009 Alex Rothenberg. See LICENSE for details.
|