#!/usr/bin/env ruby require "arxutils_sqlite3" require "simpleoptparse" require "pp" require "ykutils" require "ykxutils" config = Arxutils_Sqlite3::Config.new config.require_opts_file begin require "dbrelation" rescue LoadError => ex pp ex.message end klass = nil setting = config.load_setting_yaml_file klass = setting[:klass] # DBセットアップクラス config.require_dbsetup_file banner = "Usage: bundle exec ruby exe/makemigrate --cmd=(s|c|m|i|d) -y yaml_file --klass=class" opts = @opts ? @opts : {} opts["dbconfig"] = Arxutils_Sqlite3::Config::DBCONFIG_SQLITE3 Simpleoptparse::Simpleoptparse.parse(ARGV, opts, banner, Arxutils_Sqlite3::VERSION, nil) do |parser| parser.on("--cmd X", %w[s c m i d]) { |x| opts["cmd"] = x } parser.on("-y yaml_file", "--yaml yaml_file") { |x| opts["yaml"] = x } parser.on("--klass klass") { |x| opts["klass"] = x } end env = ENV.fetch("ENV", nil) env ||= "production" case opts["cmd"] when "s" config.check_file_exist(Arxutils_Sqlite3::Config::DEST_OPTS_FILE, banner, 10) if opts["klass"].nil? || opts["klass"].strip == "" puts banner exit 20 end klass = opts["klass"] config.make_config_directory config.setup_db_scheme_file config.setup_opts_file(klass) config.setup_setting_yaml_file(klass) db_scheme_ary = nil when "c" db_scheme_ary = nil dbconfig = opts["dbconfig"] replation = opts["replation"] dbconfig_path = config.make_dbconfig_path(dbconfig) config.check_file_exist(dbconfig_path, banner, 30) mig = config.prepare_for_migrate(db_scheme_ary, dbconfig_path, dbconfig, replation) mig.make_dbconfig(opts) when "m" yaml_fname = opts["yaml"] relation = opts[:relation] dbconfig = opts["dbconfig"] if yaml_fname.nil? || yaml_fname.strip == "" puts banner exit 40 end yaml_pn = Pathname.new(yaml_fname) config.check_file_not_exist(yaml_pn, banner, 55) db_scheme_ary = YAML.load_file( yaml_pn ) dest_dbsetup_file = config.get_dest_dbsetup_file config.check_file_exist(dest_dbsetup_file, banner, 50) config.make_dbsetup_file(db_scheme_ary, relation, klass, dest_dbsetup_file) connect_time = Arxutils_Sqlite3::Dbutil::Dbconnect.db_connect(config, dbconfig, env) # マイグレーション用スクリプトの生成、relationのクラス定義ファイルの生成 mig = config.prepare_for_migrate(db_scheme_ary, dbconfig_path, dbconfig, relation) mig.output # マイグレーション実行 ActiveRecord::MigrationContext.new(mig.migrate_dir, ActiveRecord::SchemaMigration).up when "i" dbconfig = opts["dbconfig"] connect_time = Arxutils_Sqlite3::Dbutil::Dbconnect.db_connect(config, dbconfig, env) Dbsetup.new(connect_time) when "d" db_scheme_ary = nil dbconfig = opts["dbconfig"] replation = opts["replation"] config_dir = config.get_config_dir dbconfig_path = Arxutils_Sqlite3::Util.make_dbconfig_path(config_dir, dbconfig) mig = config.prepare_for_migrate(db_scheme_ary, dbconfig_path, dbconfig, replation) mig.delete_migrate_config_and_db else puts "Invalid command(#{opts["cmd"]}) specified!!" end