ez 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/README.md +7 -7
 - data/lib/ez/domain_modeler.rb +6 -6
 - data/lib/ez/schema_modifier.rb +27 -18
 - data/lib/ez/version.rb +1 -1
 - data/lib/tasks/ez_tasks.rake +16 -0
 - metadata +2 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 33044a33df8727278898d03a7c1caaa6f139e2a2
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 64bfe376ccdc13ec0748e7ba7adefb54d938059d
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 53c36051739a15018e61042a661d05006a21a8f7a583e93e514d10772fd770d990f7edbbd8dce3717776edc7008dd413254289445098326f9c1c39d40cc5d5be
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 97d586b236546fcae7c7ef4ab93b053135ed55f3a175d169be5c1ad1078fa15c49571b434e688bf690f5f24e8e688afd7ef6977bc65cfaefe38bad0306bedd71
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,6 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # EZ
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
            **Version  
     | 
| 
      
 3 
     | 
    
         
            +
            **Version 1.1.0**
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
5 
     | 
    
         
             
            *For educational purposes only.*
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
         @@ -22,13 +22,13 @@ Then: 
     | 
|
| 
       22 
22 
     | 
    
         | 
| 
       23 
23 
     | 
    
         
             
            ## Summary of Best Practices
         
     | 
| 
       24 
24 
     | 
    
         | 
| 
       25 
     | 
    
         
            -
            1.  
     | 
| 
      
 25 
     | 
    
         
            +
            1. Run `rake db:migrate` to initially generate a file named `db/models.yml`.
         
     | 
| 
       26 
26 
     | 
    
         | 
| 
       27 
     | 
    
         
            -
            2. Use  
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
            3. Overall, just use `rake db:migrate` as usual.
         
     | 
| 
      
 27 
     | 
    
         
            +
            2. Use `db/models.yml` to define your schema. Database schema changes are applied directly and triggered automatically in development mode.  (`rake db:migrate` will also trigger the changes).  Foreign-key indexes will be generated automatically.
         
     | 
| 
       30 
28 
     | 
    
         | 
| 
      
 29 
     | 
    
         
            +
            3. You can continue to use traditional Rails migrations for any additional indexes, database constraints, etc.
         
     | 
| 
       31 
30 
     | 
    
         | 
| 
      
 31 
     | 
    
         
            +
            4. Run `rake db:migrate:preview` to do a "dry run" to see what would change based your `db/models.yml` file.
         
     | 
| 
       32 
32 
     | 
    
         | 
| 
       33 
33 
     | 
    
         
             
            ## Features
         
     | 
| 
       34 
34 
     | 
    
         | 
| 
         @@ -36,8 +36,8 @@ Then: 
     | 
|
| 
       36 
36 
     | 
    
         
             
            ### 1. Domain Modeling Enhancements
         
     | 
| 
       37 
37 
     | 
    
         | 
| 
       38 
38 
     | 
    
         
             
            * Enables **instant domain modeling without migrations** by using a file named `db/models.yml`.
         
     | 
| 
       39 
     | 
    
         
            -
            *  
     | 
| 
       40 
     | 
    
         
            -
            *  
     | 
| 
      
 39 
     | 
    
         
            +
            * This gem enhances `db:migrate` to incorporate the `db/models.yml` file automatically.
         
     | 
| 
      
 40 
     | 
    
         
            +
            * Run `rake db:migrate` to initially generate a file named `db/models.yml`.  It will have some self-documenting comments inside of it.
         
     | 
| 
       41 
41 
     | 
    
         
             
            * In development mode, there's no need to ever run `rake db:migrate`! Every browser request will trigger automatic table updates.
         
     | 
| 
       42 
42 
     | 
    
         
             
            * In the rails console, 'reload!' will also trigger table updates.
         
     | 
| 
       43 
43 
     | 
    
         
             
            * If you prefer, just run `rake db:migrate` whenever you modify `db/models.yml`.
         
     | 
    
        data/lib/ez/domain_modeler.rb
    CHANGED
    
    | 
         @@ -54,14 +54,14 @@ module EZ 
     | 
