Created
September 1, 2016 17:20
-
-
Save yosukehasumi/46fdf97e11cec5d612807c8fd64c3b0e to your computer and use it in GitHub Desktop.
Pull database field relationships from Drupal
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
#!/usr/bin/env ruby | |
require 'mysql2' | |
require 'awesome_print' | |
require 'colorize' | |
db_name = 'db_name' | |
db_host = 'localhost' | |
db_username = 'db_user' | |
db_password = 'db_pass' | |
client = Mysql2::Client.new(host: db_host, username: db_username, password: db_password, database: db_name) | |
puts "Connected to database \"#{db_name}\"".green | |
content_type = ARGV[0] | |
abort("ERROR: MISSING CONTENT TYPE ARG --- dbpenpull [content_type]") if content_type.nil? | |
fields = client.query("SELECT field_config_instance.field_name FROM field_config_instance WHERE bundle = '#{content_type}'") | |
joins = fields.collect do |field| | |
"LEFT JOIN field_data_#{field['field_name']} | |
ON node.nid=field_data_#{field['field_name']}.entity_id AND field_data_#{field['field_name']}.bundle='#{content_type}'" | |
end | |
query = " | |
SELECT | |
* | |
FROM | |
node | |
#{joins.join("\n")} | |
WHERE | |
node.type='#{content_type}'".red | |
puts query |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment