instance method table_structures

Ruby on Rails edge

Since edge Private — implementation detail, not part of the public API

Signature

table_structures(tables)

The pragmas are table valued functions as well as statements, so they can be joined to a list of names and read for many tables at once. See www.sqlite.org/pragma.html#pragfunc

The statement a table was created with comes along because the collation, auto increment and generated columns are only named there. It repeats once per column, so it is read from the first row. A view has a statement too, but it describes the query rather than any columns.

Parameters

tables req
Source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 939
        def table_structures(tables)
          return {} if tables.empty?

          fields = ["name", "type", "notnull", "dflt_value", "pk"]
          fields << "hidden" if supports_virtual_columns?

          query_all(<<~SQL).group_by { |row| row["table_name"] }
            #{MASTER_CTE}
            SELECT m.name AS table_name, CASE WHEN m.type = 'table' THEN m.sql END AS create_table_sql,
                   #{fields.map { |field| "t.#{quote_column_name(field)}" }.join(", ")}
            FROM master m
            JOIN pragma_#{table_info_pragma}(m.name) t
            WHERE m.type IN ('table', 'view')
              AND m.name IN (#{quoted_table_names(tables)})
            ORDER BY m.name, t.cid
          SQL
            .transform_values do |group|
              [group.first["create_table_sql"], group.map { |row| row.except("table_name", "create_table_sql") }]
            end
        end

Defined in activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb line 939 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::ConnectionAdapters::SQLite3Adapter

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close