Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude table #92

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ The following command-line options are provided (all are optional):
* `view`: view comparison (default: true).
* `include-schema`: include schemas that match pattern (default: all schemas).
* `exclude-schema`: exclude schemas that match pattern (default: none).
* `include-table`: include tables that match pattern (default: all schemas).
* `exclude-table`: exclude tables that match pattern (default: none).
* `exclude-column`: exclude columns that match pattern (default: none).
* `exclude-table-column`: exclude table.columns that match pattern (default: none) Only for PostgreSQL 9.4+.

You can use a configuration file to store the desired options. The _general_ section specifies which kind of objects will be output. The _target_ and _source_ section options specifies connection options to both servers. Have in mind that any command-line option can override the configuration file option. The configuration file contains the following structure:

Expand Down Expand Up @@ -411,6 +415,10 @@ view = true

include-schema = ^(hr|finance|account|crm|sales)$
exclude-schema = ^public$
include-table = ^(table_a|table_b)$
exclude-table = ^(table_c|table_d)$
exclude-column = ^(column_c|column_d)$
exclude-table-column = [{"t":"table_e","c":"column_e"}]

[target]
host = 10.27.0.8
Expand Down
8 changes: 8 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ typedef struct QuarrelGeneralOptions
/* filter options */
char *include_schema;
char *exclude_schema;
char *include_table;
char *exclude_table;
char *exclude_column;
char *exclude_table_column;
} QuarrelGeneralOptions;

typedef struct QuarrelDatabaseOptions
Expand Down Expand Up @@ -183,6 +187,10 @@ extern QuarrelGeneralOptions options;

extern char *include_schema_str;
extern char *exclude_schema_str;
extern char *include_table_str;
extern char *exclude_table_str;
extern char *exclude_column_str;
extern char *exclude_table_column_str;

typedef struct stringListCell
{
Expand Down
104 changes: 102 additions & 2 deletions src/quarrel.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ QuarrelGeneralOptions options; /* general options */
/* filter by schema */
char *include_schema_str;
char *exclude_schema_str;
char *include_table_str;
char *exclude_table_str;
char *exclude_column_str;
char *exclude_table_column_str;

PQLStatistic qstat = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand Down Expand Up @@ -300,8 +304,12 @@ help(void)
printf(" --view=BOOL view (default: %s)\n",
(opts.general.view) ? "true" : "false");
printf("\nFilter options:\n");
printf(" --include-schema=PATTERN include schemas that match PATTERN (default: all schemas)\n");
printf(" --exclude-schema=PATTERN exclude schemas that match PATTERN (default: none)\n");
printf(" --include-schema=PATTERN include schemas that match PATTERN (default: all schemas)\n");
printf(" --exclude-schema=PATTERN exclude schemas that match PATTERN (default: none)\n");
printf(" --include-table=PATTERN include tables that match PATTERN (default: none)\n");
printf(" --exclude-table=PATTERN exclude tables that match PATTERN (default: none)\n");
printf(" --exclude-column=PATTERN exclude columns that match PATTERN (default: none)\n");
printf(" --exclude-table-column=PATTERN exclude table.columns that match PATTERN (default: none) Only for PostgreSQL 9.4+\n");
printf("\nSource options:\n");
printf(" --source-dbname=DBNAME database name or connection string\n");
printf(" --source-host=HOSTNAME server host or socket directory\n");
Expand Down Expand Up @@ -373,6 +381,10 @@ loadConfig(const char *cf, QuarrelOptions *options)

options->general.include_schema = NULL; /* general - include schemas that match pattern */
options->general.exclude_schema = NULL; /* general - exclude schemas that match pattern */
options->general.include_table = NULL; /* general - include tables that match pattern */
options->general.exclude_table = NULL; /* general - exclude tables that match pattern */
options->general.exclude_column = NULL; /* general - exclude columns that match pattern */
options->general.exclude_table_column = NULL; /* general - exclude table.columns that match pattern */

options->source.host = NULL; /* source - host */
options->source.port = NULL; /* source - port */
Expand Down Expand Up @@ -623,6 +635,22 @@ loadConfig(const char *cf, QuarrelOptions *options)
if (tmp != NULL)
options->general.exclude_schema = strdup(tmp);

tmp = mini_file_get_value(config, "general", "include-table");
if (tmp != NULL)
options->general.include_table = strdup(tmp);

tmp = mini_file_get_value(config, "general", "exclude-table");
if (tmp != NULL)
options->general.exclude_table = strdup(tmp);

tmp = mini_file_get_value(config, "general", "exclude-column");
if (tmp != NULL)
options->general.exclude_column = strdup(tmp);

tmp = mini_file_get_value(config, "general", "exclude-table-column");
if (tmp != NULL)
options->general.exclude_table_column = strdup(tmp);

/* source options */
tmp = mini_file_get_value(config, "source", "host");
if (tmp != NULL)
Expand Down Expand Up @@ -4671,6 +4699,10 @@ int main(int argc, char *argv[])
{"temp-directory", required_argument, NULL, 37},
{"include-schema", required_argument, NULL, 45},
{"exclude-schema", required_argument, NULL, 46},
{"include-table", required_argument, NULL, 47},
{"exclude-table", required_argument, NULL, 48},
{"exclude-column", required_argument, NULL, 49},
{"exclude-table-column", required_argument, NULL, 50},
{NULL, 0, NULL, 0}
};

