Skip to content

Commit

Permalink
add option to select db adapter in ror (OpenAPITools#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Aug 2, 2018
1 parent e15dae4 commit 50baad3
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,8 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,

public static final String DOCEXTENSION = "docExtension";
public static final String DOCEXTENSION_DESC = "The extension of the generated documentation files, defaults to markdown, .md";

public static final String DATABASE_ADAPTER = "databaseAdapter";
public static final String DATABASE_ADAPTER_DESC = "The adapter for database (e.g. mysql, sqlite). Default: sqlite";

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Generator opts(ClientOptInput opts) {

/**
* Programmatically disable the output of .openapi-generator/VERSION, .openapi-generator-ignore,
* or other metadata files used by Swagger Codegen.
* or other metadata files used by OpenAPI Generator.
*
* @param generateMetadata true: enable outputs, false: disable outputs
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

import java.text.SimpleDateFormat;


import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.SupportingFile;

import io.swagger.v3.oas.models.media.*;
Expand Down Expand Up @@ -64,6 +67,8 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen {
protected String pidFolder = tmpFolder + File.separator + "pids";
protected String socketsFolder = tmpFolder + File.separator + "sockets";
protected String vendorFolder = "vendor";
protected String databaseAdapter = "sqlite";


public RubyOnRailsServerCodegen() {
super();
Expand All @@ -87,6 +92,9 @@ public RubyOnRailsServerCodegen() {

// remove modelPackage and apiPackage added by default
cliOptions.clear();

cliOptions.add(new CliOption(CodegenConstants.DATABASE_ADAPTER, CodegenConstants.DATABASE_ADAPTER_DESC).
defaultValue("sqlite"));
}

@Override
Expand All @@ -97,6 +105,23 @@ public void processOpts() {
//setModelPackage("models");
setApiPackage("app/controllers");

// determine which db adapter to use
if (additionalProperties.containsKey(CodegenConstants.DATABASE_ADAPTER)) {
setDatabaseAdapter((String) additionalProperties.get(CodegenConstants.DATABASE_ADAPTER));
} else {
// not set, pass the default value to template
additionalProperties.put(CodegenConstants.DATABASE_ADAPTER, databaseAdapter);
}

if ("sqlite".equals(databaseAdapter)) {
additionalProperties.put("isDBSQLite", Boolean.TRUE);
} else if ("mysql".equals(databaseAdapter)) {
additionalProperties.put("isDBMySQL", Boolean.TRUE);
} else {
LOGGER.warn("Unknown database {}. Defaul to 'sqlite'.", databaseAdapter);
additionalProperties.put("isDBSQLite", Boolean.TRUE);
}

supportingFiles.add(new SupportingFile("Gemfile", "", "Gemfile"));
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
supportingFiles.add(new SupportingFile("Rakefile", "", "Rakefile"));
Expand Down Expand Up @@ -230,7 +255,7 @@ public String toApiName(String name) {
if (name.length() == 0) {
return "ApiController";
}
// e.g. phone_number_api => PhoneNumberApi
// e.g. phone_number_controller => PhoneNumberController
return camelize(name) + "Controller";
}

Expand All @@ -239,4 +264,8 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
generateYAMLSpecFile(objs);
return super.postProcessSupportingFileData(objs);
}

public void setDatabaseAdapter(String databaseAdapter) {
this.databaseAdapter = databaseAdapter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0'
{{#isDBSQLite}}
# Use sqlite as the database for Active Record
gem 'sqlite3', '~> 1.3'
{{/isDBSQLite}}
{{#isDBMySQL}}
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '< 0.5'
{{/isDBMySQL}}
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
Expand Down Expand Up @@ -33,4 +39,4 @@ group :development do
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{#isDBMySQL}}
# MySQL. Versions 5.0 and up are supported.
#
# Install the MySQL driver
Expand Down Expand Up @@ -52,3 +53,28 @@ production:
database: api_demo_production
username: api_demo
password: <%= ENV['API_DEMO_DATABASE_PASSWORD'] %>
{{/isDBMySQL}}
{{#isDBSQLite}}
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000

production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
{{/isDBSQLite}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.1-SNAPSHOT
3.2.0-SNAPSHOT
8 changes: 7 additions & 1 deletion samples/server/petstore/ruby-on-rails/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0'
{{#isDBSQLite}}
# Use sqlite as the database for Active Record
gem 'sqlite3', '~> 1.3'
{{/isDBSQLite}}
{{#isDBMySQL}}
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '< 0.5'
{{/isDBMySQL}}
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
Expand Down Expand Up @@ -33,4 +39,4 @@ group :development do
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
26 changes: 26 additions & 0 deletions samples/server/petstore/ruby-on-rails/config/database.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{#isDBMySQL}}
# MySQL. Versions 5.0 and up are supported.
#
# Install the MySQL driver
Expand Down Expand Up @@ -52,3 +53,28 @@ production:
database: api_demo_production
username: api_demo
password: <%= ENV['API_DEMO_DATABASE_PASSWORD'] %>
{{/isDBMySQL}}
{{#isDBSQLite}}
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000

production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
{{/isDBSQLite}}

0 comments on commit 50baad3

Please sign in to comment.