#!/usr/bin/env ruby
# frozen_string_literal: true

require 'sequel'
require 'fileutils'
require_relative 'config/settings'

# 简单的迁移脚本，不依赖 boot.rb
db_path = Settings.instance.database_path
FileUtils.mkdir_p(File.dirname(db_path))

# SQLite 连接字符串
db_path_for_uri = db_path.gsub('\\', '/')
if db_path_for_uri.match?(/^[A-Za-z]:\//)
  connection_string = "sqlite:///#{db_path_for_uri}"
else
  connection_string = "sqlite://#{db_path_for_uri}"
end

db = Sequel.connect(connection_string)

puts "检查数据库表结构..."

# 检查是否有 encrypted_root_password 列
columns = db.schema(:local_devices).map { |c| c[0] }
if columns.include?(:encrypted_root_password)
  puts "✓ encrypted_root_password 字段已存在"
else
  puts "添加 encrypted_root_password 字段..."
  db.alter_table :local_devices do
    add_column :encrypted_root_password, String
  end
  puts "✓ 成功添加 encrypted_root_password 字段"
end

puts "\n迁移完成！"
