declare_schema 1.0.1 → 1.2.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 165053edc0bb071619bb898609cd3e8d489589509de8a386e3cfe97c63efd372
4
- data.tar.gz: '05338b95ee665d86973b61aa36c35189f531660f325c3849890ae38cc356c5b8'
3
+ metadata.gz: 8e34615eda56bac536ac728a8edcddcec4cad606257a3fbb0769024a1837f0dc
4
+ data.tar.gz: e54fb15d2400b5edf651678d4249b75cbeb014cfe4faa4fbfe6558f19af97e45
5
5
  SHA512:
6
- metadata.gz: f84b58499f9d1e5b4e5d5c74af3c4e3cedbbc839076f09d128be9f92df7f7a61354d5de0531804ce3d2a3b6ec8c437a35c5d777cb44b5c1c2eb7d8a28f310095
7
- data.tar.gz: 0b2403c1d17d52cecc73ab7be84729cba4979c34c9e48d1099f0ffaf4c97721f698dbced48e7f70571c476e3139829183fcac6b2957a5cea103b9cd8f49ba845
6
+ metadata.gz: 67cef61d9e6dc3bcdc4d1b8f39cf3f4abf43917df38ec5b0f1503f0c275ae94d27501fd10a654eff5ee4ca4c4105bebc1db7031c77112b562a55be8127269893
7
+ data.tar.gz: c511dc4230e52796249f9f5e260ccdef4c21b963b5564236ee2e601366efcc6823470711bc1116947cf9fe97f6ac1a0ec9d9341eee4fa713192be97aece7c762
data/CHANGELOG.md CHANGED
@@ -4,6 +4,21 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4
4
 
5
5
  Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.2.0] - Unreleased
8
+ ### Added
9
+ - Added a rake task definition that can be optionally included into a non-Rails project to generate
10
+ schema migrations.
11
+
12
+ ## [1.1.0] - 2022-07-22
13
+ ### Changed
14
+ - Fixed a bug where `DeclareSchema::Model::HabtmModelShim` `indexes` and `integer limits` were not being generated properly. Use `limit 8` for ids and primary composite key for habtm model.
15
+ - `index_definitions_with_primary_key`
16
+
17
+ ## [1.0.2] - 2022-07-18
18
+ ### Fixed
19
+ - Fixed a bug where `SchemaChange::ColumnRemove` was not loaded and causing an exception to raise
20
+ when a column was being removed during migration generation
21
+
7
22
  ## [1.0.1] - 2022-07-12
8
23
  ### Fixed
9
24
  - Remove lingering usage of `fallback_find_primary_key` method that was removed in `0.14.0`
@@ -219,6 +234,9 @@ using the appropriate Rails configuration attributes.
219
234
  ### Added
220
235
  - Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
221
236
 
237
+ [1.2.0]: https://github.com/Invoca/declare_schema/compare/v1.1.0...v1.2.0
238
+ [1.1.0]: https://github.com/Invoca/declare_schema/compare/v1.0.2...v1.1.0
239
+ [1.0.2]: https://github.com/Invoca/declare_schema/compare/v1.0.1...v1.0.2
222
240
  [1.0.1]: https://github.com/Invoca/declare_schema/compare/v1.0.0...v1.0.1
223
241
  [1.0.0]: https://github.com/Invoca/declare_schema/compare/v0.14.3...v1.0.0
224
242
  [0.14.3]: https://github.com/Invoca/declare_schema/compare/v0.14.2...v0.14.3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (1.0.1)
4
+ declare_schema (1.2.0.pre.1)
5
5
  rails (>= 5.0)
6
6
 
7
7
  GEM
@@ -36,7 +36,7 @@ module DeclareSchema
36
36
  parent_table: parent_table,
37
37
  foreign_key: foreign_key
38
38
  }
39
- options[:dependent] = :delete if fkc['ON DELETE CASCADE']
39
+ options[:dependent] = :delete if fkc['ON DELETE CASCADE'] || model.is_a?(DeclareSchema::Model::HabtmModelShim)
40
40
 
41
41
  new(model, foreign_key, **options)
42
42
  end
@@ -37,9 +37,13 @@ module DeclareSchema
37
37
  join_table
38
38
  end
39
39
 
40
+ def index_name
41
+ "index_#{table_name}_on_#{foreign_keys.first}_#{foreign_keys.last}"
42
+ end
43
+
40
44
  def field_specs
