declare_schema 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +59 -2
- data/lib/declare_schema/model/field_spec.rb +3 -2
- data/lib/declare_schema/version.rb +1 -1
- data/lib/generators/declare_schema/migration/migrator.rb +1 -5
- metadata +2 -3
- data/test/doc-only.rdoctest +0 -76
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c1bc236be22c712ca8a65ef4ed294301fc7d2021167dd0e2501066abe30b3ba
|
4
|
+
data.tar.gz: b41b86f51293569be553bab41fc245601ad82f98f4546af8d5d9d46601070fe3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a7b1808736b5f6c1ed69eb88a6afa7a69dbc6fcb014fbd5afb23734abd177348440caf5394ba941e66f6473011fae7543ce5b1e43b81ec8231ab3abfc369627
|
7
|
+
data.tar.gz: b96afd3ecfc86d51a795e43f5b04859d7e45c7869ffcf9f21a6a8a63da05e8499dd03b66e4e951b967f0e4feb8d21c2c0cf6aa88e952e53b3254c12ab541e0eb
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,68 @@
|
|
1
1
|
# DeclareSchema
|
2
2
|
|
3
|
-
Declare your active_record model schemas and have database migrations generated for you!
|
3
|
+
Declare your Rails/active_record model schemas and have database migrations generated for you!
|
4
4
|
|
5
|
+
## Example
|
6
|
+
|
7
|
+
Make a model and declare your schema within a `fields do ... end` block:
|
8
|
+
```ruby
|
9
|
+
class Company < ActiveRecord::Base
|
10
|
+
fields do
|
11
|
+
company_name :string, limit: 100
|
12
|
+
ticker_symbol :string, limit: 4, null: true, index: true, unique: true
|
13
|
+
employee_count :integer
|
14
|
+
comments :text
|
15
|
+
|
16
|
+
timestamps
|
17
|
+
end
|
18
|
+
|
19
|
+
belongs_to :industry
|
20
|
+
end
|
21
|
+
```
|
22
|
+
Then generate the migration:
|
23
|
+
```sh
|
24
|
+
$ rails generate declare_schema:migration
|
25
|
+
|
26
|
+
---------- Up Migration ----------
|
27
|
+
create_table :companies, id: :bigint do |t|
|
28
|
+
t.string :company_name, null: false, limit: 100
|
29
|
+
t.string :ticker_symbol, limit: 4
|
30
|
+
t.integer :employee_count, null: false
|
31
|
+
t.text :comments, null: false
|
32
|
+
t.datetime :created_at
|
33
|
+
t.datetime :updated_at
|
34
|
+
t.integer :industry_id, null: false
|
35
|
+
end
|
36
|
+
add_index :companies, [:ticker_symbol], unique: true, name: 'on_ticker_symbol'
|
37
|
+
add_index :companies, [:industry_id], name: 'on_industry_id'
|
38
|
+
execute "ALTER TABLE companies ADD CONSTRAINT index_companies_on_industry_id FOREIGN KEY index_companies_on_industry_id(industry_id) REFERENCES industries(id) "
|
39
|
+
----------------------------------
|
40
|
+
|
41
|
+
---------- Down Migration --------
|
42
|
+
drop_table :companies
|
43
|
+
----------------------------------
|
44
|
+
|
45
|
+
|
46
|
+
What now: [g]enerate migration, generate and [m]igrate now or [c]ancel? g
|
47
|
+
=> "g"
|
48
|
+
|
49
|
+
Migration filename: [<enter>=declare_schema_migration_1|<custom_name>]: add_company_model
|
50
|
+
```
|
51
|
+
Note that the migration generator is interactive -- it can't tell the difference between renaming something vs. adding one thing and removing another, so sometimes it will ask you to clarify.
|
52
|
+
|
53
|
+
## Installing
|
54
|
+
|
55
|
+
Install the `DeclareSchema` gem directly:
|
56
|
+
```
|
57
|
+
$ gem install declare_schema
|
58
|
+
```
|
59
|
+
or add it to your `bundler` Gemfile:
|
60
|
+
```
|
61
|
+
gem 'declare_schema'
|
62
|
+
```
|
5
63
|
## Testing
|
6
64
|
To run tests:
|
7
65
|
```
|
8
66
|
rake test:prepare_testapp[force]
|
9
67
|
rake test:all < test_responses.txt
|
10
68
|
```
|
11
|
-
(Note: there currently are no unit tests. The above will run the `rdoctests`.)
|
@@ -41,7 +41,8 @@ module DeclareSchema
|
|
41
41
|
|
42
42
|
@model = model
|
43
43
|
@name = name.to_sym
|
44
|
-
|
44
|
+
type.is_a?(Symbol) or raise ArgumentError, "type must be a Symbol; got #{type.inspect}"
|
45
|
+
@type = type
|
45
46
|
position_option = options.delete(:position)
|
46
47
|
@options = options
|
47
48
|
|
@@ -72,7 +73,7 @@ module DeclareSchema
|
|
72
73
|
type
|
73
74
|
else
|
74
75
|
field_class = DeclareSchema.to_class(type)
|
75
|
-
field_class && field_class::COLUMN_TYPE or raise UnknownSqlTypeError, "#{type.inspect} for #{model}
|
76
|
+
field_class && field_class::COLUMN_TYPE or raise UnknownSqlTypeError, "#{type.inspect} for #{model}##{@name}"
|
76
77
|
end
|
77
78
|
end
|
78
79
|
end
|
@@ -295,10 +295,6 @@ module Generators
|
|
295
295
|
down = [undo_changes, undo_renames, undo_drops, undo_creates, undo_index_changes, undo_fk_changes].flatten.reject(&:blank?) * "\n\n"
|
296
296
|
|
297
297
|
[up, down]
|
298
|
-
rescue Exception => ex
|
299
|
-
puts "Caught exception: #{ex}"
|
300
|
-
puts ex.backtrace.join("\n")
|
301
|
-
raise
|
302
298
|
end
|
303
299
|
|
304
300
|
def create_table(model)
|
@@ -473,7 +469,7 @@ module Generators
|
|
473
469
|
return [[], []] if Migrator.disable_indexing
|
474
470
|
|
475
471
|
new_table_name = model.table_name
|
476
|
-
existing_fks = DeclareSchema::Model::ForeignKeySpec.for_model(model, old_table_name)
|
472
|
+
existing_fks = ::DeclareSchema::Model::ForeignKeySpec.for_model(model, old_table_name)
|
477
473
|
model_fks = model.constraint_specs
|
478
474
|
add_fks = model_fks - existing_fks
|
479
475
|
drop_fks = existing_fks - model_fks
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: declare_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Invoca Development adapted from hobo_fields by Tom Locke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-09-
|
11
|
+
date: 2020-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -76,7 +76,6 @@ files:
|
|
76
76
|
- spec/lib/declare_schema/field_declaration_dsl_spec.rb
|
77
77
|
- spec/spec_helper.rb
|
78
78
|
- test/api.rdoctest
|
79
|
-
- test/doc-only.rdoctest
|
80
79
|
- test/generators.rdoctest
|
81
80
|
- test/interactive_primary_key.rdoctest
|
82
81
|
- test/migration_generator.rdoctest
|
data/test/doc-only.rdoctest
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
# DeclareSchema
|
2
|
-
|
3
|
-
## Introduction
|
4
|
-
|
5
|
-
Welcome to DeclareSchema -- a spin-off from HoboFields part of the Hobo project (Hobo not required!).
|
6
|
-
|
7
|
-
**DeclareSchema writes your Rails migrations for you! Your migration writing days are over!**
|
8
|
-
|
9
|
-
All we ask is that you declare your fields in the model. It's still perfectly DRY because you're not having to repeat that in the migration -- DeclareSchema does that for you. In fact, you'll come to love having them there.
|
10
|
-
|
11
|
-
It still has all the benefits of writing your own migrations, for example if you want to add some special code to migrate your old data, you can just edit the generated migration.
|
12
|
-
|
13
|
-
## Example
|
14
|
-
|
15
|
-
First off, pass the `--skip-migration` option when generating your models:
|
16
|
-
|
17
|
-
$ rails generate model blog_post --skip-migration
|
18
|
-
|
19
|
-
Now edit your model as follows:
|
20
|
-
|
21
|
-
class BlogPost < ActiveRecord::Base
|
22
|
-
fields do
|
23
|
-
title :string
|
24
|
-
body :text
|
25
|
-
timestamps
|
26
|
-
end
|
27
|
-
end
|
28
|
-
{: .ruby}
|
29
|
-
|
30
|
-
|
31
|
-
Then, simply run
|
32
|
-
|
33
|
-
$ rails generate declare_schema:migration
|
34
|
-
|
35
|
-
And voila
|
36
|
-
|
37
|
-
---------- Up Migration ----------
|
38
|
-
create_table :blog_posts do |t|
|
39
|
-
t.string :title
|
40
|
-
t.text :body
|
41
|
-
t.datetime :created_at
|
42
|
-
t.datetime :updated_at
|
43
|
-
end
|
44
|
-
----------------------------------
|
45
|
-
|
46
|
-
---------- Down Migration --------
|
47
|
-
drop_table :blog_posts
|
48
|
-
----------------------------------
|
49
|
-
{: .ruby}
|
50
|
-
|
51
|
-
The migration generator has created a migration to change from the schema that is currently in your database, to the schema that your models need. That's really all there is to it. You are now free to simply hack away on your app and run the migration generator every time the database needs to play catch-up.
|
52
|
-
|
53
|
-
Note that the migration generator is interactive -- it can't tell the difference between renaming something vs. adding one thing and removing another, so sometimes it will ask you to clarify. It's a bit picky about what it makes you type in response, because we really don't want you to lose data when someone's amazing twitter distracts you at the wrong moment.
|
54
|
-
|
55
|
-
## Installing
|
56
|
-
|
57
|
-
The simplest and recommended way to install DeclareSchema is as a gem:
|
58
|
-
|
59
|
-
$ gem install declare_schema
|
60
|
-
|
61
|
-
## API
|
62
|
-
|
63
|
-
## Migration Generator Details
|
64
|
-
|
65
|
-
The migration generator doctests provide a lot more detail. They're not really that great as documentation because doctests run in a single irb session, and that doesn't fit well with the concept of a generator. Skip these unless you're really keen to see the details of the migration generator in action
|
66
|
-
|
67
|
-
- [Migration Generator Details](/manual/declare_schema/migration_generator)
|
68
|
-
|
69
|
-
## About Doctests
|
70
|
-
|
71
|
-
DeclareSchema is documented and tested using *doctests*. This is an idea that comes from Python that we've been experimenting with for Hobo. Whenever you see code-blocks that start "`>>`", read them as IRB sessions. The `rdoctest` tool extracts these and runs them to verify they behave as advertised.
|
72
|
-
|
73
|
-
Doctests are a great way to get both documentation and tests from the same source. We're still experimenting with exactly how this all works though, so if the docs seem strange in places, please bear with us!
|
74
|
-
|
75
|
-
You may download rubydoctest via [github](http://www.github.com/tablatom/rubydoctest)
|
76
|
-
|