Class: RubynCode::DB::Schema
- Inherits:
-
Object
- Object
- RubynCode::DB::Schema
- Defined in:
- lib/rubyn_code/db/schema.rb
Overview
Provides schema introspection helpers and version checking for the database.
Instance Method Summary collapse
-
#applied_versions ⇒ Array<Integer>
Returns all applied migration versions in order.
-
#columns(table_name) ⇒ Array<Hash>
Returns column information for the given table.
-
#current_version ⇒ Integer?
Returns the current schema version (highest applied migration).
-
#indexes(table_name) ⇒ Array<Hash>
Returns index information for the given table.
-
#initialize(connection) ⇒ Schema
constructor
A new instance of Schema.
-
#table_exists?(table_name) ⇒ Boolean
Checks whether a given table exists in the database.
-
#tables ⇒ Array<String>
Returns a list of table names in the database (excluding internal SQLite tables).
-
#up_to_date?(migrator) ⇒ Boolean
Returns true if the schema is up to date with all available migrations.
-
#version_applied?(version) ⇒ Boolean
Checks whether a specific migration version has been applied.
Constructor Details
#initialize(connection) ⇒ Schema
Returns a new instance of Schema.
9 10 11 |
# File 'lib/rubyn_code/db/schema.rb', line 9 def initialize(connection) @connection = connection end |
Instance Method Details
#applied_versions ⇒ Array<Integer>
Returns all applied migration versions in order.
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.
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_version ⇒ Integer?
Returns the current schema version (highest applied migration).
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.
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.
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 |
#tables ⇒ Array<String>
Returns a list of table names in the database (excluding internal SQLite tables).
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.
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.
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 |