41
45
  foreign_keys.each_with_index.each_with_object({}) do |(v, position), result|
42
- result[v] = ::DeclareSchema::Model::FieldSpec.new(self, v, :integer, position: position, null: false)
46
+ result[v] = ::DeclareSchema::Model::FieldSpec.new(self, v, :bigint, position: position, null: false)
43
47
  end
44
48
  end
45
49
 
@@ -53,7 +57,7 @@ module DeclareSchema
53
57
 
54
58
  def index_definitions_with_primary_key
55
59
  [
56
- IndexDefinition.new(self, foreign_keys, unique: true, name: ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME),
60
+ IndexDefinition.new(self, foreign_keys, unique: true, name: index_name), # creates a primary composite key on both foriegn keys
57
61
  IndexDefinition.new(self, foreign_keys.last) # not unique by itself; combines with primary key to be unique
58
62
  ]
59
63
  end
@@ -33,11 +33,14 @@ module DeclareSchema
33
33
 
34
34
  class << self
35
35
  # extract IndexSpecs from an existing table
36
- # always includes the PRIMARY KEY index
36
+ # always includes the PRIMARY KEY index for Model unless it is a HABTM Model.
37
37
  def for_model(model, old_table_name = nil)
38
38
  t = old_table_name || model.table_name
39
39
 
40
- primary_key_columns = Array(model.connection.primary_key(t)).presence or
40
+ habtm_model = model.is_a?(DeclareSchema::Model::HabtmModelShim)
41
+
42
+ primary_key_columns = Array(model.connection.primary_key(t)).presence
43
+ primary_key_columns || habtm_model or
41
44
  raise "could not find primary key for table #{t} in #{model.connection.columns(t).inspect}"
42
45
 
43
46
  primary_key_found = false
@@ -47,14 +50,14 @@ module DeclareSchema
47
50
  i.columns == primary_key_columns && i.unique or
48
51
  raise "primary key on #{t} was not unique on #{primary_key_columns} (was unique=#{i.unique} on #{i.columns})"
49
52
  primary_key_found = true
50
- elsif i.columns == primary_key_columns && i.unique
53
+ elsif i.columns == primary_key_columns && i.unique && !habtm_model
51
54
  # skip this primary key index since we'll create it below, with PRIMARY_KEY_NAME
52
55
  next
53
56
  end
54
57
  new(model, i.columns, name: i.name, unique: i.unique, where: i.where, table_name: old_table_name)
55
58
  end.compact
56
59
 
57
- if !primary_key_found
60
+ if !primary_key_found && !habtm_model
58
61
  index_definitions << new(model, primary_key_columns, name: PRIMARY_KEY_NAME, unique: true, where: nil, table_name: old_table_name)
59
62
  end
60
63
  index_definitions
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "declare_schema"
4
+ require "declare_schema/schema_change/all"
5
+ require "declare_schema/schema_change/column_remove"
6
+ require "generators/declare_schema/migration/migrator"
7
+ require "erb"
8
+
9
+ namespace :declare_schema do
10
+ desc 'Generate migrations for the database schema'
11
+ task :generate => 'db:load_config' do
12
+ up, down = Generators::DeclareSchema::Migration::Migrator.new(renames: {}).generate
13
+
14
+ if up.blank?
15
+ puts "Database and models match -- nothing to change"
16
+ return
17
+ end
18
+
19
+ puts "\n---------- Up Migration ----------"
20
+ puts up
21
+ puts "----------------------------------"
22
+
23
+ puts "\n---------- Down Migration --------"
24
+ puts down
25
+ puts "----------------------------------"
26
+
27
+ final_migration_name = default_migration_name
28
+ migration_template = ERB.new(<<~EOF.chomp)
29
+ # frozen_string_literal: true
30
+ class <%= @migration_class_name %> < (ActiveRecord::Migration[4.2])
31
+ def self.up
32
+ <%= @up.presence or raise "no @up given!" %>
33
+ end
34
+ def self.down
35
+ <%= @down.presence or raise "no @down given!" %>
36
+ end
37
+ end
38
+ EOF
39
+
40
+ @up = " #{up.strip.split("\n").join("\n ")}"
41
+ @down = " #{down.strip.split("\n").join("\n ")}"
42
+ @migration_class_name = final_migration_name.camelize
43
+
44
+ File.write("db/migrate/#{Time.now.to_i}_#{final_migration_name.underscore}.rb", migration_template.result(binding))
45
+ end
46
+ end
47
+
48
+ def default_migration_name
49
+ existing = Dir["db/migrate/*declare_schema_migration*"]
50
+ max = existing.grep(/([0-9]+)\.rb$/) { Regexp.last_match(1).to_i }.max.to_i
51
+ "declare_schema_migration_#{max + 1}"
52
+ end
@@ -7,6 +7,7 @@ module DeclareSchema
7
7
  autoload :Base, 'declare_schema/schema_change/base'
8
8
  autoload :ColumnAdd, 'declare_schema/schema_change/column_add'
9
9
  autoload :ColumnChange, 'declare_schema/schema_change/column_change'
10
+ autoload :ColumnRemove, 'declare_schema/schema_change/column_remove'
10
11
  autoload :ColumnRename, 'declare_schema/schema_change/column_rename'
11
12
  autoload :ForeignKeyAdd, 'declare_schema/schema_change/foreign_key_add'
12
13
  autoload :ForeignKeyRemove, 'declare_schema/schema_change/foreign_key_remove'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "1.0.1"
4
+ VERSION = "1.2.0.pre.1"
5
5
  end
@@ -55,9 +55,10 @@ module Generators
55
55
 
56
56
  def load_rails_models
57
57
  ActiveRecord::Migration.verbose = false
58
-
59
- Rails.application.eager_load!
60
- Rails::Engine.subclasses.each(&:eager_load!)
58
+ if defined?(Rails)
59
+ Rails.application.eager_load!
60
+ Rails::Engine.subclasses.each(&:eager_load!)
61
+ end
61
62
  self.class.before_generating_migration_callback&.call
62
63
  end
63
64
 
@@ -102,14 +102,9 @@ RSpec.describe DeclareSchema::Model::HabtmModelShim do
102
102
  expect(result.size).to eq(2), result.inspect
103
103
 
104
104
  expect(result.first).to be_a(::DeclareSchema::Model::IndexDefinition)
105
- expect(result.first.name).to eq('PRIMARY')
105
+ expect(result.first.name).to eq('index_parent_1_parent_2_on_parent_1_id_parent_2_id')
106
106
  expect(result.first.fields).to eq(['parent_1_id', 'parent_2_id'])
107
107
  expect(result.first.unique).to be_truthy
108
-
109
- expect(result.last).to be_a(::DeclareSchema::Model::IndexDefinition)
110
- expect(result.last.name).to eq('on_parent_2_id')
111
- expect(result.last.unique).to be_falsey
112
- expect(result.last.fields).to eq(['parent_2_id'])
113
108
  end
114
109
  end
115
110
 
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: 1.0.1
4
+ version: 1.2.0.pre.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: 2022-07-12 00:00:00.000000000 Z
11
+ date: 2022-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -33,7 +33,6 @@ extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
35
  - ".github/CODEOWNERS"
36
- - ".github/dependabot.yml"
37
36
  - ".github/workflows/declare_schema_build.yml"
38
37
  - ".github/workflows/gem_release.yml"
39
38
  - ".gitignore"
@@ -68,6 +67,7 @@ files:
68
67
  - lib/declare_schema/model/index_definition.rb
69
68
  - lib/declare_schema/model/table_options_definition.rb
70
69
  - lib/declare_schema/railtie.rb
70
+ - lib/declare_schema/rake.rb
71
71
  - lib/declare_schema/schema_change/all.rb
72
72
  - lib/declare_schema/schema_change/base.rb
73
73
  - lib/declare_schema/schema_change/column_add.rb
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  - !ruby/object:Gem::Version
143
143
  version: 1.3.6
144
144
  requirements: []
145
- rubygems_version: 3.3.11
145
+ rubygems_version: 3.1.6
146
146
  signing_key:
147
147
  specification_version: 4
148
148
  summary: Database schema declaration and migration generator for Rails
@@ -1,14 +0,0 @@
1
- version: 2
2
- updates:
3
- - package-ecosystem: bundler
4
- directory: "/"
5
- schedule:
6
- interval: weekly
7
- day: friday
8
- time: "22:00"
9
- timezone: PST8PDT
10
- open-pull-requests-limit: 99
11
- versioning-strategy: lockfile-only
12
- commit-message:
13
- prefix: No-Jira
14
- include: scope