add option to select db adapter in ror (#711)

This commit is contained in:
William Cheng 2018-08-02 13:52:34 +08:00 committed by GitHub
parent 40024ac72a
commit 375c26ccb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 102 additions and 5 deletions

View File

@ -257,4 +257,8 @@ public class CodegenConstants {
public static final String DOCEXTENSION = "docExtension"; 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 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";
} }

View File

@ -95,7 +95,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
/** /**
* Programmatically disable the output of .openapi-generator/VERSION, .openapi-generator-ignore, * 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 * @param generateMetadata true: enable outputs, false: disable outputs
*/ */

View File

@ -19,7 +19,10 @@ package org.openapitools.codegen.languages;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenType; import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.SupportingFile; import org.openapitools.codegen.SupportingFile;
import io.swagger.v3.oas.models.media.*; import io.swagger.v3.oas.models.media.*;
@ -64,6 +67,8 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen {
protected String pidFolder = tmpFolder + File.separator + "pids"; protected String pidFolder = tmpFolder + File.separator + "pids";
protected String socketsFolder = tmpFolder + File.separator + "sockets"; protected String socketsFolder = tmpFolder + File.separator + "sockets";
protected String vendorFolder = "vendor"; protected String vendorFolder = "vendor";
protected String databaseAdapter = "sqlite";
public RubyOnRailsServerCodegen() { public RubyOnRailsServerCodegen() {
super(); super();
@ -87,6 +92,9 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen {
// remove modelPackage and apiPackage added by default // remove modelPackage and apiPackage added by default
cliOptions.clear(); cliOptions.clear();
cliOptions.add(new CliOption(CodegenConstants.DATABASE_ADAPTER, CodegenConstants.DATABASE_ADAPTER_DESC).
defaultValue("sqlite"));
} }
@Override @Override
@ -97,6 +105,23 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen {
//setModelPackage("models"); //setModelPackage("models");
setApiPackage("app/controllers"); 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("Gemfile", "", "Gemfile"));
supportingFiles.add(new SupportingFile("README.md", "", "README.md")); supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
supportingFiles.add(new SupportingFile("Rakefile", "", "Rakefile")); supportingFiles.add(new SupportingFile("Rakefile", "", "Rakefile"));
@ -230,7 +255,7 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen {
if (name.length() == 0) { if (name.length() == 0) {
return "ApiController"; return "ApiController";
} }
// e.g. phone_number_api => PhoneNumberApi // e.g. phone_number_controller => PhoneNumberController
return camelize(name) + "Controller"; return camelize(name) + "Controller";
} }
@ -239,4 +264,8 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen {
generateYAMLSpecFile(objs); generateYAMLSpecFile(objs);
return super.postProcessSupportingFileData(objs); return super.postProcessSupportingFileData(objs);
} }
public void setDatabaseAdapter(String databaseAdapter) {
this.databaseAdapter = databaseAdapter;
}
} }

View File

@ -3,8 +3,14 @@ source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0' 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 # Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '< 0.5' gem 'mysql2', '>= 0.3.18', '< 0.5'
{{/isDBMySQL}}
# Use Puma as the app server # Use Puma as the app server
gem 'puma', '~> 3.0' gem 'puma', '~> 3.0'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
@ -33,4 +39,4 @@ group :development do
end end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem # 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]

View File

@ -1,3 +1,4 @@
{{#isDBMySQL}}
# MySQL. Versions 5.0 and up are supported. # MySQL. Versions 5.0 and up are supported.
# #
# Install the MySQL driver # Install the MySQL driver
@ -52,3 +53,28 @@ production:
database: api_demo_production database: api_demo_production
username: api_demo username: api_demo
password: <%= ENV['API_DEMO_DATABASE_PASSWORD'] %> 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}}

View File

@ -1 +1 @@
3.1.1-SNAPSHOT 3.2.0-SNAPSHOT

View File

@ -3,8 +3,14 @@ source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0' 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 # Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '< 0.5' gem 'mysql2', '>= 0.3.18', '< 0.5'
{{/isDBMySQL}}
# Use Puma as the app server # Use Puma as the app server
gem 'puma', '~> 3.0' gem 'puma', '~> 3.0'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
@ -33,4 +39,4 @@ group :development do
end end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem # 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]

View File

@ -1,3 +1,4 @@
{{#isDBMySQL}}
# MySQL. Versions 5.0 and up are supported. # MySQL. Versions 5.0 and up are supported.
# #
# Install the MySQL driver # Install the MySQL driver
@ -52,3 +53,28 @@ production:
database: api_demo_production database: api_demo_production
username: api_demo username: api_demo
password: <%= ENV['API_DEMO_DATABASE_PASSWORD'] %> 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}}