How to use the dbicdump to only dump specific table
Asked Answered
S

1

5

I just need to dump specific tables from my database such that these specific tables (3 tables to be exact out of 200 tables) will now be implemented by DBIx::Class::Schema.

Here is the command from the docs (https://metacpan.org/pod/dbicdump):

dbicdump -o dump_directory=./lib   -o components='["InflateColumn::DateTime"]'   -o preserve_case=1   MyApp::Schema dbi:mysql:database=database_name user pass;

I tried appending the table name after the database_name but no luck, it still dumps all the tables in the specified database. Need help. I can't find anything in the docs.

Also Out of topic question :

What does these means? -o components='["InflateColumn::DateTime"]' -o preserve_case=1 I also cant find their explanation in the docs.

Thanks

Shatter answered 2/3, 2016 at 12:52 Comment(1)
From the link you posted "You can pass any DBIx::Class::Schema::Loader::Base constructor option using -o <option>=<value>." Following the provided link, you get "Normally database names are lowercased and split by underscore, use [-o preserve_case=1] if you have CamelCase database names." As for InflateColumn::DateTime, "Auto-create DateTime objects from date and datetime columns."Noriega
S
7

You can pass the option constraint to the underlying DBIx::Class::Schema::Loader instance so it only selects certain tables. The documentation is a bit vague on this.

These can be specified either as a regex (preferrably on the qr// form), or as an arrayref of arrayrefs. Regexes are matched against the (unqualified) table name, while arrayrefs are matched according to "moniker_parts".

For example:

db_schema => [qw(some_schema other_schema)],
moniker_parts => [qw(schema name)],
constraint => [
    [ qr/\Asome_schema\z/ => qr/\A(?:foo|bar)\z/ ],
    [ qr/\Aother_schema\z/ => qr/\Abaz\z/ ],
],

In this case only the tables foo and bar in some_schema and baz in other_schema will be dumped.

So what you need to pass to dbicdump would look something like this.

dbicdump \
  -o dump_directory=./lib \
  -o components='["InflateColumn::DateTime"]' \
  -o preserve_case=1 \
  -o constraint='qr/^(?:foo|bar|baz)$/' \
   MyApp::Schema dbi:mysql:database=database_name user pass;

That will only give you the tables foo, bar and baz. You need the quoted regular expression without array refs if there is only one schema, and you don't want to use manually preset monikers (which are names used for tables in the generated Schema class).

Sori answered 2/3, 2016 at 13:12 Comment(2)
Hi what do you mean by if there is only one schema? Am i missing something? Should i create this ONE SCHEMA? And how? I though that command relies entirely on the database schema. ThanksShatter
@Shatter I was referring to the rather confusing documentation quote, where they dump two DBs into two Schema classes at the same time. Just run the command I posted. I tested it. A simple way to verify is to run tree on the MyApp/Schema folder. If it contains a load of files, it didn't work. ;)Sori

© 2022 - 2024 — McMap. All rights reserved.