Module: INatGet::App::Maintenance
- Defined in:
- lib/inat-get/app/maintenance.rb
Class Method Summary collapse
- .db_check(config, continue = false) ⇒ Object
- .db_create(config) ⇒ Object
- .db_migrate(config) ⇒ Object
- .db_reset(config) ⇒ Object
- .db_update(config) ⇒ Object
- .file_based?(uri) ⇒ Boolean private
- .info(config) ⇒ Object
- .migrations_path ⇒ Object private
- .run_migrations(config, target: nil) ⇒ Object private
- .show_config(config) ⇒ Object
Class Method Details
.db_check(config, continue = false) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/inat-get/app/maintenance.rb', line 33 def db_check config, continue = false connect_string = config.dig :database, :connect uri = URI::parse connect_string if file_based?(uri) unless File.exist?(uri.path) $stderr.puts "❌ \e[1mDatabase file not found:\e[0m #{ uri.path }" exit Errno::ENOENT::Errno end end db_opts = { user: config.dig(:database, :user), password: config.dig(:database, :password) }.compact begin db = Sequel.connect connect_string, **db_opts rescue => e $stderr.puts "❌ \e[1mDB Connection error:\e[0m #{ e. }" exit Errno::ECONNREFUSED::Errno end Sequel.extension :migration migrator = Sequel::Migrator::migrator_class(migrations_path).new(db, migrations_path) md_ver = migrator.target db_ver = migrator.current if md_ver != db_ver $stderr.puts "🚨 \e[1mDatabase is not actual:\e[0m" $stderr.puts " Target version: \e[1m#{ md_ver }\e[0m" $stderr.puts " Current version: \e[1m#{ db_ver }\e[0m" exit Errno::ECANCELED::Errno end unless continue $stderr.puts "✅ \e[1mDatabase is actual:\e[0m version \e[1m#{md_ver}\e[0m" exit Errno::NOERROR::Errno end db.transaction(isolation: :committed) do # Чистим зависшие запросы db[:requests].where(Sequel.|({ finished: nil }, Sequel.~({ busy: nil }))).delete end Sequel::DATABASES.each(&:disconnect) true end |
.db_create(config) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/inat-get/app/maintenance.rb', line 86 def db_create config connect_string = config.dig :database, :connect uri = URI.parse connect_string db_opts = { user: config.dig(:database, :user), password: config.dig(:database, :password) }.compact if file_based?(uri) path = File. uri.path if File.exist?(path) $stderr.puts "❌ \e[1mDatabase file already exists:\e[0m #{ path }" exit Errno::EEXIST::Errno end FileUtils.mkdir_p File.dirname(path) db = Sequel.connect(connect_string, **db_opts) db.disconnect puts "✅ \e[1mDatabase file created:\e[0m #{ path }" else begin db = Sequel.connect(connect_string, **db_opts) db.test_connection db.disconnect puts "✅ \e[1mDatabase connection verified (#{ uri.scheme })\e[0m" rescue => e $stderr.puts "❌ \e[1mCannot connect to database:\e[0m" $stderr.puts " #{ e. }" $stderr.puts " Please create database manually first" exit Errno::ECONNREFUSED::Errno end end begin run_migrations(config) rescue => e $stderr.puts "❌ \e[1mError while creating:\e[0m" $stderr.puts " #{ e. }" exit Errno::ECANCELED::Errno end puts "✅ \e[1mDatabase successfully created\e[0m" exit Errno::NOERROR::Errno end |
.db_migrate(config) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/inat-get/app/maintenance.rb', line 76 def db_migrate config target = config[:maintenance_params] unless target.is_a?(Integer) $stderr.puts "❌ \e[1mVersion must be an integer\e[0m" exit Errno::ECANCELED::Errno end run_migration config, target: target exit Errno::NOERROR::Errno end |
.db_reset(config) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/inat-get/app/maintenance.rb', line 124 def db_reset config connect_string = config.dig :database, :connect db_opts = { user: config.dig(:database, :user), password: config.dig(:database, :password) }.compact uri = URI.parse connect_string if file_based?(uri) path = File. uri.path if File.exist?(path) File.delete path puts "❌ \e[1mDatabase file removed:\e[0m #{ path }" end else begin db = Sequel.connect(connect_string, **db_opts) tables = db.tables if tables.any? mysql_mode = uri.scheme =~ /mysql/i if mysql_mode db.execute "SET FOREIGN_KEY_CHECKS = 0" end tables.each do |table| db.drop_table table, cascade: !mysql_mode end if mysql_mode db.execute "SET FOREIGN_KEY_CHECKS = 1" end puts "❌ \e[1mAll tables dropped (#{ tables.size }):\e[0m #{ tables.join(", ") }" else puts "🚨 \e[1mNo tables found in database\e[0m" end db.disconnect rescue => e $stderr.puts "❌ \e[1mError resetting database:\e[0m #{ e. }" exit Errno::ECONNREFUSED::Errno end end puts "🔄 \e[1mRecreating database...\e[0m" db_create(config) end |
.db_update(config) ⇒ Object
71 72 73 74 |
# File 'lib/inat-get/app/maintenance.rb', line 71 def db_update config run_migration config exit Errno::NOERROR::Errno end |
.file_based?(uri) ⇒ Boolean (private)
179 180 181 |
# File 'lib/inat-get/app/maintenance.rb', line 179 def file_based? uri uri.scheme.downcase == 'sqlite' end |
.info(config) ⇒ Object
29 30 31 |
# File 'lib/inat-get/app/maintenance.rb', line 29 def info config # TODO: implement end |
.migrations_path ⇒ Object (private)
183 184 185 |
# File 'lib/inat-get/app/maintenance.rb', line 183 def migrations_path @migrations_path ||= File.(File.join(File.dirname(__FILE__), '../../../share/inat-get/db/migrations/')) end |
.run_migrations(config, target: nil) ⇒ Object (private)
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/inat-get/app/maintenance.rb', line 165 def run_migrations config, target: nil connect_string = config.dig :database, :connect db_opts = { user: config.dig(:database, :user), password: config.dig(:database, :password) }.compact db = Sequel.connect(connect_string, **db_opts) opts = { target: target }.compact Sequel::Migrator.run(db, migrations_path, **opts) migrator = Sequel::Migrator.migrator_class(migrations_path).new(db, migrations_path) puts "✅ \e[1mDatabase migrated:\e[0m version \e[1m#{ migrator.current }\e[0m" db.disconnect rescue => e $stderr.puts "❌ \e[1mMigration error:\e[0m #{ e. }\n#{ e.backtrace.inspect }" exit Errno::ECANCELED::Errno end |
.show_config(config) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/inat-get/app/maintenance.rb', line 16 def show_config config config.delete :maintenance config.delete :maintenance_params config.delete :tasks if config[:database] && config[:database][:password] config[:database][:password] = '******' end puts "🌿 \e[1miNatGet Config:\e[0m" YAML.dump(config, $stdout, stringify_names: true) puts '---' exit Errno::NOERROR::Errno end |