Java 从旧的MySQL数据库迁移数据

Java 从旧的MySQL数据库迁移数据,java,mysql,ruby-on-rails,stored-procedures,backup,Java,Mysql,Ruby On Rails,Stored Procedures,Backup,我最近将一个JavaEEWeb应用程序(运行在MySQL数据库上)重写为Rails 3.1。现在的问题是,新应用程序的数据库模型与旧应用程序不同,因为我添加、删除并重命名了一些属性。数据库表名称也不同 是否有迁移此数据的方法?我能想象的唯一方法是编写一个包含许多ALTERTABLE和CREATETABLE语句的存储过程,以将数据库更新为新模型 提前谢谢 解决方案: 我最后在mysql存储过程中使用INSERT..SELECT语句来迁移数据。插入到新模式中。新建表格从旧模式中选择。旧表格。我现在正

我最近将一个JavaEEWeb应用程序(运行在MySQL数据库上)重写为Rails 3.1。现在的问题是,新应用程序的数据库模型与旧应用程序不同,因为我添加、删除并重命名了一些属性。数据库表名称也不同

是否有迁移此数据的方法?我能想象的唯一方法是编写一个包含许多ALTERTABLE和CREATETABLE语句的存储过程,以将数据库更新为新模型

提前谢谢

解决方案


我最后在mysql存储过程中使用INSERT..SELECT语句来迁移数据。插入到新模式中。新建表格从旧模式中选择。旧表格。我现在正在考虑做一个Rake任务来调用该过程并做其他事情。

唯一的方法是编写一个脚本,从旧数据库中获取数据并将thme插入新数据库。或者您可以通过某种方式连接到这两个数据库,然后进行一些选择和插入查询,如

 insert into new_db.table as select old_db.table.field1, ....


无论如何,这是一个手动过程,如果可以通过脚本而不是存储过程在某种程度上实现自动化,那么您可以使用mysql的信息模式在rails控制台中尝试rails和一些sql

sql = ActiveRecord::Base.connection()
old_tables = sql.execute "select table_name from information_schema.tables where table_schema = your_old_schema"
res.each do | old_table|
  old_fields = sql.execute "select distinct column_name, data_type from information_schema.columns where table_name='#{old_table}' and table_schema='your_old_schema'"
  new_fields = sql.execute "select distinct column_name, data_type from information_schema.columns where table_name='#{old_table}' and table_schema='your_new_schema'"
  #compare fields and so on...
end

我同意我的观点,只是sql。最后,如果需要在多台服务器上执行sql,只需写下sql。非常感谢,我用最终的解决方案编辑了我的问题。
sql = ActiveRecord::Base.connection()
old_tables = sql.execute "select table_name from information_schema.tables where table_schema = your_old_schema"
res.each do | old_table|
  old_fields = sql.execute "select distinct column_name, data_type from information_schema.columns where table_name='#{old_table}' and table_schema='your_old_schema'"
  new_fields = sql.execute "select distinct column_name, data_type from information_schema.columns where table_name='#{old_table}' and table_schema='your_new_schema'"
  #compare fields and so on...
end