|
| 
       54 
54 
     | 
    
         
             
                  end
         
     | 
| 
       55 
55 
     | 
    
         
             
                end
         
     | 
| 
       56 
56 
     | 
    
         | 
| 
       57 
     | 
    
         
            -
                def self.update_tables(silent = false)
         
     | 
| 
       58 
     | 
    
         
            -
                  self.new.update_tables(silent)
         
     | 
| 
      
 57 
     | 
    
         
            +
                def self.update_tables(silent = false, dry_run = false)
         
     | 
| 
      
 58 
     | 
    
         
            +
                  self.new.update_tables(silent, dry_run)
         
     | 
| 
       59 
59 
     | 
    
         
             
                end
         
     | 
| 
       60 
60 
     | 
    
         | 
| 
       61 
     | 
    
         
            -
                def update_tables(silent = false)
         
     | 
| 
      
 61 
     | 
    
         
            +
                def update_tables(silent = false, dry_run = false)
         
     | 
| 
       62 
62 
     | 
    
         
             
                  return false unless @ok
         
     | 
| 
       63 
63 
     | 
    
         | 
| 
       64 
     | 
    
         
            -
                  SchemaModifier.migrate(@spec, silent)
         
     | 
| 
      
 64 
     | 
    
         
            +
                  SchemaModifier.migrate(@spec, silent, dry_run)
         
     | 
| 
       65 
65 
     | 
    
         
             
                  return true
         
     | 
| 
       66 
66 
     | 
    
         | 
| 
       67 
67 
     | 
    
         
             
                  rescue => e
         
     | 
| 
         @@ -138,8 +138,8 @@ module EZ 
     | 
|
| 
       138 
138 
     | 
    
         | 
| 
       139 
139 
     | 
    
         
             
                  default_column_value = nil
         
     | 
| 
       140 
140 
     | 
    
         
             
                  DEFAULT_VALUE_REGEXES.each { |r| default_column_value = $1 if column_type.sub!(r, '') }
         
     | 
| 
       141 
     | 
    
         
            -
                  default_column_value = default_column_value.to_i if column_type == 'integer'
         
     | 
| 
       142 
     | 
    
         
            -
                  default_column_value = default_column_value.to_f if column_type == 'float'
         
     | 
| 
      
 141 
     | 
    
         
            +
                  default_column_value = default_column_value.to_i if default_column_value.present? && column_type == 'integer'
         
     | 
| 
      
 142 
     | 
    
         
            +
                  default_column_value = default_column_value.to_f if default_column_value.present? && column_type == 'float'
         
     | 
| 
       143 
143 
     | 
    
         | 
| 
       144 
144 
     | 
    
         
             
                  if column_type == 'boolean'
         
     | 
| 
       145 
145 
     | 
    
         
             
                    default_column_value = default_column_value.present? && default_column_value.downcase.strip == 'true'
         
     | 
    
        data/lib/ez/schema_modifier.rb
    CHANGED
    
    | 
         @@ -4,28 +4,38 @@ module EZ 
     | 
|
| 
       4 
4 
     | 
    
         
             
              # and applies any necessary db schema changes.
         
     | 
| 
       5 
5 
     | 
    
         
             
              class SchemaModifier
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
       7 
     | 
    
         
            -
                attr_reader :db, :spec
         
     | 
| 
      
 7 
     | 
    
         
            +
                attr_reader :db, :spec, :dry_run
         
     | 
| 
       8 
8 
     | 
    
         | 
| 
       9 
     | 
    
         
            -
                def initialize(model_spec, silent = false)
         
     | 
| 
      
 9 
     | 
    
         
            +
                def initialize(model_spec, silent = false, dry_run = false)
         
     | 
| 
       10 
10 
     | 
    
         
             
                  @silent = silent
         
     | 
| 
      
 11 
     | 
    
         
            +
                  @dry_run = dry_run
         
     | 
| 
       11 
12 
     | 
    
         
             
                  @spec = model_spec
         
     | 
| 
       12 
13 
     | 
    
         
             
                  connect_to_database
         
     | 
| 
       13 
