ASP.NETプロジェクト移植ROR実践---DB移行(完了)
最近RoRで、ちょうど手元にASPがあります.NETのSAPプロジェクト.このプロジェクトを借りて移植したいです.移植の最初のステップはデータベースに違いありません.SQLSERVER2005---POSTGRESQL8.4実施計画:1.msのすべてのテーブルをrubyで読み書きし、テーブル構造を読み出して対応するrails構造に変換し、railsのデータ移行ファイルを生成します.2.1で生成されたファイルによってDB構造が生成され、元のデータについては、まだ方法がなく、元のibatisを使った持続化が行われています.今はrailsのmoduleを少し近づけるしかありません.----------------------------------------------------------------------------------------------------今第一歩を解決しているので、このコミュニティが似たようなjobをしたことがあるかどうか分かりませんが、参考にしたいと思います.
SQL:
app.config:
AccessDb.rb:
Config.rb:#データベース結合モジュール
Convert2Csv.rb#はデータベース構造をcsvに変換する責任を負います(ファイルはcsvフォルダに入れます)
ConvertCsv2rb.rb#本プロジェクトの主なファイルは、データベース構造csvを対応するrubyファイルに変換する責任を負います(ファイルはmigrateフォルダに置きます)
最後にmigrateフォルダをrailsエンジニアリングの下にあるdbフォルダに入れます.
実行:rake db:migrate
成功すると自動的にschemaが生成されます.rbは同時にpostgresqlの中のテーブルも生成しました.
間にエラーが発生した場合は、railsの下のlogファイルと中のファイルを見ることができます.
最も一般的なエラーはdefault値で、sqlserverの中でdefaultは厳密ではありませんがrailsは異なり、integerタイプは絶対に「」を与えることはできません.
中の特殊な要求は自由に変えることができて、難しくありません.質問があればオンラインで答えます.
次はmodelを書き始め、er図とビジネスロジックに対応してror版のsapを構築し始めた.
SQL:
SELECT
(case when a.colorder=1 then d.name else '' end) as ,--
a.colorder as ,
a.name as ,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) as ,
(case when (SELECT count(*) FROM sysobjects--
WHERE (name in
(SELECT name FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid FROM syscolumns
WHERE (id = a.id) AND (name = a.name))
)))))
AND (xtype = 'PK'))>0 then '√' else '' end) as ,-- END
b.name as ,
a.length as ,
COLUMNPROPERTY(a.id,a.name,'PRECISION') as ,
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as ,
(case when a.isnullable=1 then '√'else '' end) as ,
isnull(e.text,'') as ,
isnull(g.[value],'') AS
FROM syscolumns a left join systypes b
on a.xtype=b.xusertype
inner join sysobjects d
on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e
on a.cdefault=e.id
left join sys.extended_properties g
on a.id=g.major_id AND a.colid = g.minor_id
where d.name in(select
o.name AS table_name
from
dbo.sysobjects o
where o.xtype = 'U' )--
order by a.id,a.colorder
app.config:
Provider=SQLNCLI.1;Password=****;User ID=****;Initial Catalog=****;Data Source=****
AccessDb.rb:
class AccessDb
attr_accessor :config, :connection, :data, :fields
def initialize(config=nil)
@config = config
@connection = nil
@data = nil
@fields = nil
end
def open
connection_string = @config
@connection = WIN32OLE.new('ADODB.Connection')
@connection.Open(connection_string)
end
def query(sql)
recordset = WIN32OLE.new('ADODB.Recordset')
recordset.Open(sql, @connection)
@fields = []
recordset.Fields.each do |field|
@fields << field.Name
end
begin
@data = recordset.GetRows.transpose
rescue
@data = []
end
recordset.Close
end
def execute(sql)
@connection.Execute(sql)
end
def close
@connection.Close
end
end
Config.rb:#データベース結合モジュール
module Config
require 'AccessDb'
def connect
require "win32ole"
config = ""
File.open("app.config", "r") do |file|
file.each_line {|line| config += line}
end
db = AccessDb.new(config)
begin
db.open
yield(db)
ensure
db.close
end
end
end
Convert2Csv.rb#はデータベース構造をcsvに変換する責任を負います(ファイルはcsvフォルダに入れます)
require 'Config'
require "csv"
include Config
sql = ""
File.open("E:\\Ruby\\config.sql", "r") do |file|
file.each_line {|line| sql += line}
end
connect do |db|
db.query(sql)
temp = ""
name = ""
content = ""
for row in db.data
if temp != row[0]
name = "csv\\" + row[0] + ".csv"
temp = row[0]
file = File.new(name,"w")
content = ""
end
if name == "csv\\" + row[0] + ".csv"
content << row.join("\t") << "
"
File.open(name, "w") {|f| f.write(content) }
end
end
end
ConvertCsv2rb.rb#本プロジェクトの主なファイルは、データベース構造csvを対応するrubyファイルに変換する責任を負います(ファイルはmigrateフォルダに置きます)
require "csv"
def ConvertCsv2Rb
version = 0
Dir.foreach("csv") do |file|
if file != "." && file != ".." && file != "sysdiagrams.csv" && file != "out.csv"
detail = ""
index = "\t\tend
"
CSV.open("csv\\" + file,'r', "\t") do |row|
head = "class "
head << "Create" << MakeName(row[0]) << " < ActiveRecord::Migration" << "
"
head << "\t" << "def self.up" << "
"
head << "\t\t" << "create_table :" << row[0].downcase << " do |t| " << "
"
name = "migrate\\" << "20100628" << version.to_s() << "_create_" << row[0].downcase + ".rb"#20090708025608_create_feeds.rb
file = File.new(name,"w")
detail << MakeDetail(row)
if row[4]=="*"
index << "\t\t\tadd_index :" << row[0].downcase << ",\t[:" << row[2].downcase << "],\t:unique => true" << "
"
end
foot = "\tend
\t" << "def self.down" << "
"
foot << "\t\t"<< "drop_table :" << row[0].downcase << "
"
foot << "\tend
end"
yield(detail)
File.open(name, "w") {|f| f.write(head << detail << index << foot) }
end #CSV
version = version + 1
end #if
end #Dir
end #def
def MakeDetail(row)
detail = "\t\t\t"
type = case row[5]
when "int","smallint" then "t.integer"
when "nvarchar" then "t.text"
when "datetime" then "t.datetime"
when "varchar","char" then "t.string"
when "numeric" then "t.decimal"
when "bit" then "t.binary"
else ""
end
limit = case row[5]
when "varchar","char" then ",\t :limit => " << row[7] << ",\t :default => ''"
when "nvarchar" then ",\t :default => ''"
when "numeric" then ",\t :precision => " << row[7] << ",\t :scale => " << row[8] << ",\t :default => 0"
when "bit" then ",\t :default => false"
else ""
end
isnull = case row[9]
when "*" then ",\t :null => false"
else ""
end
detail << type << "\t:" << row[2].downcase << limit << isnull << "
"
return detail
end
def MakeName(name)
lowerw = ""
name.split("_").each do |w|
lowerw << w[0..0].upcase
lowerw << w[1..-1].downcase
end
return lowerw
end
ConvertCsv2Rb do |detail|
end
最後にmigrateフォルダをrailsエンジニアリングの下にあるdbフォルダに入れます.
実行:rake db:migrate
成功すると自動的にschemaが生成されます.rbは同時にpostgresqlの中のテーブルも生成しました.
間にエラーが発生した場合は、railsの下のlogファイルと中のファイルを見ることができます.
最も一般的なエラーはdefault値で、sqlserverの中でdefaultは厳密ではありませんがrailsは異なり、integerタイプは絶対に「」を与えることはできません.
中の特殊な要求は自由に変えることができて、難しくありません.質問があればオンラインで答えます.
次はmodelを書き始め、er図とビジネスロジックに対応してror版のsapを構築し始めた.