spanner-translator 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +27 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +79 -0
- data/LICENSE.txt +21 -0
- data/Makefile +12 -0
- data/README.md +116 -0
- data/Rakefile +12 -0
- data/lib/rubocop/ast/class_node.rb +20 -0
- data/lib/rubocop/ast/send_node.rb +49 -0
- data/lib/spanner/translator/Rakefile +11 -0
- data/lib/spanner/translator/cli.rb +66 -0
- data/lib/spanner/translator/configuration.rb +29 -0
- data/lib/spanner/translator/railtie.rb +17 -0
- data/lib/spanner/translator/rules/check/has_any_index.rb +22 -0
- data/lib/spanner/translator/rules/check.rb +13 -0
- data/lib/spanner/translator/rules/translation/add_committed_at_timestamp.rb +24 -0
- data/lib/spanner/translator/rules/translation/add_explicit_primary_key.rb +35 -0
- data/lib/spanner/translator/rules/translation/add_index_options.rb +56 -0
- data/lib/spanner/translator/rules/translation/add_test_case.rb +31 -0
- data/lib/spanner/translator/rules/translation/base_rule.rb +76 -0
- data/lib/spanner/translator/rules/translation/bigint_to_integer.rb +21 -0
- data/lib/spanner/translator/rules/translation/enum_column_to_string_with_constraint.rb +69 -0
- data/lib/spanner/translator/rules/translation/replace_application_record.rb +19 -0
- data/lib/spanner/translator/rules/translation/replace_create_table_args.rb +20 -0
- data/lib/spanner/translator/rules/translation/time_columns_to_time.rb +22 -0
- data/lib/spanner/translator/rules/translation.rb +33 -0
- data/lib/spanner/translator/rules.rb +11 -0
- data/lib/spanner/translator/schema.rb +35 -0
- data/lib/spanner/translator/version.rb +7 -0
- data/lib/spanner/translator.rb +26 -0
- data/sig/spanner/translator.rbs +6 -0
- data/spanner-translator.gemspec +37 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 54175adb11828290cc2c5f7993973130dd9b870b66fcf90748af2de11a3a3ea1
|
4
|
+
data.tar.gz: 5e2a822a94c976fa76f20e86122cbd95961a5616e008e2cfba53510d38e2594c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c8169d285df624c76f63ad577d588b19b85606cd393abdd5f0ebbad229de8c5409e4c2ed0d3fa461df8d18db9b75dc97dabf6169ddea0bc61c43376c1b751551
|
7
|
+
data.tar.gz: 6013cd95da080222aca143982461624971c1c3a22ab71c3deddc791bc85a22f1b00b947fa402aa66a3c66ffa1de139b5f79fec7568431fe2aa654d46e96d5012
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 3.2.2
|
3
|
+
NewCops: enable
|
4
|
+
|
5
|
+
Style/StringLiterals:
|
6
|
+
Enabled: true
|
7
|
+
EnforcedStyle: double_quotes
|
8
|
+
|
9
|
+
Style/StringLiteralsInInterpolation:
|
10
|
+
Enabled: true
|
11
|
+
EnforcedStyle: double_quotes
|
12
|
+
|
13
|
+
# irrelevant after Ruby >=3
|
14
|
+
Style/FrozenStringLiteralComment:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/HashSyntax:
|
18
|
+
Enabled: true
|
19
|
+
EnforcedShorthandSyntax: consistent
|
20
|
+
|
21
|
+
Layout/LineLength:
|
22
|
+
Max: 120
|
23
|
+
|
24
|
+
Metrics/BlockLength:
|
25
|
+
Max: 120
|
26
|
+
Exclude:
|
27
|
+
- 'spec/**/*'
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.3.0
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at abachman@recurly.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in spanner-translator.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# development dependencies
|
9
|
+
gem "debug", ">= 1.0.0"
|
10
|
+
gem "rake", "~> 13.0"
|
11
|
+
gem "rspec", "~> 3.0"
|
12
|
+
gem "rubocop", "~> 1.59.0"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
spanner-translator (0.3.0)
|
5
|
+
rubocop-ast (~> 1.30.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.2)
|
11
|
+
debug (1.8.0)
|
12
|
+
irb (>= 1.5.0)
|
13
|
+
reline (>= 0.3.1)
|
14
|
+
diff-lcs (1.5.0)
|
15
|
+
io-console (0.6.0)
|
16
|
+
irb (1.8.1)
|
17
|
+
rdoc
|
18
|
+
reline (>= 0.3.8)
|
19
|
+
json (2.7.1)
|
20
|
+
language_server-protocol (3.17.0.3)
|
21
|
+
parallel (1.24.0)
|
22
|
+
parser (3.3.0.2)
|
23
|
+
ast (~> 2.4.1)
|
24
|
+
racc
|
25
|
+
psych (5.1.0)
|
26
|
+
stringio
|
27
|
+
racc (1.7.1)
|
28
|
+
rainbow (3.1.1)
|
29
|
+
rake (13.0.6)
|
30
|
+
rdoc (6.5.0)
|
31
|
+
psych (>= 4.0.0)
|
32
|
+
regexp_parser (2.9.0)
|
33
|
+
reline (0.3.8)
|
34
|
+
io-console (~> 0.5)
|
35
|
+
rexml (3.2.6)
|
36
|
+
rspec (3.12.0)
|
37
|
+
rspec-core (~> 3.12.0)
|
38
|
+
rspec-expectations (~> 3.12.0)
|
39
|
+
rspec-mocks (~> 3.12.0)
|
40
|
+
rspec-core (3.12.2)
|
41
|
+
rspec-support (~> 3.12.0)
|
42
|
+
rspec-expectations (3.12.3)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.12.0)
|
45
|
+
rspec-mocks (3.12.6)
|
46
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
47
|
+
rspec-support (~> 3.12.0)
|
48
|
+
rspec-support (3.12.1)
|
49
|
+
rubocop (1.59.0)
|
50
|
+
json (~> 2.3)
|
51
|
+
language_server-protocol (>= 3.17.0)
|
52
|
+
parallel (~> 1.10)
|
53
|
+
parser (>= 3.2.2.4)
|
54
|
+
rainbow (>= 2.2.2, < 4.0)
|
55
|
+
regexp_parser (>= 1.8, < 3.0)
|
56
|
+
rexml (>= 3.2.5, < 4.0)
|
57
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
58
|
+
ruby-progressbar (~> 1.7)
|
59
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
60
|
+
rubocop-ast (1.30.0)
|
61
|
+
parser (>= 3.2.1.0)
|
62
|
+
ruby-progressbar (1.13.0)
|
63
|
+
stringio (3.0.8)
|
64
|
+
unicode-display_width (2.5.0)
|
65
|
+
|
66
|
+
PLATFORMS
|
67
|
+
arm64-darwin-21
|
68
|
+
arm64-darwin-23
|
69
|
+
x86_64-linux
|
70
|
+
|
71
|
+
DEPENDENCIES
|
72
|
+
debug (>= 1.0.0)
|
73
|
+
rake (~> 13.0)
|
74
|
+
rspec (~> 3.0)
|
75
|
+
rubocop (~> 1.59.0)
|
76
|
+
spanner-translator!
|
77
|
+
|
78
|
+
BUNDLED WITH
|
79
|
+
2.4.8
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Adam Bachman
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/Makefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# based on https://github.com/recurly/platform-clients-ruby/blob/master/Makefile
|
2
|
+
|
3
|
+
clean:
|
4
|
+
rm -f *.gem
|
5
|
+
|
6
|
+
build: clean
|
7
|
+
gem build spanner-translator.gemspec
|
8
|
+
|
9
|
+
release: build
|
10
|
+
gem push --key github --host https://rubygems.pkg.github.com/recurly spanner-translator-*.gem
|
11
|
+
|
12
|
+
.PHONY: clean build release
|
data/README.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# Spanner::Translator
|
2
|
+
|
3
|
+
A quick-and-dirty [Rubocop::AST](https://github.com/rubocop/rubocop-ast) based `create_table` Ruby source rewriting translator to facilitate migration from MySQL to Google Cloud Spanner.
|
4
|
+
|
5
|
+
Not intended to be a generally-useful gem, but available for use / fork as needed.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add the gem as a `github:` dependency to your Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
group :development do
|
13
|
+
gem 'spanner-translator', github: 'abachman/spanner-translator', branch: 'main', require: false
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
If you're in a Rails project, configure the library and use the `spanner:translate` Rake task:
|
20
|
+
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
# config/initializers/spanner_translator.rb
|
24
|
+
|
25
|
+
if Rails.env.development?
|
26
|
+
require 'spanner/translator'
|
27
|
+
|
28
|
+
Spanner::Translator.configure do |config|
|
29
|
+
config.default_primary_keys = [:related_table_id, :id]
|
30
|
+
config.db_schema = Rails.root.join('db/schema.rb')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
```sh
|
36
|
+
$ bundle exec rake spanner:translate[smalls]
|
37
|
+
```
|
38
|
+
|
39
|
+
Or in any project, call the library from a script:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# frozen_string_literal: true
|
43
|
+
|
44
|
+
require 'spanner/translator'
|
45
|
+
|
46
|
+
Spanner::Translator.configure do |config|
|
47
|
+
config.default_primary_keys = [:id]
|
48
|
+
end
|
49
|
+
|
50
|
+
source = <<~RUBY
|
51
|
+
create_table "smalls", id: { type: :bigint, unsigned: true, default: nil } do |t|
|
52
|
+
t.string "name", null: false
|
53
|
+
t.index ["name"], name: "index_smalls_on_name", order: { name: :asc }
|
54
|
+
end
|
55
|
+
RUBY
|
56
|
+
|
57
|
+
puts Spanner::Translator::CLI.process_code!(source)
|
58
|
+
```
|
59
|
+
|
60
|
+
Or, maybe simplest, use `bundler/inline` to gem install and require the library from a one-off Ruby script that can be run from inside a Rails project directory:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
require 'bundler/inline'
|
64
|
+
gemfile do
|
65
|
+
source 'https://rubygems.org'
|
66
|
+
gem 'spanner-translator', github: 'recurly/spanner-translator', branch: 'main'
|
67
|
+
end
|
68
|
+
|
69
|
+
require 'spanner/translator'
|
70
|
+
Spanner::Translator.configure do |config|
|
71
|
+
# use a composite primary key for every converted table
|
72
|
+
config.default_primary_keys = [:owner_id, :id]
|
73
|
+
# spanner-translator assumes schema exists at ./db/schema.rb
|
74
|
+
end
|
75
|
+
|
76
|
+
tables = %w(users blog_posts comments)
|
77
|
+
|
78
|
+
tables.each do |table_name|
|
79
|
+
# load the corresponding `create_table` statement from db/schema.rb
|
80
|
+
source = Spanner::Translator::Schema.extract_create_table(table_name)
|
81
|
+
|
82
|
+
# print the translated Spanner `create_table` statement
|
83
|
+
puts Spanner::Translator::CLI.process_code!(source)
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
### Safely Rewriting a File
|
88
|
+
|
89
|
+
Rewriting a source file in your project boils down to:
|
90
|
+
|
91
|
+
1. Read file source
|
92
|
+
2. Process source with rules
|
93
|
+
3. Write file
|
94
|
+
4. Run test
|
95
|
+
5. Revert if test fails
|
96
|
+
|
97
|
+
|
98
|
+
## Development
|
99
|
+
|
100
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
101
|
+
|
102
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
103
|
+
|
104
|
+
Run the test suite with `bundle exec rspec`.
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/abachman/spanner-translator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/abachman/spanner-translator/blob/main/CODE_OF_CONDUCT.md).
|
109
|
+
|
110
|
+
## License
|
111
|
+
|
112
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
113
|
+
|
114
|
+
## Code of Conduct
|
115
|
+
|
116
|
+
Everyone interacting in the Spanner::Translator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/abachman/spanner-translator/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module AST
|
5
|
+
# extensions for RuboCop::AST::ClassNode objects
|
6
|
+
class ClassNode
|
7
|
+
def inherits_from?(class_name)
|
8
|
+
return false unless child_nodes[1]&.const_type?
|
9
|
+
|
10
|
+
child_nodes[1].short_name.to_s == class_name.to_s
|
11
|
+
end
|
12
|
+
|
13
|
+
def superclass_loc
|
14
|
+
return unless child_nodes[1]&.const_type?
|
15
|
+
|
16
|
+
child_nodes[1].loc.expression
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module AST
|
5
|
+
# Spanner-specific extensions for RuboCop::AST::SendNode objects
|
6
|
+
class SendNode
|
7
|
+
%i[index column bigint datetime timestamp].each do |t_method_name|
|
8
|
+
define_method(:"t_#{t_method_name}?") do
|
9
|
+
sending_t? && method_name == t_method_name
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def sending_t?
|
14
|
+
receiver && receiver.source == "t"
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_table?
|
18
|
+
method_name == :create_table
|
19
|
+
end
|
20
|
+
|
21
|
+
def find_hash_option(option_name)
|
22
|
+
return unless arguments.last.hash_type?
|
23
|
+
|
24
|
+
option_name = option_name.to_sym
|
25
|
+
arguments.last.children.find do |c|
|
26
|
+
c.pair_type? && c.children[0].sym_type? && c.children[0].children[0] == option_name
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def find_hash_option_source(option_name)
|
31
|
+
find_hash_option(option_name.to_sym)&.source
|
32
|
+
end
|
33
|
+
|
34
|
+
%i[null name unique limit default].each do |option_name|
|
35
|
+
define_method(:"#{option_name}_option") do
|
36
|
+
find_hash_option(option_name)
|
37
|
+
end
|
38
|
+
|
39
|
+
define_method(:"#{option_name}_option_value") do
|
40
|
+
find_hash_option(option_name)&.value&.source
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def last_sibling
|
45
|
+
parent.children.last
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spanner/translator"
|
4
|
+
|
5
|
+
namespace :spanner do
|
6
|
+
desc "Translate Rails MySQL create_table statements to Cloud Spanner"
|
7
|
+
task :translate, [:table] => :environment do |_t, args|
|
8
|
+
source = Spanner::Translator::Schema.extract_create_table(args[:table])
|
9
|
+
puts Spanner::Translator::CLI.process_code!(source)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rubocop-ast"
|
4
|
+
require "spanner/translator/rules"
|
5
|
+
|
6
|
+
module Spanner
|
7
|
+
module Translator
|
8
|
+
# CLI methods for calling the translator
|
9
|
+
module CLI
|
10
|
+
class << self
|
11
|
+
# process code translation, raising errors if a CheckRule fails.
|
12
|
+
def process_code!(code,
|
13
|
+
check_classes = Rules::Check.constants,
|
14
|
+
rule_classes = Rules::Translation::ORDERED_SCHEMA_RULES, options: {})
|
15
|
+
check_classes.each do |rule_class|
|
16
|
+
process_check_rule(Spanner::Translator::Rules::Check.const_get(rule_class), code)
|
17
|
+
end
|
18
|
+
|
19
|
+
process_code(code, rule_classes, options:)
|
20
|
+
end
|
21
|
+
|
22
|
+
# process code translation for only the schema related rules
|
23
|
+
def process_code(code, rule_classes = Rules::Translation::ORDERED_SCHEMA_RULES, options: {})
|
24
|
+
rule_classes.each do |rule_class|
|
25
|
+
code = process_rule(Spanner::Translator::Rules::Translation.const_get(rule_class), code, options:)
|
26
|
+
end
|
27
|
+
return code unless rule_classes == Rules::Translation::ORDERED_SCHEMA_RULES
|
28
|
+
|
29
|
+
formatted_create_table(code)
|
30
|
+
end
|
31
|
+
|
32
|
+
def process_rule(rule_class, code, options: {})
|
33
|
+
source = RuboCop::AST::ProcessedSource.new(code, 2.7)
|
34
|
+
source_buffer = source.buffer
|
35
|
+
rewriter = Parser::Source::TreeRewriter.new(source_buffer)
|
36
|
+
rule = rule_class.new(rewriter, options:)
|
37
|
+
source.ast.each_node { |n| rule.process(n) }
|
38
|
+
rewriter.process
|
39
|
+
end
|
40
|
+
|
41
|
+
def process_check_rule(rule_class, code)
|
42
|
+
source = RuboCop::AST::ProcessedSource.new(code, 2.7)
|
43
|
+
rule = rule_class.new(nil)
|
44
|
+
source.ast.each_node { |n| rule.process(n) }
|
45
|
+
rule.assert!
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# dumb create_table block code formatting
|
51
|
+
def formatted_create_table(code)
|
52
|
+
code.lines.map do |l|
|
53
|
+
case l
|
54
|
+
when /(create_table|end)/
|
55
|
+
l.strip
|
56
|
+
when /t\.index/
|
57
|
+
" #{l.strip}" unless Spanner::Translator.configuration.skip_indexes
|
58
|
+
else
|
59
|
+
" #{l.strip}"
|
60
|
+
end
|
61
|
+
end.compact.join("\n")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spanner
|
4
|
+
module Translator
|
5
|
+
# Configuration
|
6
|
+
class Configuration
|
7
|
+
attr_accessor :rules, :checks, :db_schema, :skip_indexes
|
8
|
+
attr_reader :default_primary_keys
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
reset!
|
12
|
+
end
|
13
|
+
|
14
|
+
def reset!
|
15
|
+
@rules = nil
|
16
|
+
@checks = nil
|
17
|
+
@db_schema = "db/schema.rb"
|
18
|
+
@default_primary_keys = ["id"]
|
19
|
+
@skip_indexes = false
|
20
|
+
end
|
21
|
+
|
22
|
+
# Set primary key column(s), make sure it is an array of symbols
|
23
|
+
def default_primary_keys=(value)
|
24
|
+
@default_primary_keys = value.is_a?(Array) ? value : [value]
|
25
|
+
@default_primary_keys.map!(&:to_sym)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spanner/translator"
|
4
|
+
require "rails"
|
5
|
+
|
6
|
+
module Spanner
|
7
|
+
module Translator
|
8
|
+
# optional railtie to add the gem's rake tasks
|
9
|
+
class Railtie < Rails::Railtie
|
10
|
+
railtie_name :spanner_translator
|
11
|
+
|
12
|
+
rake_tasks do
|
13
|
+
load File.join(File.expand_path(__dir__), "Rakefile")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spanner
|
4
|
+
module Translator
|
5
|
+
module Rules
|
6
|
+
module Check
|
7
|
+
# Check if table has any t.index statement
|
8
|
+
class HasAnyIndex < BaseRule
|
9
|
+
def on_send(node)
|
10
|
+
return unless node.t_index?
|
11
|
+
|
12
|
+
@seen["index"] = true
|
13
|
+
end
|
14
|
+
|
15
|
+
def assert!
|
16
|
+
raise "table has no index" unless any_index?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spanner/translator/rules/translation/base_rule"
|
4
|
+
|
5
|
+
module Spanner
|
6
|
+
module Translator
|
7
|
+
module Rules
|
8
|
+
module Check # rubocop:disable Style/Documentation
|
9
|
+
autoload :HasAnyIndex, "spanner/translator/rules/check/has_any_index"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spanner
|
4
|
+
module Translator
|
5
|
+
module Rules
|
6
|
+
module Translation
|
7
|
+
# Add t.time :committed_at with allow_commit_timestamp: true before the first t.index
|
8
|
+
#
|
9
|
+
# FIXME (@abachman): if table has no indexes, this rule will not be applied
|
10
|
+
#
|
11
|
+
class AddCommittedAtTimestamp < BaseRule
|
12
|
+
def on_send(node)
|
13
|
+
return if @seen["committed_at"]
|
14
|
+
return unless node.t_index?
|
15
|
+
|
16
|
+
committed_at_column = 't.time "committed_at", null: false, allow_commit_timestamp: true'
|
17
|
+
@rewriter.insert_before(node.loc.expression, "#{committed_at_column}\n ")
|
18
|
+
@seen["committed_at"] = true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module Spanner
|
6
|
+
module Translator
|
7
|
+
module Rules
|
8
|
+
module Translation
|
9
|
+
# Based on the `create_table` statement, add an explicit column
|
10
|
+
# creation for the default primary key.
|
11
|
+
#
|
12
|
+
# Scenarios:
|
13
|
+
# create_table "users" do |t|
|
14
|
+
# create_table "users", id: { type: :bigint, unsigned: true, default: nil } do |t|
|
15
|
+
#
|
16
|
+
# Other scenarios should be ignored.
|
17
|
+
#
|
18
|
+
class AddExplicitPrimaryKey < BaseRule
|
19
|
+
def on_send(node)
|
20
|
+
capture_primary_key_argument(node)
|
21
|
+
|
22
|
+
return if @seen["primary_key"]
|
23
|
+
return unless node.sending_t? # we assume every create_table has at least one t. statement
|
24
|
+
return if @primary_key_argument&.value
|
25
|
+
|
26
|
+
primary_key_name = "id"
|
27
|
+
|
28
|
+
@rewriter.insert_before(node.loc.expression, "t.integer \"#{primary_key_name}\", limit: 8, null: false\n ")
|
29
|
+
@seen["primary_key"] = true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module Spanner
|
6
|
+
module Translator
|
7
|
+
module Rules
|
8
|
+
module Translation
|
9
|
+
# - set order: { column_name: :asc, datetime: :desc } for each column on every index
|
10
|
+
# - propagate name: if explicitly set on index
|
11
|
+
# - propagate unique: if explicitly set on index
|
12
|
+
# - add null_filtered: true if any column is nullable and index is unique: true
|
13
|
+
class AddIndexOptions < BaseRule
|
14
|
+
def on_send(node)
|
15
|
+
capture_column_nullity(node)
|
16
|
+
return unless node.t_index?
|
17
|
+
|
18
|
+
@rewriter.replace(node.loc.expression, "t.index #{new_arguments_for_column_definition(node)}")
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def new_arguments_for_column_definition(node)
|
24
|
+
new_arguments = [node.arguments[0].source]
|
25
|
+
conditionally_propagate_argument new_arguments, node, "name"
|
26
|
+
conditionally_propagate_argument new_arguments, node, "unique"
|
27
|
+
new_arguments << order_hash_argument(node)
|
28
|
+
conditionally_add_null_filtered_argument new_arguments, node
|
29
|
+
new_arguments.join(", ")
|
30
|
+
end
|
31
|
+
|
32
|
+
def column_names(node)
|
33
|
+
JSON.parse(node.arguments[0].source)
|
34
|
+
end
|
35
|
+
|
36
|
+
def order_hash_argument(node)
|
37
|
+
order_hash = column_names(node).map do |column_name|
|
38
|
+
"#{column_name}: :#{column_name.match?(/[a-z_]+_at$/) ? "desc" : "asc"}"
|
39
|
+
end
|
40
|
+
"order: { #{order_hash.join(", ")} }"
|
41
|
+
end
|
42
|
+
|
43
|
+
# requires capture_column_nullity to be called
|
44
|
+
def should_null_filter?(node)
|
45
|
+
column_names(node).any? { |c| @column_nullity[c] } &&
|
46
|
+
(node.unique_option && node.unique_option.value.source == "true")
|
47
|
+
end
|
48
|
+
|
49
|
+
def conditionally_add_null_filtered_argument(new_arguments, node)
|
50
|
+
new_arguments << "null_filtered: true" if should_null_filter?(node)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spanner
|
4
|
+
module Translator
|
5
|
+
module Rules
|
6
|
+
module Translation
|
7
|
+
# replace ApplicationRecord with the configured class during translation of models
|
8
|
+
class AddTestCase < BaseRule
|
9
|
+
def_node_matcher :main_describe?, <<~PATTERN
|
10
|
+
(block
|
11
|
+
(send (const nil? :RSpec) :describe ...)
|
12
|
+
...)
|
13
|
+
PATTERN
|
14
|
+
|
15
|
+
def on_block(node)
|
16
|
+
main_describe?(node) do
|
17
|
+
# only rewrite the first time RSpec.describe is seen
|
18
|
+
unless_seen(:insert_test_case) do
|
19
|
+
if translation_options[:test_case]
|
20
|
+
@rewriter.insert_before(node.loc.end, translation_options[:test_case])
|
21
|
+
else
|
22
|
+
@rewriter.insert_before(node.loc.end, ' # i am here\n')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rubocop-ast"
|
4
|
+
require "rubocop/ast/send_node"
|
5
|
+
require "rubocop/ast/class_node"
|
6
|
+
|
7
|
+
module Spanner
|
8
|
+
module Translator
|
9
|
+
module Rules
|
10
|
+
class BaseRule < Parser::AST::Processor # rubocop:disable Style/Documentation
|
11
|
+
include RuboCop::AST::Traversal
|
12
|
+
extend RuboCop::AST::NodePattern::Macros
|
13
|
+
|
14
|
+
attr_reader :translation_options
|
15
|
+
|
16
|
+
# rubocop:disable Lint/MissingSuper
|
17
|
+
def initialize(rewriter, options: {})
|
18
|
+
@rewriter = rewriter
|
19
|
+
@seen = {}
|
20
|
+
@column_nullity = {}
|
21
|
+
@translation_options = options
|
22
|
+
@primary_key_argument = nil
|
23
|
+
@primary_key_properties = nil
|
24
|
+
end
|
25
|
+
# rubocop:enable Lint/MissingSuper
|
26
|
+
|
27
|
+
def create_range(begin_pos, end_pos)
|
28
|
+
Parser::Source::Range.new(@rewriter.source_buffer, begin_pos, end_pos)
|
29
|
+
end
|
30
|
+
|
31
|
+
def capture_table_name(node)
|
32
|
+
@table_name = node.arguments[0].source.tr('"', "") if node.method_name == :create_table
|
33
|
+
end
|
34
|
+
|
35
|
+
def capture_primary_key_argument(node)
|
36
|
+
return unless node.method_name == :create_table
|
37
|
+
|
38
|
+
@primary_key_argument = node.find_hash_option(:primary_key)
|
39
|
+
@primary_key_properties = node.find_hash_option(:id)
|
40
|
+
end
|
41
|
+
|
42
|
+
RAILS_COLUMN_TYPES = %i[bigint string text integer float decimal
|
43
|
+
datetime timestamp time date binary boolean].freeze
|
44
|
+
|
45
|
+
# track every columns' null acceptance
|
46
|
+
def capture_column_nullity(node)
|
47
|
+
is_typed_column_node = node.receiver &&
|
48
|
+
node.receiver.source == "t" &&
|
49
|
+
RAILS_COLUMN_TYPES.include?(node.method_name)
|
50
|
+
return unless is_typed_column_node
|
51
|
+
|
52
|
+
column_name = node.arguments[0].source.tr('"', "")
|
53
|
+
null_option = node.find_hash_option(:null)
|
54
|
+
|
55
|
+
# "may be null" means this column is not defined with explicit "null: false"
|
56
|
+
@column_nullity[column_name] = null_option&.value&.source != "false"
|
57
|
+
end
|
58
|
+
|
59
|
+
def any_index?
|
60
|
+
@seen["index"]
|
61
|
+
end
|
62
|
+
|
63
|
+
def unless_seen(condition)
|
64
|
+
return if @seen[condition]
|
65
|
+
|
66
|
+
yield
|
67
|
+
@seen[condition] = true
|
68
|
+
end
|
69
|
+
|
70
|
+
def conditionally_propagate_argument(arguments, node, name)
|
71
|
+
arguments << node.find_hash_option_source(name) if node.find_hash_option(name)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spanner
|
4
|
+
module Translator
|
5
|
+
module Rules
|
6
|
+
module Translation
|
7
|
+
# t.bigint is not supported in spanner, so replace with t.integer, keeping null option if given
|
8
|
+
class BigintToInteger < BaseRule
|
9
|
+
def on_send(node)
|
10
|
+
return unless node.t_bigint?
|
11
|
+
|
12
|
+
new_arguments = [node.arguments[0].source, "limit: 8"]
|
13
|
+
conditionally_propagate_argument new_arguments, node, "null"
|
14
|
+
|
15
|
+
@rewriter.replace(node.loc.expression, "t.integer #{new_arguments.join(", ")}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module Spanner
|
6
|
+
module Translator
|
7
|
+
module Rules
|
8
|
+
module Translation
|
9
|
+
# replace t.column enums with the appropriate t.string and the correct check constraints
|
10
|
+
class EnumColumnToStringWithConstraint < BaseRule
|
11
|
+
def on_send(node)
|
12
|
+
capture_table_name(node)
|
13
|
+
return unless node.t_column?
|
14
|
+
|
15
|
+
constraint_name = generate_constraint_name(node)
|
16
|
+
unless_seen(constraint_name) do
|
17
|
+
@rewriter.replace(node.loc.expression, "t.string #{new_arguments_for_column_definition(node)}")
|
18
|
+
constraint = %["#{column_name(node)} IN (#{choices_for_enum_string(node)})"]
|
19
|
+
@rewriter.insert_after(node.last_sibling.source_range,
|
20
|
+
"\n t.check_constraint #{constraint}, name: \"#{constraint_name}\"")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def column_name(node)
|
27
|
+
JSON.parse(node.arguments[0].source)
|
28
|
+
end
|
29
|
+
|
30
|
+
def generate_constraint_name(node)
|
31
|
+
table_name = @table_name
|
32
|
+
"chk_rails_enum_#{table_name}_#{column_name(node)}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def choices_for_enum_string(node)
|
36
|
+
choices_for_enum(node).map { |o| "'#{o}'" }.join(", ")
|
37
|
+
end
|
38
|
+
|
39
|
+
def choices_for_enum(node)
|
40
|
+
choices = choices_from_node(node)
|
41
|
+
|
42
|
+
# add empty string to options if null is permitted
|
43
|
+
choices.unshift("") unless node.null_option_value == "false"
|
44
|
+
choices
|
45
|
+
end
|
46
|
+
|
47
|
+
def choices_from_node(node)
|
48
|
+
if node.limit_option.nil?
|
49
|
+
# Rails 7+ uses `enum('a','b','c')`
|
50
|
+
enum_choices = node.arguments[1].source
|
51
|
+
/\((.*)\)/.match(enum_choices)[1].split(",").map(&:strip).map { |o| o[1..-2] }
|
52
|
+
else
|
53
|
+
# Rails < 7 used `limit: ["a", "b", "c"]`
|
54
|
+
JSON.parse(node.limit_option.value.source)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def new_arguments_for_column_definition(node)
|
59
|
+
new_arguments = [%("#{column_name(node)}")]
|
60
|
+
conditionally_propagate_argument new_arguments, node, "null"
|
61
|
+
conditionally_propagate_argument new_arguments, node, "default"
|
62
|
+
new_arguments << "limit: #{choices_for_enum(node).map(&:length).max}"
|
63
|
+
new_arguments.join(", ")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spanner
|
4
|
+
module Translator
|
5
|
+
module Rules
|
6
|
+
module Translation
|
7
|
+
# replace ApplicationRecord with the configured class during translation of models
|
8
|
+
class ReplaceApplicationRecord < BaseRule
|
9
|
+
def on_class(node)
|
10
|
+
return unless node.inherits_from?(:ApplicationRecord)
|
11
|
+
return unless translation_options[:class]
|
12
|
+
|
13
|
+
@rewriter.replace(node.superclass_loc, translation_options[:class])
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spanner
|
4
|
+
module Translator
|
5
|
+
module Rules
|
6
|
+
module Translation
|
7
|
+
# Replace create_table arguments with sensible spanner options
|
8
|
+
class ReplaceCreateTableArgs < BaseRule
|
9
|
+
def on_send(node)
|
10
|
+
return unless node.create_table?
|
11
|
+
|
12
|
+
# keep table name argument, replace the rest with sensible spanner options
|
13
|
+
primary_keys = Spanner::Translator.configuration.default_primary_keys.inspect
|
14
|
+
@rewriter.replace(node.arguments[1].source_range, "primary_key: #{primary_keys}, force: :cascade")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spanner
|
4
|
+
module Translator
|
5
|
+
module Rules
|
6
|
+
module Translation
|
7
|
+
# convert t.datetime and t.timestamp to t.time, keeping null option if given
|
8
|
+
class TimeColumnsToTime < BaseRule
|
9
|
+
def on_send(node)
|
10
|
+
return unless node.t_datetime? || node.t_timestamp?
|
11
|
+
|
12
|
+
new_arguments = [node.arguments[0].source]
|
13
|
+
conditionally_propagate_argument new_arguments, node, "null"
|
14
|
+
conditionally_propagate_argument new_arguments, node, "default"
|
15
|
+
|
16
|
+
@rewriter.replace(node.loc.expression, "t.time #{new_arguments.join(", ")}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spanner/translator/rules/translation/base_rule"
|
4
|
+
|
5
|
+
module Spanner
|
6
|
+
module Translator
|
7
|
+
module Rules
|
8
|
+
module Translation # rubocop:disable Style/Documentation
|
9
|
+
# some rules must be perormed in a specific order
|
10
|
+
ORDERED_SCHEMA_RULES = %w[
|
11
|
+
AddExplicitPrimaryKey
|
12
|
+
AddCommittedAtTimestamp
|
13
|
+
AddIndexOptions
|
14
|
+
EnumColumnToStringWithConstraint
|
15
|
+
TimeColumnsToTime
|
16
|
+
BigintToInteger
|
17
|
+
ReplaceCreateTableArgs
|
18
|
+
].freeze
|
19
|
+
|
20
|
+
autoload :AddExplicitPrimaryKey, "spanner/translator/rules/translation/add_explicit_primary_key"
|
21
|
+
autoload :AddCommittedAtTimestamp, "spanner/translator/rules/translation/add_committed_at_timestamp"
|
22
|
+
autoload :AddIndexOptions, "spanner/translator/rules/translation/add_index_options"
|
23
|
+
autoload :AddTestCase, "spanner/translator/rules/translation/add_test_case"
|
24
|
+
autoload :BigintToInteger, "spanner/translator/rules/translation/bigint_to_integer"
|
25
|
+
autoload :EnumColumnToStringWithConstraint,
|
26
|
+
"spanner/translator/rules/translation/enum_column_to_string_with_constraint"
|
27
|
+
autoload :ReplaceCreateTableArgs, "spanner/translator/rules/translation/replace_create_table_args"
|
28
|
+
autoload :ReplaceApplicationRecord, "spanner/translator/rules/translation/replace_application_record"
|
29
|
+
autoload :TimeColumnsToTime, "spanner/translator/rules/translation/time_columns_to_time"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rubocop-ast"
|
4
|
+
|
5
|
+
# Utility methods for working with Rails MySQL table schemas
|
6
|
+
module Spanner
|
7
|
+
module Translator
|
8
|
+
# Access a Rails formatted db/schema.rb file and return create_table source
|
9
|
+
module Schema
|
10
|
+
class << self
|
11
|
+
def legacy_schema_path
|
12
|
+
Spanner::Translator.configuration.db_schema
|
13
|
+
end
|
14
|
+
|
15
|
+
def legacy_schema
|
16
|
+
raise Spanner::Translator::Error, "db/schema.rb not found" unless File.exist?(legacy_schema_path)
|
17
|
+
|
18
|
+
@legacy_schema ||= File.read(legacy_schema_path)
|
19
|
+
end
|
20
|
+
|
21
|
+
# extract the Ruby `create_table` statement for the given table name
|
22
|
+
def extract_create_table(table_name)
|
23
|
+
legacy_schema.match(/create_table "#{table_name}".*?end$/m)[0]
|
24
|
+
end
|
25
|
+
|
26
|
+
# extract the Parser::AST for the given table name,
|
27
|
+
# see https://github.com/whitequark/parser/blob/master/doc/AST_FORMAT.md
|
28
|
+
def extract_create_table_ast(table_name)
|
29
|
+
source = extract_create_table(table_name)
|
30
|
+
RuboCop::AST::ProcessedSource.new(source, RUBY_VERSION.to_f).ast
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spanner/translator/railtie" if defined?(Rails::Railtie)
|
4
|
+
|
5
|
+
module Spanner
|
6
|
+
# MySQL to Google Cloud Spanner Rails create_table statement translator
|
7
|
+
module Translator
|
8
|
+
class Error < StandardError; end
|
9
|
+
|
10
|
+
autoload :CLI, "spanner/translator/cli"
|
11
|
+
autoload :Configuration, "spanner/translator/configuration"
|
12
|
+
autoload :Rules, "spanner/translator/rules"
|
13
|
+
autoload :Schema, "spanner/translator/schema"
|
14
|
+
autoload :VERSION, "spanner/translator/version"
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def configure
|
18
|
+
yield(configuration)
|
19
|
+
end
|
20
|
+
|
21
|
+
def configuration
|
22
|
+
@configuration ||= Configuration.new
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/spanner/translator/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "spanner-translator"
|
7
|
+
spec.version = Spanner::Translator::VERSION
|
8
|
+
spec.authors = ["Adam Bachman"]
|
9
|
+
spec.email = ["abachman@recurly.com"]
|
10
|
+
|
11
|
+
spec.summary = "Translate pieces of a Rails app from MySQL to Google Cloud Spanner."
|
12
|
+
spec.description = "Translate pieces of a Rails app from MySQL to Google Cloud Spanner."
|
13
|
+
spec.homepage = "https://github.com/recurly/spanner-tranlator"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 3.2"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/recurly/spanner-tranlator"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/recurly/spanner-tranlator#changelog"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(__dir__) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_dependency "rubocop-ast", "~> 1.30.0"
|
33
|
+
|
34
|
+
# For more information and examples about making a new gem, check out our
|
35
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
36
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spanner-translator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Bachman
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-01-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rubocop-ast
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.30.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.30.0
|
27
|
+
description: Translate pieces of a Rails app from MySQL to Google Cloud Spanner.
|
28
|
+
email:
|
29
|
+
- abachman@recurly.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- ".rubocop.yml"
|
36
|
+
- ".ruby-version"
|
37
|
+
- CHANGELOG.md
|
38
|
+
- CODE_OF_CONDUCT.md
|
39
|
+
- Gemfile
|
40
|
+
- Gemfile.lock
|
41
|
+
- LICENSE.txt
|
42
|
+
- Makefile
|
43
|
+
- README.md
|
44
|
+
- Rakefile
|
45
|
+
- lib/rubocop/ast/class_node.rb
|
46
|
+
- lib/rubocop/ast/send_node.rb
|
47
|
+
- lib/spanner/translator.rb
|
48
|
+
- lib/spanner/translator/Rakefile
|
49
|
+
- lib/spanner/translator/cli.rb
|
50
|
+
- lib/spanner/translator/configuration.rb
|
51
|
+
- lib/spanner/translator/railtie.rb
|
52
|
+
- lib/spanner/translator/rules.rb
|
53
|
+
- lib/spanner/translator/rules/check.rb
|
54
|
+
- lib/spanner/translator/rules/check/has_any_index.rb
|
55
|
+
- lib/spanner/translator/rules/translation.rb
|
56
|
+
- lib/spanner/translator/rules/translation/add_committed_at_timestamp.rb
|
57
|
+
- lib/spanner/translator/rules/translation/add_explicit_primary_key.rb
|
58
|
+
- lib/spanner/translator/rules/translation/add_index_options.rb
|
59
|
+
- lib/spanner/translator/rules/translation/add_test_case.rb
|
60
|
+
- lib/spanner/translator/rules/translation/base_rule.rb
|
61
|
+
- lib/spanner/translator/rules/translation/bigint_to_integer.rb
|
62
|
+
- lib/spanner/translator/rules/translation/enum_column_to_string_with_constraint.rb
|
63
|
+
- lib/spanner/translator/rules/translation/replace_application_record.rb
|
64
|
+
- lib/spanner/translator/rules/translation/replace_create_table_args.rb
|
65
|
+
- lib/spanner/translator/rules/translation/time_columns_to_time.rb
|
66
|
+
- lib/spanner/translator/schema.rb
|
67
|
+
- lib/spanner/translator/version.rb
|
68
|
+
- sig/spanner/translator.rbs
|
69
|
+
- spanner-translator.gemspec
|
70
|
+
homepage: https://github.com/recurly/spanner-tranlator
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata:
|
74
|
+
homepage_uri: https://github.com/recurly/spanner-tranlator
|
75
|
+
source_code_uri: https://github.com/recurly/spanner-tranlator
|
76
|
+
changelog_uri: https://github.com/recurly/spanner-tranlator#changelog
|
77
|
+
rubygems_mfa_required: 'true'
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '3.2'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubygems_version: 3.5.3
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Translate pieces of a Rails app from MySQL to Google Cloud Spanner.
|
97
|
+
test_files: []
|