Skip to content

Commit 375c26c

Browse files
authored
add option to select db adapter in ror (#711)
1 parent 40024ac commit 375c26c

8 files changed

Lines changed: 102 additions & 5 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,8 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
257257

258258
public static final String DOCEXTENSION = "docExtension";
259259
public static final String DOCEXTENSION_DESC = "The extension of the generated documentation files, defaults to markdown, .md";
260+
261+
public static final String DATABASE_ADAPTER = "databaseAdapter";
262+
public static final String DATABASE_ADAPTER_DESC = "The adapter for database (e.g. mysql, sqlite). Default: sqlite";
263+
260264
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public Generator opts(ClientOptInput opts) {
9595

9696
/**
9797
* Programmatically disable the output of .openapi-generator/VERSION, .openapi-generator-ignore,
98-
* or other metadata files used by Swagger Codegen.
98+
* or other metadata files used by OpenAPI Generator.
9999
*
100100
* @param generateMetadata true: enable outputs, false: disable outputs
101101
*/

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyOnRailsServerCodegen.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
import java.text.SimpleDateFormat;
2121

22+
23+
import org.openapitools.codegen.CliOption;
2224
import org.openapitools.codegen.CodegenType;
25+
import org.openapitools.codegen.CodegenConstants;
2326
import org.openapitools.codegen.SupportingFile;
2427

2528
import io.swagger.v3.oas.models.media.*;
@@ -64,6 +67,8 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen {
6467
protected String pidFolder = tmpFolder + File.separator + "pids";
6568
protected String socketsFolder = tmpFolder + File.separator + "sockets";
6669
protected String vendorFolder = "vendor";
70+
protected String databaseAdapter = "sqlite";
71+
6772

6873
public RubyOnRailsServerCodegen() {
6974
super();
@@ -87,6 +92,9 @@ public RubyOnRailsServerCodegen() {
8792

8893
// remove modelPackage and apiPackage added by default
8994
cliOptions.clear();
95+
96+
cliOptions.add(new CliOption(CodegenConstants.DATABASE_ADAPTER, CodegenConstants.DATABASE_ADAPTER_DESC).
97+
defaultValue("sqlite"));
9098
}
9199

92100
@Override
@@ -97,6 +105,23 @@ public void processOpts() {
97105
//setModelPackage("models");
98106
setApiPackage("app/controllers");
99107

108+
// determine which db adapter to use
109+
if (additionalProperties.containsKey(CodegenConstants.DATABASE_ADAPTER)) {
110+
setDatabaseAdapter((String) additionalProperties.get(CodegenConstants.DATABASE_ADAPTER));
111+
} else {
112+
// not set, pass the default value to template
113+
additionalProperties.put(CodegenConstants.DATABASE_ADAPTER, databaseAdapter);
114+
}
115+
116+
if ("sqlite".equals(databaseAdapter)) {
117+
additionalProperties.put("isDBSQLite", Boolean.TRUE);
118+
} else if ("mysql".equals(databaseAdapter)) {
119+
additionalProperties.put("isDBMySQL", Boolean.TRUE);
120+
} else {
121+
LOGGER.warn("Unknown database {}. Defaul to 'sqlite'.", databaseAdapter);
122+
additionalProperties.put("isDBSQLite", Boolean.TRUE);
123+
}
124+
100125
supportingFiles.add(new SupportingFile("Gemfile", "", "Gemfile"));
101126
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
102127
supportingFiles.add(new SupportingFile("Rakefile", "", "Rakefile"));
@@ -230,7 +255,7 @@ public String toApiName(String name) {
230255
if (name.length() == 0) {
231256
return "ApiController";
232257
}
233-
// e.g. phone_number_api => PhoneNumberApi
258+
// e.g. phone_number_controller => PhoneNumberController
234259
return camelize(name) + "Controller";
235260
}
236261

@@ -239,4 +264,8 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
239264
generateYAMLSpecFile(objs);
240265
return super.postProcessSupportingFileData(objs);
241266
}
267+
268+
public void setDatabaseAdapter(String databaseAdapter) {
269+
this.databaseAdapter = databaseAdapter;
270+
}
242271
}

modules/openapi-generator/src/main/resources/ruby-on-rails-server/Gemfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ source 'https://rubygems.org'
33

44
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
55
gem 'rails', '~> 5.0.0'
6+
{{#isDBSQLite}}
7+
# Use sqlite as the database for Active Record
8+
gem 'sqlite3', '~> 1.3'
9+
{{/isDBSQLite}}
10+
{{#isDBMySQL}}
611
# Use mysql as the database for Active Record
712
gem 'mysql2', '>= 0.3.18', '< 0.5'
13+
{{/isDBMySQL}}
814
# Use Puma as the app server
915
gem 'puma', '~> 3.0'
1016
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
@@ -33,4 +39,4 @@ group :development do
3339
end
3440

3541
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
36-
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
42+
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

modules/openapi-generator/src/main/resources/ruby-on-rails-server/database.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{#isDBMySQL}}
12
# MySQL. Versions 5.0 and up are supported.
23
#
34
# Install the MySQL driver
@@ -52,3 +53,28 @@ production:
5253
database: api_demo_production
5354
username: api_demo
5455
password: <%= ENV['API_DEMO_DATABASE_PASSWORD'] %>
56+
{{/isDBMySQL}}
57+
{{#isDBSQLite}}
58+
# SQLite version 3.x
59+
# gem install sqlite3-ruby (not necessary on OS X Leopard)
60+
development:
61+
adapter: sqlite3
62+
database: db/development.sqlite3
63+
pool: 5
64+
timeout: 5000
65+
66+
# Warning: The database defined as "test" will be erased and
67+
# re-generated from your development database when you run "rake".
68+
# Do not set this db to the same as development or production.
69+
test:
70+
adapter: sqlite3
71+
database: db/test.sqlite3
72+
pool: 5
73+
timeout: 5000
74+
75+
production:
76+
adapter: sqlite3
77+
database: db/production.sqlite3
78+
pool: 5
79+
timeout: 5000
80+
{{/isDBSQLite}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.1-SNAPSHOT
1+
3.2.0-SNAPSHOT

samples/server/petstore/ruby-on-rails/Gemfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ source 'https://rubygems.org'
33

44
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
55
gem 'rails', '~> 5.0.0'
6+
{{#isDBSQLite}}
7+
# Use sqlite as the database for Active Record
8+
gem 'sqlite3', '~> 1.3'
9+
{{/isDBSQLite}}
10+
{{#isDBMySQL}}
611
# Use mysql as the database for Active Record
712
gem 'mysql2', '>= 0.3.18', '< 0.5'
13+
{{/isDBMySQL}}
814
# Use Puma as the app server
915
gem 'puma', '~> 3.0'
1016
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
@@ -33,4 +39,4 @@ group :development do
3339
end
3440

3541
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
36-
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
42+
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

samples/server/petstore/ruby-on-rails/config/database.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{#isDBMySQL}}
12
# MySQL. Versions 5.0 and up are supported.
23
#
34
# Install the MySQL driver
@@ -52,3 +53,28 @@ production:
5253
database: api_demo_production
5354
username: api_demo
5455
password: <%= ENV['API_DEMO_DATABASE_PASSWORD'] %>
56+
{{/isDBMySQL}}
57+
{{#isDBSQLite}}
58+
# SQLite version 3.x
59+
# gem install sqlite3-ruby (not necessary on OS X Leopard)
60+
development:
61+
adapter: sqlite3
62+
database: db/development.sqlite3
63+
pool: 5
64+
timeout: 5000
65+
66+
# Warning: The database defined as "test" will be erased and
67+
# re-generated from your development database when you run "rake".
68+
# Do not set this db to the same as development or production.
69+
test:
70+
adapter: sqlite3
71+
database: db/test.sqlite3
72+
pool: 5
73+
timeout: 5000
74+
75+
production:
76+
adapter: sqlite3
77+
database: db/production.sqlite3
78+
pool: 5
79+
timeout: 5000
80+
{{/isDBSQLite}}

0 commit comments

Comments
 (0)