Seeking to move all my shared models to an engine which can be included in each of my micro apps.
寻求将我的所有共享模型移动到可以包含在我的每个微应用程序中的引擎。
This engine should provide a model layer to all our legacy data, including:
该引擎应为我们的所有遗留数据提供模型层,包括:
- Model files
- Schema files
- Migrations (we're following Pivotal Labs' pattern, this isn't the issue)
迁移(我们遵循Pivotal Labs的模式,这不是问题)
Model files are being patched in automatically, that's fine.
模型文件正在自动修补,没关系。
Schema files are being monkey-patched in using Nikolay Strum's db.rake:
使用Nikolay Strum的db.rake对模式文件进行了修补:
namespace :db do
namespace :schema do
# desc 'Dump additional database schema'
task :dump => [:environment, :load_config] do
filename = "#{Rails.root}/db/foo_schema.rb"
File.open(filename, 'w:utf-8') do |file|
ActiveRecord::Base.establish_connection("foo_#{Rails.env}")
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end
end
namespace :test do
# desc 'Purge and load foo_test schema'
task :load_schema do
# like db:test:purge
abcs = ActiveRecord::Base.configurations
ActiveRecord::Base.connection.recreate_database(abcs['foo_test']['database'], mysql_creation_options(abcs['foo_test']))
# like db:test:load_schema
ActiveRecord::Base.establish_connection('foo_test')
ActiveRecord::Schema.verbose = false
load("#{Rails.root}/db/foo_schema.rb")
end
end
end
We need rake db:create
and rake db:schema:load
to work,
我们需要rake db:create和rake db:schema:load to work,
The db.rake
patches only affect db:schema:dump
and db:test:load_schema
(part of tests_prepare, I assume). I've attempted to patch them into db:schema:load
using:
db.rake补丁仅影响db:schema:dump和db:test:load_schema(tests_prepare的一部分,我假设)。我试图将它们修补到db:schema:load using:
namespace :db do
# Helpers
def mysql_creation_options(config)
@charset = ENV['CHARSET'] || 'utf8'
@collation = ENV['COLLATION'] || 'utf8_unicode_ci'
{:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
end
def load_schema(schema_name)
abcs = ActiveRecord::Base.configurations
ActiveRecord::Base.connection.recreate_database(abcs[schema_name+'_test']['database'], mysql_creation_options(abcs[schema_name+'_test']))
# like db:test:load_schema
ActiveRecord::Base.establish_connection(schema_name+'_test')
ActiveRecord::Schema.verbose = false
load("#{Rails.root}/db/#{schema_name}_schema.rb")
end
namespace :schema do
# desc 'Dump additional database schema'
task :dump => [:environment, :load_config] do
dump_schema = -> (schema_name) {
filename = "#{Rails.root}/db/#{schema_name}_schema.rb"
File.open(filename, 'w:utf-8') do |file|
ActiveRecord::Base.establish_connection("#{schema_name}_#{Rails.env}")
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
}
dump_schema.call('kiddom')
dump_schema.call('kiddom_warehouse')
end
# When loading from schema, load these files, too
task :load => [:environment, :load_config] do
load_schema('kiddom')
load_schema('kiddom_warehouse')
end
end
namespace :test do
# desc 'Purge and load foo_test schema'
task :load_schema do
load_schema('kiddom')
load_schema('kiddom_warehouse')
end
end
end
But this gives me the error NoMethodError: undefined method 'recreate_database' for #<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x007feb6bb43558>
. Apparently, this only works on Oracle-type databases?
但是这给了我错误NoMethodError:## ActiveRecord :: ConnectionAdapters :: SQLite3Adapter:0x007feb6bb43558>的未定义方法'recreate_database'。显然,这仅适用于Oracle类型的数据库?
What are the Rails commands for the underlying DROP
and CREATE DATABASE
sql I'm trying to affect for the extra schema.rb
's?
什么是底层DROP和CREATE DATABASE sql的Rails命令我试图影响额外的schema.rb?
1 个解决方案
#1
0
You are using SQLite as your database engine. Hope that is what you want to do.
您正在使用SQLite作为数据库引擎。希望这就是你想要做的。
Since you are creating SQLite Database , things differ a bit from other database adapters like MySQLAdpter or Postgress.
由于您正在创建SQLite数据库,因此与其他数据库适配器(如MySQLAdpter或Postgress)有所不同。
In case of MySQL, database has to be created prior to establish a connection by spending "CREATE DATABASE ... " SQL commands. So you must create the database before establishing connection.
在MySQL的情况下,必须在建立连接之前通过花费“CREATE DATABASE ...”SQL命令来创建数据库。因此,您必须在建立连接之前创建数据库。
But in case of SQLite, since the database reside in a file and one file can contain only one database, there is no separate step to create database. An attempt to establishing a connection to the database itself will cause the database file to be created.
但是对于SQLite,由于数据库驻留在一个文件中,而一个文件只能包含一个数据库,因此没有单独的步骤来创建数据库。尝试建立与数据库本身的连接将导致创建数据库文件。
Hence create_database method won't work when using SQLiteAdapter. You can simply remove that line from your code.
因此,使用SQLiteAdapter时create_database方法将不起作用。您只需从代码中删除该行即可。
You may have a look at the source code for the Rake task db:create
您可以查看Rake任务db:create的源代码
Also, the source code for 'create' method in SQLiteDatabaseTasks. As you can see, it simply calls the establish_connection method
另外,SQLiteDatabaseTasks中“create”方法的源代码。如您所见,它只是调用establish_connection方法
#1
0
You are using SQLite as your database engine. Hope that is what you want to do.
您正在使用SQLite作为数据库引擎。希望这就是你想要做的。
Since you are creating SQLite Database , things differ a bit from other database adapters like MySQLAdpter or Postgress.
由于您正在创建SQLite数据库,因此与其他数据库适配器(如MySQLAdpter或Postgress)有所不同。
In case of MySQL, database has to be created prior to establish a connection by spending "CREATE DATABASE ... " SQL commands. So you must create the database before establishing connection.
在MySQL的情况下,必须在建立连接之前通过花费“CREATE DATABASE ...”SQL命令来创建数据库。因此,您必须在建立连接之前创建数据库。
But in case of SQLite, since the database reside in a file and one file can contain only one database, there is no separate step to create database. An attempt to establishing a connection to the database itself will cause the database file to be created.
但是对于SQLite,由于数据库驻留在一个文件中,而一个文件只能包含一个数据库,因此没有单独的步骤来创建数据库。尝试建立与数据库本身的连接将导致创建数据库文件。
Hence create_database method won't work when using SQLiteAdapter. You can simply remove that line from your code.
因此,使用SQLiteAdapter时create_database方法将不起作用。您只需从代码中删除该行即可。
You may have a look at the source code for the Rake task db:create
您可以查看Rake任务db:create的源代码
Also, the source code for 'create' method in SQLiteDatabaseTasks. As you can see, it simply calls the establish_connection method
另外,SQLiteDatabaseTasks中“create”方法的源代码。如您所见,它只是调用establish_connection方法