Class: Ragdoll::Core::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/ragdoll/core/database.rb

Class Method Summary collapse

Class Method Details

.connected?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/ragdoll/core/database.rb', line 120

def self.connected?
  ActiveRecord::Base.connected?
end

.default_configObject



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ragdoll/core/database.rb', line 128

def self.default_config
  {
    adapter: "postgresql",
    database: "ragdoll_development",
    username: "ragdoll",
    password: ENV.fetch("RAGDOLL_DATABASE_PASSWORD", nil),
    host: "localhost",
    port: 5432,
    auto_migrate: true,
    logger: Logger.new($stdout, level: Logger::WARN)
  }
end

.disconnect!Object



124
125
126
# File 'lib/ragdoll/core/database.rb', line 124

def self.disconnect!
  ActiveRecord::Base.clear_all_connections!
end

.migrate!Object



24
25
26
27
28
29
30
31
32
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ragdoll/core/database.rb', line 24

def self.migrate!
  # Get the path to the gem root directory
  # Current file is lib/ragdoll/core/database.rb, so go up 3 levels to get to gem root
  gem_root = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", ".."))
  migration_paths = [
    File.join(gem_root, "db", "migrate")
  ]

  ActiveRecord::Migration.verbose = true

  # Ensure schema_migrations table exists first
  unless ActiveRecord::Base.connection.table_exists?("schema_migrations")
    ActiveRecord::Base.connection.create_table("schema_migrations", id: false) do |t|
      t.string :version, null: false
    end
    ActiveRecord::Base.connection.add_index("schema_migrations", :version, unique: true)
  end

  # Debug migration path (silenced for clean test output)
  # puts "Migration path: #{migration_paths.first}" if ActiveRecord::Migration.verbose
  migration_files = Dir[File.join(migration_paths.first, "*.rb")].sort
  # puts "Found #{migration_files.length} migration files" if ActiveRecord::Migration.verbose

  # Load and run each migration manually since ActiveRecord migration context seems broken
  migration_files.each do |migration_file|
    # Extract version from filename
    version = File.basename(migration_file, ".rb").split("_").first

    # Skip if already migrated
    next if ActiveRecord::Base.connection.select_values(
      "SELECT version FROM schema_migrations WHERE version = '#{version}'"
    ).any?

    # Load the migration file to define the class
    require migration_file

    # Get the migration class - convert snake_case to CamelCase
    filename_parts = File.basename(migration_file, ".rb").split("_")[1..]
    migration_class_name = filename_parts.map(&:capitalize).join

    begin
      migration_class = Object.const_get(migration_class_name)
    rescue NameError
      puts "Warning: Could not find migration class #{migration_class_name} in #{migration_file}"
      next
    end

    # Run the migration quietly
    old_verbose = ActiveRecord::Migration.verbose
    ActiveRecord::Migration.verbose = false
    migration_class.migrate(:up)
    ActiveRecord::Migration.verbose = old_verbose

    # Record the migration
    ActiveRecord::Base.connection.insert(
      "INSERT INTO schema_migrations (version) VALUES ('#{version}')"
    )

    # Silenced migration progress - uncomment for debugging
    # puts "Migrated #{migration_class_name}" if ActiveRecord::Migration.verbose
  end
end

.migration_pathsObject



141
142
143
# File 'lib/ragdoll/core/database.rb', line 141

def self.migration_paths
  [File.join(File.dirname(__FILE__), "..", "..", "..", "..", "db", "migrate")]
end

.reset!Object



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
# File 'lib/ragdoll/core/database.rb', line 87

def self.reset!
  ActiveRecord::Migration.verbose = false

  # Drop all tables in correct order (respecting foreign key constraints)
  # Order: dependent tables first, then parent tables
  tables_to_drop = %w[
    ragdoll_search_results
    ragdoll_searches
    ragdoll_embeddings
    ragdoll_contents
    ragdoll_documents
    schema_migrations
  ]

  tables_to_drop.each do |table|
    if ActiveRecord::Base.connection.table_exists?(table)
      # For PostgreSQL, we can use CASCADE to drop dependent objects
      if ActiveRecord::Base.connection.adapter_name.downcase.include?("postgresql")
        ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{table} CASCADE")
      else
        ActiveRecord::Base.connection.drop_table(table)
      end
    end
  end

  # Also drop any functions/triggers that might exist
  if ActiveRecord::Base.connection.adapter_name.downcase.include?("postgresql")
    ActiveRecord::Base.connection.execute("DROP FUNCTION IF EXISTS ragdoll_documents_vector_update() CASCADE")
  end

  migrate!
end

.setup(config = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ragdoll/core/database.rb', line 9

def self.setup(config = {})
  database_config = default_config.merge(config)

  # Set up ActiveRecord connection
  ActiveRecord::Base.establish_connection(database_config)

  # Set up logging if specified
  ActiveRecord::Base.logger = database_config[:logger] if database_config[:logger]

  # Auto-migrate if specified
  return unless database_config[:auto_migrate]

  migrate!
end