14 
     | 
    
         
             
                end
         
     | 
| 
       14 
15 
     | 
    
         | 
| 
       15 
     | 
    
         
            -
                def self.migrate(model_spec, silent = false)
         
     | 
| 
       16 
     | 
    
         
            -
                  self.new(model_spec, silent).migrate
         
     | 
| 
      
 16 
     | 
    
         
            +
                def self.migrate(model_spec, silent = false, dry_run = false)
         
     | 
| 
      
 17 
     | 
    
         
            +
                  self.new(model_spec, silent, dry_run).migrate
         
     | 
| 
       17 
18 
     | 
    
         
             
                end
         
     | 
| 
       18 
19 
     | 
    
         | 
| 
       19 
20 
     | 
    
         
             
                def migrate
         
     | 
| 
       20 
21 
     | 
    
         
             
                  @changed = false
         
     | 
| 
       21 
22 
     | 
    
         | 
| 
      
 23 
     | 
    
         
            +
                  if @dry_run
         
     | 
| 
      
 24 
     | 
    
         
            +
                    puts "Previewing... no changes will actually take place..."
         
     | 
| 
      
 25 
     | 
    
         
            +
                    puts
         
     | 
| 
      
 26 
     | 
    
         
            +
                  end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
       22 
28 
     | 
    
         
             
                  add_missing_schema
         
     | 
| 
       23 
29 
     | 
    
         
             
                  remove_dead_schema
         
     | 
| 
       24 
30 
     | 
    
         | 
| 
       25 
31 
     | 
    
         
             
                  if @changed
         
     | 
| 
       26 
     | 
    
         
            -
                    update_schema_version
         
     | 
| 
      
 32 
     | 
    
         
            +
                    update_schema_version unless @dry_run
         
     | 
| 
      
 33 
     | 
    
         
            +
                    puts "\n(No changes were actually made)" if @dry_run
         
     | 
| 
       27 
34 
     | 
    
         
             
                  else
         
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
      
 35 
     | 
    
         
            +
                      puts "All tables are up-to-date."
         
     | 
| 
      
 36 
     | 
    
         
            +
                    if @dry_run
         
     | 
| 
      
 37 
     | 
    
         
            +
                      puts "\nNo changes would be made."
         
     | 
| 
      
 38 
     | 
    
         
            +
                    end
         
     | 
| 
       29 
39 
     | 
    
         
             
                  end
         
     | 
| 
       30 
40 
     | 
    
         | 
| 
       31 
41 
     | 
    
         
             
                  return @changed
         
     | 
| 
         @@ -77,22 +87,23 @@ module EZ 
     | 
|
| 
       77 
87 
     | 
    
         
             
                      if !assume_missing
         
     | 
| 
       78 
88 
     | 
    
         
             
                        display_change "Adding new column '#{col_name}' as #{col_type} for model #{model_name}"
         
     | 
| 
       79 
89 
     | 
    
         
             
                      end
         
     | 
| 
       80 
     | 
    
         
            -
                      db.add_column(table_name, col_name.to_sym, col_type.to_sym, default: col_default)
         
     | 
| 
      
 90 
     | 
    
         
            +
                      db.add_column(table_name, col_name.to_sym, col_type.to_sym, default: col_default)  unless @dry_run
         
     | 
| 
       81 
91 
     | 
    
         
             
                      if data[:index]
         
     | 
| 
       82 
92 
     | 
    
         
             
                        display_change "  (adding database index for '#{col_name}')"
         
     | 
| 
       83 
     | 
    
         
            -
                        db.add_index table_name, col_name.to_sym
         
     | 
| 
      
 93 
     | 
    
         
            +
                        db.add_index table_name, col_name.to_sym unless @dry_run
         
     | 
| 
       84 
94 
     | 
    
         
             
                      end
         
     | 
| 
       85 
95 
     | 
    
         
             
                    else
         
     | 
| 
       86 
96 
     | 
    
         
             
                      if db_col.type != col_type
         
     | 
| 
       87 
97 
     | 
    
         
             
                        display_change "Changing column type for #{col_name} to #{col_type} for model #{model_name}"
         
     | 
