Class: ERDB::SQL
Instance Method Summary collapse
-
#connect ⇒ Object
Connect to a database.
-
#disconnect ⇒ Object
Disconnect from a database.
-
#initialize(adapter, database) ⇒ SQL
constructor
Initialize a new SQL instance.
-
#to_erdb ⇒ Object
Convert database tables to ERD convertable Array.
Constructor Details
#initialize(adapter, database) ⇒ SQL
Initialize a new SQL instance.
9 10 11 12 13 14 |
# File 'lib/erdb/adapters/sql.rb', line 9 def initialize(adapter, database) super @db = ActiveRecord::Base @connection = nil end |
Instance Method Details
#connect ⇒ Object
Connect to a database.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/erdb/adapters/sql.rb', line 19 def connect puts "\n" puts "Connecting to #{@adapter} database..." case @adapter.to_sym when :sqlite3 connect_sqlite3 when :postgresql, :mysql2 connect_with_connection_string else raise "Adapter not supported." end @connection = @db.connection end |
#disconnect ⇒ Object
Disconnect from a database.
71 72 73 74 |
# File 'lib/erdb/adapters/sql.rb', line 71 def disconnect @db.remove_connection @connection = nil end |
#to_erdb ⇒ Object
Convert database tables to ERD convertable Array.
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 |
# File 'lib/erdb/adapters/sql.rb', line 38 def to_erdb puts "\nAnalyzing database..." raise "No tables found in database." if @connection.tables.empty? tables = @connection.tables.reject do |table| %w[schema_migrations ar_internal_metadata].include?(table) end tables.map do |table| columns = @connection.columns(table).map { |column| { name: column.name, type: column.type || "unknown" } } relations = @connection.foreign_keys(table).map do |fk| { from: { table: table, column: fk.[:column] }, to: { table: fk[:to_table], column: fk.[:primary_key] } } end hash = { name: table, columns: columns, relations: relations } hash[:is_junction_table] = junction_table?(hash) hash end end |