#!/usr/bin/perl

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Session;

my $schema = shift @ARGV;

$schema = defined $schema ? $schema : 'songspace';

my $dbh = Common::Session::getdbh('ingestdb');
my $sth = $dbh->prepare(qq{
SELECT
	concat_ws(',',
		'has_one',
		referenced_table_name,
		concat_ws('|',
			table_schema,
			referenced_table_name,
			referenced_column_name
		),
		`column_name`
	)
FROM
	information_schema.key_column_usage
WHERE 1
	AND table_schema='$schema'
	AND `constraint_name` != 'PRIMARY'
	AND referenced_table_name IS NOT null
	AND `table_name`=?
});
my $tabs = $dbh->selectcol_arrayref("select table_name from information_schema.tables where table_schema='$schema'");
for my $tab (sort @{$tabs}) {
	$sth->execute($tab);
	if ( $sth->rows ) {
		print "$tab:\n";
		while(my $row = $sth->fetchrow_arrayref) {
			my $yml = $row->[0];
			print "  - \'$yml\'\n";
		}
		print "\n";
	}
}