| 
       88 
98 
     | 
    
         
             
                      end
         
     | 
| 
       89 
99 
     | 
    
         | 
| 
      
 100 
     | 
    
         
            +
                      # puts "#{table_name} #{col_name}: #{db_col.default} and #{col_default}"
         
     | 
| 
       90 
101 
     | 
    
         
             
                      if db_col.default != col_default
         
     | 
| 
       91 
     | 
    
         
            -
                        display_change "Applying new default value #{col_default} for #{col_name} for model #{model_name}"
         
     | 
| 
      
 102 
     | 
    
         
            +
                        display_change "Applying new default value #{col_default || 'NULL'} for #{col_name} for model #{model_name}"
         
     | 
| 
       92 
103 
     | 
    
         
             
                      end
         
     | 
| 
       93 
104 
     | 
    
         | 
| 
       94 
105 
     | 
    
         
             
                      if (db_col.type != col_type) || (db_col.default != col_default)
         
     | 
| 
       95 
     | 
    
         
            -
                        db.change_column(table_name, col_name.to_sym, col_type.to_sym, default: col_default)
         
     | 
| 
      
 106 
     | 
    
         
            +
                        db.change_column(table_name, col_name.to_sym, col_type.to_sym, default: col_default)  unless @dry_run
         
     | 
| 
       96 
107 
     | 
    
         
             
                      end
         
     | 
| 
       97 
108 
     | 
    
         
             
                    end
         
     | 
| 
       98 
109 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -101,7 +112,7 @@ module EZ 
     | 
|
| 
       101 
112 
     | 
    
         
             
                def add_model(model_name, columns)
         
     | 
| 
       102 
113 
     | 
    
         
             
                  table_name = model_name.tableize
         
     | 
| 
       103 
114 
     | 
    
         
             
                  display_change "Defining new table for model '#{model_name}'."
         
     | 
| 
       104 
     | 
    
         
            -
                  db.create_table table_name
         
     | 
| 
      
 115 
     | 
    
         
            +
                  db.create_table table_name  unless @dry_run
         
     | 
| 
       105 
116 
     | 
    
         
             
                  add_missing_columns model_name, columns, true
         
     | 
| 
       106 
117 
     | 
    
         
             
                  filename = "app/models/#{model_name.underscore}.rb"
         
     | 
| 
       107 
118 
     | 
    
         
             
                  unless Rails.env.production? || File.exists?(filename)
         
     | 
| 
         @@ -125,13 +136,15 @@ module EZ 
     | 
|
| 
       125 
136 
     | 
    
         
             
                    if @spec.has_key?(model_name)
         
     | 
| 
       126 
137 
     | 
    
         
             
                      db_columns = db.columns(table_name).map { |column| column.name.to_sym } - [:id, :created_at, :updated_at]
         
     | 
| 
       127 
138 
     | 
    
         
             
                      spec_columns = @spec[model_name].keys.map(&:to_sym)
         
     | 
| 
      
 139 
     | 
    
         
            +
                      # puts spec_columns.inspect
         
     | 
| 
       128 
140 
     | 
    
         
             
                      dead_columns = db_columns - spec_columns
         
     | 
| 
       129 
141 
     | 
    
         | 
| 
      
 142 
     | 
    
         
            +
             
     | 
| 
       130 
143 
     | 
    
         
             
                      if dead_columns.any?
         
     | 
| 
       131 
144 
     | 
    
         
             
                        dead_columns.each do |dead_column_name|
         
     | 
| 
       132 
145 
     | 
    
         
             
                          display_change "Removing unused column '#{dead_column_name}' from model '#{model_name}'"
         
     | 
| 
       133 
146 
     | 
    
         
             
                        end
         
     | 
| 
       134 
     | 
    
         
            -
                        db.remove_columns(table_name, *dead_columns)
         
     | 
| 
      
 147 
     | 
    
         
            +
                        db.remove_columns(table_name, *dead_columns)  unless @dry_run
         
     | 
| 
       135 
148 
     | 
    
         
             
                      end
         
     | 
| 
       136 
149 
     | 
    
         
             
                    end
         
     | 
| 
       137 
150 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -149,7 +162,7 @@ module EZ 
     | 
|
| 
       149 
162 
     | 
    
         
             
                  dead_tables.each do |table_name|
         
     | 
| 
       150 
163 
     | 
    
         
             
                    model_name = table_name.classify
         
     | 
| 
       151 
164 
     | 
    
         
             
                    display_change "Dropping table #{table_name}"
         
     | 
| 
       152 
     | 
    
         
            -
                    db.drop_table(table_name)
         
     | 
| 
      
 165 
     | 
    
         
            +
                    db.drop_table(table_name) unless @dry_run
         
     | 
| 
       153 
166 
     | 
    
         
             
                    begin
         
     | 
| 
       154 
167 
     | 
    
         
             
                      filename = "app/models/#{model_name.underscore}.rb"
         
     | 
| 
       155 
168 
     | 
    
         
             
                      code = IO.read(filename)
         
     | 
| 
         @@ -157,7 +170,7 @@ module EZ 
     | 
|
| 
       157 
170 
     | 
    
         | 
| 
       158 
171 
     | 
    
         
             
                      if is_empty
         
     | 
| 
       159 
172 
     | 
    
         
             
                        display_change "Deleting file #{filename}"
         
     | 
| 
       160 
     | 
    
         
            -
                        File.unlink(filename)
         
     | 
| 
      
 173 
     | 
    
         
            +
                        File.unlink(filename) unless @dry_run
         
     | 
| 
       161 
174 
     | 
    
         
             
                      end
         
     | 
| 
       162 
175 
     | 
    
         
             
                    rescue => e
         
     | 
| 
       163 
176 
     | 
    
         
             
                      display_change "Could not delete old model #{model_name.underscore}.rb."
         
     | 
| 
         @@ -165,10 +178,6 @@ module EZ 
     | 
|
| 
       165 
178 
     | 
    
         
             
                  end
         
     | 
| 
       166 
179 
     | 
    
         
             
                end
         
     | 
| 
       167 
180 
     | 
    
         | 
| 
       168 
     | 
    
         
            -
                def silence_migration_output
         
     | 
| 
       169 
     | 
    
         
            -
                  ActiveRecord::Migration.verbose = false
         
     | 
| 
       170 
     | 
    
         
            -
                end
         
     | 
| 
       171 
     | 
    
         
            -
             
     | 
| 
       172 
181 
     | 
    
         
             
                def connect_to_database
         
     | 
| 
       173 
182 
     | 
    
         
             
                  ActiveRecord::Base.establish_connection
         
     | 
| 
       174 
183 
     | 
    
         
             
                  @db = ActiveRecord::Base.connection
         
     | 
    
        data/lib/ez/version.rb
    CHANGED
    
    
    
        data/lib/tasks/ez_tasks.rake
    CHANGED
    
    | 
         @@ -1,3 +1,19 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            namespace :db do
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
              namespace :migrate do
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
                desc "Preview table updates"
         
     | 
| 
      
 6 
     | 
    
         
            +
                task :preview => :environment do
         
     | 
| 
      
 7 
     | 
    
         
            +
                  if File.exists?('db/models.yml')
         
     | 
| 
      
 8 
     | 
    
         
            +
                    EZ::DomainModeler.update_tables(false, true)
         
     | 
| 
      
 9 
     | 
    
         
            +
                  else
         
     | 
| 
      
 10 
     | 
    
         
            +
                    puts "Nothing to preview."
         
     | 
| 
      
 11 
     | 
    
         
            +
                  end
         
     | 
| 
      
 12 
     | 
    
         
            +
                end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
              end
         
     | 
| 
      
 15 
     | 
    
         
            +
            end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
       1 
17 
     | 
    
         
             
            namespace :ez do
         
     | 
| 
       2 
18 
     | 
    
         | 
| 
       3 
19 
     | 
    
         
             
              desc "Generate models.yml if it doesn't exist yet."
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: ez
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 1.1.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Jeff Cohen
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2014-09- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2014-09-05 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: hirb
         
     |