Filter tables
JoyceBabu opened this issue · 9 comments
Is it possible to filter tables when generating migrations? For example, I would like to filter all tables that match a particular pattern. Is it possible to filter the tables list using a call back function or regex pattern?
Hi @JoyceBabu
It's not possible to filter tables when generating migrations.
That would be a nice feature, of course. I will add this option in the next version.
I managed to do it by overriding MySqlSchemaAdapter::getTables
and GenerateCommand::execute
.
Are you interested in creating a PR?
I meant I created a new class extending Odan\Migration\Adapter\Database\MySqlSchemaAdapter
, overriding the getTables
method. Since the dba
object is directly initialized in Odan\Migration\Command\GenerateCommand::execute
, I had to extend GenerateCommand
class too.
Hence my changes won't be useful as PR.
It would be nice feature, if I could do this directly from phinx.php
, by specifying a callback function. If there was an option to initialize the SchemaAdapterInterface
object from phinx.php
, that would give even more power and flexibility to the user.
Ok, but please be careful. Technically it possible to overwrite protected methods, but they are not save against internal changes (refactoring) of the library. This could break your code, even in a patch version.
It would be nice feature, if I could do this directly from phinx.php
Yes, this is the reason I was asking for. :-)
Do you want to create a PR that implements this feature?
Sorry for not responding earlier. I did not get a chance to look into this again.
Since this is a cli tool, the callback approach is not very easy. It would require writing plugins, and passing the plugin name via cli. Also, I felt that would beat the purpose of this tool, which is to create migrations with minimal effort.
The solution that I ended up with was to create a .phinxignore
file with regular expressions for the tables/columns to be ignored.
# Empty lines and lines beginning with # are ignored
# Ignore tables beginning with _
^_
# Backup tables with date
_20\d{6}
# Backup tables ending in _bak/_copy/_temp/tmp/_old/_backup
_(?:bak|copy|te?mp|old|backup)$
# Tables containing digits
\d+
# Columns can be ignored using @
# Ignore columns beginning with _ in all tables
@^_
# Columns ending with _copy/_bak in all tables
@_(?:copy|bak)$
# Columns with digits, from tables ending with _log
_log$@\d
I just upgraded to v4.4
from v4.0
and found that my solution is broken, since it depended on internal APIs. I could try updating it to work with master branch, if the above solution seems acceptable to you.
Is it more user friendly to use glob patterns instead? I personally prefer regular expression, since it is more expressive. But glob patterns are very common in ignore files (For example.gitignore
, .dockerignore
, .ignore
used by ripgrep / ag), even though they all dealing with files.
My current solution only allows blacklists (I think whitelists can be still created using negative lookahead/lookbehind constructs). It would be useful to add a proper whitelist feature with !
, similar to .gitignore
format.
This tool uses the configuration of phinx and reads some additional keys from it. For the sake of simplicity, I would add such filter rules to the existing phinx configuration.
On a multi developer project, the ignore list will be developer specific and should not be version controlled. But since the phinx configuration file is version controlled, this might end up being a problem.
Ok, but please be careful. Technically it possible to overwrite protected methods, but they are not save against internal changes (refactoring) of the library. This could break your code, even in a patch version.
I am no longer able specify a custom schema adapter, since GenerateCommand
class has been declared final. Can you make the Schema Adapter class configurable?