Expand All @@ -4685,6 +4717,10 @@ int main(int argc, char *argv[])
bool target_prompt_given = false;
bool include_schema_given = false;
bool exclude_schema_given = false;
bool include_table_given = false;
bool exclude_table_given = false;
bool exclude_column_given = false;
bool exclude_table_column_given = false;

/* general and connection options */
QuarrelOptions opts;
Expand Down Expand Up @@ -4922,6 +4958,22 @@ int main(int argc, char *argv[])
gopts.exclude_schema = strdup(optarg);
exclude_schema_given = true;
break;
case 47:
gopts.include_table = strdup(optarg);
include_table_given = true;
break;
case 48:
gopts.exclude_table = strdup(optarg);
exclude_table_given = true;
break;
case 49:
gopts.exclude_column = strdup(optarg);
exclude_column_given = true;
break;
case 50:
gopts.exclude_table_column = strdup(optarg);
exclude_table_column_given = true;
break;
default:
fprintf(stderr, "Try \"%s --help\" for more information.\n", PGQ_NAME);
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -5022,6 +5074,14 @@ int main(int argc, char *argv[])
options.include_schema = gopts.include_schema;
if (exclude_schema_given)
options.exclude_schema = gopts.exclude_schema;
if (include_table_given)
options.include_table = gopts.include_table;
if (exclude_table_given)
options.exclude_table = gopts.exclude_table;
if (exclude_column_given)
options.exclude_column = gopts.exclude_column;
if (exclude_table_column_given)
options.exclude_table_column = gopts.exclude_table_column;

if (sopts.dbname)
opts.source.dbname = sopts.dbname;
Expand Down Expand Up @@ -5155,6 +5215,46 @@ int main(int argc, char *argv[])
{
exclude_schema_str = "";
}
if (options.include_table != NULL)
{
include_table_str = psprintf(" AND c.relname ~ '%s'", options.include_table);

logNoise("filter include table: %s", include_table_str);
}
else
{
include_table_str = "";
}
if (options.exclude_table != NULL)
{
exclude_table_str = psprintf(" AND c.relname !~ '%s'", options.exclude_table);

logNoise("filter exclude table: %s", exclude_table_str);
}
else
{
exclude_table_str = "";
}
if (options.exclude_column != NULL)
{
exclude_column_str = psprintf(" AND a.attname !~ '%s'", options.exclude_column);

logNoise("filter exclude column: %s", exclude_column_str);
}
else
{
exclude_column_str = "";
}
if (options.exclude_table_column != NULL)
{
exclude_table_column_str = psprintf("%s", options.exclude_table_column);

logNoise("filter exclude table-column: %s", exclude_table_column_str);
}
else
{
exclude_table_column_str = "";
}

/*
* We cannot start sending everything to stdout here because we will have
Expand Down
Loading