-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zero downtime 2.0 #35
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e626363
ZeroDowntime migration feature v2.0 (using version column)
nattfodd 2e9ee79
Rubocop offences fixed & cleanup
nattfodd 1d64498
Another bunch of offences fixed
nattfodd bc15b11
Another bunch of rubocop offences
nattfodd 069b734
Tests added & new record case fixed
nattfodd 9be2e3d
Rubocop added for development env
nattfodd ead302a
README fix
nattfodd ba15da2
Removed unused require
nattfodd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
inherit_from: .rubocop_todo.yml | ||
|
||
AllCops: | ||
Exclude: | ||
- 'bin/*' | ||
|
||
Style/Documentation: | ||
Enabled: false | ||
|
||
Metrics/MethodLength: | ||
Max: 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
# This configuration was generated by | ||
# `rubocop --auto-gen-config` | ||
# on 2018-04-12 15:40:01 +0300 using RuboCop version 0.54.0. | ||
# The point is for the user to remove these configuration records | ||
# one by one as the offenses are removed from the code base. | ||
# Note that changes in the inspected code, or installation of new | ||
# versions of RuboCop, may require this file to be generated again. | ||
|
||
# Offense count: 16 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyle. | ||
# SupportedStyles: empty_lines, no_empty_lines | ||
Layout/EmptyLinesAroundBlockBody: | ||
Exclude: | ||
- 'spec/transcryptor/active_record/adapter_spec.rb' | ||
- 'spec/transcryptor/active_record/re_encrypt_statement_spec.rb' | ||
- 'spec/transcryptor/data_mapper/adapter_spec.rb' | ||
- 'spec/transcryptor/instance_spec.rb' | ||
- 'spec/transcryptor_spec.rb' | ||
|
||
# Offense count: 1 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment. | ||
Layout/ExtraSpacing: | ||
Exclude: | ||
- 'lib/transcryptor/migration/specify_latest_version.rb' | ||
|
||
# Offense count: 1 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: AllowForAlignment. | ||
Layout/SpaceAroundOperators: | ||
Exclude: | ||
- 'lib/transcryptor/migration/specify_latest_version.rb' | ||
|
||
# Offense count: 2 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. | ||
# SupportedStyles: space, no_space | ||
# SupportedStylesForEmptyBraces: space, no_space | ||
Layout/SpaceBeforeBlockBraces: | ||
Exclude: | ||
- 'spec/transcryptor/abstract_adapter_spec.rb' | ||
|
||
# Offense count: 2 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyle. | ||
# SupportedStyles: require_no_space, require_space | ||
Layout/SpaceInLambdaLiteral: | ||
Exclude: | ||
- 'spec/transcryptor/active_record/re_encrypt_statement_spec.rb' | ||
|
||
# Offense count: 2 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. | ||
# SupportedStyles: space, no_space, compact | ||
# SupportedStylesForEmptyBraces: space, no_space | ||
Layout/SpaceInsideHashLiteralBraces: | ||
Exclude: | ||
- 'lib/transcryptor/active_record/zero_downtime.rb' | ||
|
||
# Offense count: 1 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyle. | ||
# SupportedStyles: space, no_space | ||
Layout/SpaceInsideStringInterpolation: | ||
Exclude: | ||
- 'lib/transcryptor/abstract_adapter.rb' | ||
|
||
# Offense count: 2 | ||
Lint/AmbiguousBlockAssociation: | ||
Exclude: | ||
- 'spec/transcryptor/migration_spec.rb' | ||
|
||
# Offense count: 1 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments. | ||
Lint/UnusedBlockArgument: | ||
Exclude: | ||
- 'spec/transcryptor/instance_spec.rb' | ||
|
||
# Offense count: 12 | ||
# Configuration parameters: CountComments, ExcludedMethods. | ||
Metrics/BlockLength: | ||
Max: 160 | ||
|
||
# Offense count: 1 | ||
# Configuration parameters: CountComments. | ||
Metrics/MethodLength: | ||
Max: 19 | ||
|
||
# Offense count: 2 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods. | ||
# SupportedStyles: line_count_based, semantic, braces_for_chaining | ||
# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object | ||
# FunctionalMethods: let, let!, subject, watch | ||
# IgnoredMethods: lambda, proc, it | ||
Style/BlockDelimiters: | ||
Exclude: | ||
- 'spec/transcryptor/abstract_adapter_spec.rb' | ||
|
||
# Offense count: 10 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyle. | ||
# SupportedStyles: braces, no_braces, context_dependent | ||
Style/BracesAroundHashParameters: | ||
Exclude: | ||
- 'spec/transcryptor/abstract_adapter_spec.rb' | ||
- 'spec/transcryptor/active_record/adapter_spec.rb' | ||
- 'spec/transcryptor/data_mapper/adapter_spec.rb' | ||
- 'spec/transcryptor/data_mapper/re_encrypt_statement_spec.rb' | ||
|
||
# Offense count: 16 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: AutoCorrect, EnforcedStyle. | ||
# SupportedStyles: nested, compact | ||
Style/ClassAndModuleChildren: | ||
Enabled: false | ||
|
||
# Offense count: 1 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyle. | ||
# SupportedStyles: empty, nil, both | ||
Style/EmptyElse: | ||
Exclude: | ||
- 'lib/transcryptor/attr_encrypted/column_names.rb' | ||
|
||
# Offense count: 1 | ||
# Cop supports --auto-correct. | ||
Style/EmptyLambdaParameter: | ||
Exclude: | ||
- 'lib/transcryptor/instance.rb' | ||
|
||
# Offense count: 1 | ||
# Cop supports --auto-correct. | ||
Style/Encoding: | ||
Exclude: | ||
- 'lib/transcryptor.rb' | ||
|
||
# Offense count: 1 | ||
# Cop supports --auto-correct. | ||
Style/ExpandPathArguments: | ||
Exclude: | ||
- 'spec/spec_helper.rb' | ||
|
||
# Offense count: 1 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. | ||
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys | ||
Style/HashSyntax: | ||
Exclude: | ||
- 'Rakefile' | ||
|
||
# Offense count: 2 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: Strict. | ||
Style/NumericLiterals: | ||
MinDigits: 15 | ||
|
||
# Offense count: 1 | ||
# Cop supports --auto-correct. | ||
Style/RedundantSelf: | ||
Exclude: | ||
- 'lib/transcryptor/data_mapper/re_encrypt_statement.rb' | ||
|
||
# Offense count: 1 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyle. | ||
# SupportedStyles: implicit, explicit | ||
Style/RescueStandardError: | ||
Exclude: | ||
- 'spec/transcryptor/instance_spec.rb' | ||
|
||
# Offense count: 9 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. | ||
# SupportedStyles: single_quotes, double_quotes | ||
Style/StringLiterals: | ||
Exclude: | ||
- 'Rakefile' | ||
- 'lib/transcryptor/active_record/adapter.rb' | ||
- 'spec/transcryptor/active_record/adapter_spec.rb' | ||
- 'spec/transcryptor/active_record/re_encrypt_statement_spec.rb' | ||
- 'spec/transcryptor/data_mapper/adapter_spec.rb' | ||
- 'spec/transcryptor/instance_spec.rb' | ||
|
||
# Offense count: 10 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyleForMultiline. | ||
# SupportedStylesForMultiline: comma, consistent_comma, no_comma | ||
Style/TrailingCommaInArguments: | ||
Exclude: | ||
- 'spec/transcryptor/active_record/adapter_spec.rb' | ||
- 'spec/transcryptor/active_record/re_encrypt_statement_spec.rb' | ||
- 'spec/transcryptor/data_mapper/adapter_spec.rb' | ||
- 'spec/transcryptor/data_mapper/re_encrypt_statement_spec.rb' | ||
- 'spec/transcryptor/instance_spec.rb' | ||
|
||
# Offense count: 6 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyleForMultiline. | ||
# SupportedStylesForMultiline: comma, consistent_comma, no_comma | ||
Style/TrailingCommaInArrayLiteral: | ||
Exclude: | ||
- 'spec/transcryptor/active_record/re_encrypt_statement_spec.rb' | ||
|
||
# Offense count: 3 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: EnforcedStyleForMultiline. | ||
# SupportedStylesForMultiline: comma, consistent_comma, no_comma | ||
Style/TrailingCommaInHashLiteral: | ||
Exclude: | ||
- 'lib/transcryptor/instance.rb' | ||
- 'spec/transcryptor/instance_spec.rb' | ||
|
||
# Offense count: 4 | ||
# Cop supports --auto-correct. | ||
# Configuration parameters: MinSize, WordRegex. | ||
# SupportedStyles: percent, brackets | ||
Style/WordArray: | ||
EnforcedStyle: brackets | ||
|
||
# Offense count: 65 | ||
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. | ||
# URISchemes: http, https | ||
Metrics/LineLength: | ||
Max: 175 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'transcryptor/migration/migrate_encrypted_fields' | ||
require 'transcryptor/migration/specify_latest_version' | ||
|
||
module Transcryptor | ||
# Allows ZeroDowntime migration using version columns. | ||
# Usage example: | ||
# Transcryptor.draw do | ||
# define_encryption User, | ||
# field: :ssn, | ||
# options: { | ||
# key: '67c3800d1572d9d964a6ff3bd821ed02', | ||
# algorithm: 'aes-256-gcm' | ||
# }, | ||
# version: 20180401000000 | ||
# | ||
# define_encryption User, | ||
# field: :ssn, | ||
# options: { | ||
# key: '0726c4d149fa59523bc47d592151584b', | ||
# algorithm: 'id-aes192-GCM' | ||
# }, | ||
# version: 20180401000001 | ||
# end | ||
class Migration | ||
class << self | ||
attr_accessor :migrations, :latest_versions | ||
|
||
def draw(&block) | ||
@latest_versions = {} | ||
|
||
instance_eval(&block) | ||
evaluate_latest_versions! | ||
patch_models! | ||
end | ||
|
||
private | ||
|
||
def define_encryption(model_class, | ||
field:, | ||
options:, | ||
version:) | ||
@migrations ||= {} | ||
@migrations[model_class] ||= {} | ||
@migrations[model_class][field] ||= {} | ||
@migrations[model_class][field][version] = options | ||
end | ||
|
||
def evaluate_latest_versions! | ||
@migrations.each do |model_class, fields| | ||
fields.each do |field, versions| | ||
@migrations[model_class][field][:latest_version] ||= | ||
versions.keys.max | ||
end | ||
end | ||
end | ||
|
||
# Generates methods for every field's version: | ||
# user.ssn | ||
# user.ssn_20180401000000 | ||
# user.ssn_20180401000001 | ||
# user.ssn_20180401000002 | ||
def patch_models! | ||
migrations.each do |model_class, fields| | ||
generate_versioned_fields_if_needed!(model_class, fields) | ||
|
||
model_class.instance_eval do | ||
after_find :migrate_encrypted_fields! | ||
after_initialize :specify_latest_version!, if: :new_record? | ||
end | ||
|
||
model_class.class_eval do | ||
include Transcryptor::Migration::MigrateEncryptedFields | ||
include Transcryptor::Migration::SpecifyLatestVersion | ||
end | ||
end | ||
end | ||
|
||
def generate_versioned_fields_if_needed!(model_class, fields) | ||
fields.each do |field, versions| | ||
versions.each do |version, opts| | ||
next if version == :latest_version | ||
|
||
generate_versioned_fields!(model_class, field, version, opts) | ||
end | ||
end | ||
end | ||
|
||
def generate_versioned_fields!(model_class, field, version, opts) | ||
versioned_field = "#{field}_#{version}".to_sym | ||
model_class.instance_eval do | ||
attr_encrypted versioned_field, **opts | ||
end | ||
|
||
curr_field_name = get_field_name(field, model_class) | ||
vers_field_name = get_field_name(versioned_field, model_class) | ||
|
||
redefine_versioned_fields!( | ||
model_class, vers_field_name, curr_field_name | ||
) | ||
end | ||
|
||
def redefine_versioned_fields!(model_class, | ||
vers_field_name, | ||
curr_field_name) | ||
model_class.class_eval do | ||
define_method(vers_field_name) { public_send(curr_field_name) } | ||
|
||
define_method("#{vers_field_name}_iv") do | ||
public_send("#{curr_field_name}_iv") | ||
end | ||
|
||
define_method("#{vers_field_name}_salt") do | ||
public_send("#{curr_field_name}_salt") | ||
end | ||
end | ||
end | ||
|
||
def get_field_name(field, model_class) | ||
opts = model_class.encrypted_attributes[field] | ||
"#{opts[:prefix]}#{field}#{opts[:suffix]}" | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/MethodLength: Method has too many lines. [11/10]