Class: RubynCode::DB::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/db/schema.rb

Overview

Provides schema introspection helpers and version checking for the database.

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • connection (Connection)

    the database connection



9
10
11
# File 'lib/rubyn_code/db/schema.rb', line 9

def initialize(connection)
  @connection = connection
end

Instance Method Details

#applied_versionsArray<Integer>

Returns all applied migration versions in order.

Returns:

  • (Array<Integer>)


28
29
30
31
32
33
34
# File 'lib/rubyn_code/db/schema.rb', line 28

def applied_versions
  @connection.query(
    'SELECT version FROM schema_migrations ORDER BY version'
  ).to_a.map { |row| row['version'] }
rescue StandardError
  []
end

#columns(table_name) ⇒ Array<Hash>

Returns column information for the given table.

Parameters:

  • table_name (String)

Returns:

  • (Array<Hash>)

    each hash has keys: cid, name, type, notnull, dflt_value, pk



63
64
65
# File 'lib/rubyn_code/db/schema.rb', line 63

def columns(table_name)
  @connection.query("PRAGMA table_info(#{quote_identifier(table_name)})").to_a
end

#current_versionInteger?

Returns the current schema version (highest applied migration).

Returns:

  • (Integer, nil)

    the version number, or nil if no migrations applied



16
17
18
19
20
21
22
23
# File 'lib/rubyn_code/db/schema.rb', line 16

def current_version
  row = @connection.query(
    'SELECT MAX(version) AS max_version FROM schema_migrations'
  ).to_a.first
  row && row['max_version']
rescue StandardError
  nil
end

#indexes(table_name) ⇒ Array<Hash>

Returns index information for the given table.

Parameters:

  • table_name (String)

Returns:

  • (Array<Hash>)


71
72
73
# File 'lib/rubyn_code/db/schema.rb', line 71

def indexes(table_name)
  @connection.query("PRAGMA index_list(#{quote_identifier(table_name)})").to_a
end

#table_exists?(table_name) ⇒ Boolean

Checks whether a given table exists in the database.

Parameters:

  • table_name (String)

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'lib/rubyn_code/db/schema.rb', line 79

def table_exists?(table_name)
  rows = @connection.query(
    "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?",
    [table_name]
  ).to_a
  !rows.empty?
end

#tablesArray<String>

Returns a list of table names in the database (excluding internal SQLite tables).

Returns:

  • (Array<String>)


52
53
54
55
56
57
# File 'lib/rubyn_code/db/schema.rb', line 52

def tables
  @connection.query(
    "SELECT name FROM sqlite_master WHERE type = 'table' " \
    "AND name NOT LIKE 'sqlite_%' ORDER BY name"
  ).to_a.map { |row| row['name'] }
end

#up_to_date?(migrator) ⇒ Boolean

Returns true if the schema is up to date with all available migrations.

Parameters:

Returns:

  • (Boolean)


91
92
93
# File 'lib/rubyn_code/db/schema.rb', line 91

def up_to_date?(migrator)
  migrator.pending_migrations.empty?
end

#version_applied?(version) ⇒ Boolean

Checks whether a specific migration version has been applied.

Parameters:

  • version (Integer)

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'lib/rubyn_code/db/schema.rb', line 40

def version_applied?(version)
  rows = @connection.query(
    'SELECT 1 FROM schema_migrations WHERE version = ?', [version]
  ).to_a
  !rows.empty?
rescue StandardError
  false
end