-
copy dbinfo.rake to lib/tasks/dbinfo.rake and run
rake db:info
from command line -
Your *md file appears in guide/database/structure_report.md folder, create it before run
Last active
August 24, 2016 05:21
-
-
Save bsa7/3480f4e13f00d54d4cb87bfd4077c8aa to your computer and use it in GitHub Desktop.
ActiveRecord database info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :db do | |
task info: [:environment] do | |
tables_reflections = {} | |
Rails.application.eager_load! | |
models_created_by_user = ActiveRecord::Base.descendants | |
models_created_by_user.sort_by { |model| model.table_name || model.name }.each do |model| | |
begin | |
tables_reflections[model.table_name] = {} | |
model.reflections.keys.each do |reflection_key| | |
reflection = model.reflections[reflection_key] | |
foreign_key = reflection.foreign_key.to_s | |
tables_reflections[model.table_name][foreign_key] ||= [] | |
tables_reflections[model.table_name][foreign_key] << { | |
table_name: reflection.table_name, | |
macro: reflection.macro | |
} | |
end | |
rescue | |
next | |
end | |
end | |
Rails.logger.ap tables_reflections: tables_reflections | |
File.open('guide/database/structure_report.md', 'w') do |dbinfo_file| | |
db_info_header = [ | |
"## Структура данных на #{Time.now}\n\n", | |
"__Этот файл может автоматически обновляться.__\n\n", | |
"Для обновления используйте `rake db:info` в командной строке.\n\n" | |
] | |
dbinfo_file.write db_info_header.join | |
ActiveRecord::Base.connection.tables.sort_by { |table_name| table_name }.each do |table_name| | |
puts "Сбор сведений о таблице #{table_name} ..." | |
table_columns = ActiveRecord::Base.connection.columns(table_name) | |
records_count_query_result = ActiveRecord::Base.connection.execute("SELECT COUNT(*) FROM #{table_name}").entries[0] || {} | |
records_count = records_count_query_result[0] || records_count_query_result['count'] || 0 | |
dbinfo_file.write "\n #### <a name='table_#{table_name}'>#{table_name}</a>: #{records_count || 0} lines \n\n" | |
if table_columns.find { |x| x.name == 'created_at' } | |
last_records_query_result = ActiveRecord::Base.connection.execute("SELECT created_at FROM #{table_name} ORDER BY created_at DESC LIMIT 1").entries[0] || {} | |
datetime = last_records_query_result[-1] || last_records_query_result['created_at'] || 'No records found' | |
dbinfo_file.write " * __last_record__: '#{datetime}'\n" | |
end | |
table_reflections = tables_reflections[table_name] || {} | |
table_columns.sort { |x,y| x.name <=> y.name }.each do |column| | |
index_list = ActiveRecord::Base.connection.indexes(table_name) | |
index_list.reject! do |table_index| | |
!table_index.columns.include? column.name | |
end | |
indexes = index_list.present? ? " #`#{index_list.map(&:name).join('`, `')}`" : '' | |
dbinfo_file.write " * __#{column.name}__: #{column.sql_type}#{indexes}\n" | |
(table_reflections[column.name] || []).each do |column_reflection| | |
dbinfo_file.write " * #{column_reflection[:macro]}: [#{column_reflection[:table_name]}](#table_#{column_reflection[:table_name]})\n" | |
end | |
end | |
end | |
end | |
end | |
end |
Этот файл может автоматически обновляться.
Для обновления используйте rake db:info
в командной строке.
abilities: 0 lines
- last_record: 'No records found'
- action_name: character varying
- controller_name: character varying
- created_at: timestamp without time zone
- id: integer
- is_can: boolean
- updated_at: timestamp without time zone
ar_internal_metadata: 1 lines
- last_record: '2016-07-30 19:16:45.440946'
- created_at: timestamp without time zone
- key: character varying
- updated_at: timestamp without time zone
- value: character varying
proxies: 4595 lines
- last_record: '2016-08-01 02:25:14'
- created_at: timestamp without time zone
- id: integer
- ip_port: character varying
- success_attempts_count: integer #
index_proxies_on_success_attempts_count
- total_attempts_count: integer #
index_proxies_on_total_attempts_count
- updated_at: timestamp without time zone
roles: 0 lines
- last_record: 'No records found'
- created_at: timestamp without time zone
- id: integer
- label: character varying
- updated_at: timestamp without time zone
roles_users: 0 lines
- last_record: 'No records found'
- created_at: timestamp without time zone
- id: integer
- role_id: integer #
index_roles_users_on_role_id
- belongs_to: roles
- updated_at: timestamp without time zone
- user_id: integer #
index_roles_users_on_user_id
- belongs_to: users
schema_migrations: 5 lines
- version: character varying
users: 1 lines
- last_record: '2016-07-31 09:22:44.44976'
- created_at: timestamp without time zone
- current_sign_in_at: timestamp without time zone
- current_sign_in_ip: inet
- email: character varying #
index_users_on_email
- encrypted_password: character varying
- id: integer
- last_sign_in_at: timestamp without time zone
- last_sign_in_ip: inet
- remember_created_at: timestamp without time zone
- reset_password_sent_at: timestamp without time zone
- reset_password_token: character varying #
index_users_on_reset_password_token
- sign_in_count: integer
- updated_at: timestamp without time zone
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment