NAME

DBIx::Diff::Schema - Compare schema of two DBI databases

VERSION

This document describes version 0.095 of DBIx::Diff::Schema (from Perl distribution DBIx-Diff-Schema), released on 2020-04-23.

SYNOPSIS

use DBIx::Diff::Schema qw(diff_db_schema diff_table_schema
                          db_schema_eq table_schema_eq);

To compare schemas of whole databases:

my $res = diff_db_schema($dbh1, $dbh2);
say "the two dbs are equal" if db_schema_eq($dbh1, $dbh2);

To compare schemas of a single table from two databases:

my $res = diff_table_schema($dbh1, $dbh2, 'tablename');
say "the two tables are equal" if table_schema_eq($dbh1, $dbh2, 'tablename');

DESCRIPTION

Currently only tested on Postgres and SQLite.

FUNCTIONS

check_table_exists

Usage:

check_table_exists($dbh, $table) -> any

Check whether a table exists.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $dbh* => obj

    DBI database handle.

  • $table* => str

    Table name.

Return value: (any)

db_schema_eq

Usage:

db_schema_eq($dbh1, $dbh2) -> any

Return true if two DBI databases have the same schema.

This is basically just a shortcut for:

my $res = diff_db_schema(...);
!%$res;

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $dbh1* => obj

    DBI database handle for the first database.

  • $dbh2* => obj

    DBI database handle for the second database.

Return value: (any)

diff_db_schema

Usage:

diff_db_schema($dbh1, $dbh2) -> any

Compare schemas of two DBI databases.

This function compares schemas of two DBI databases. You supply two DBI database handles and this function will return a hash:

{
    # list of tables found in first db but missing in second
    deleted_tables => ['table1', ...],

    # list of tables found only in the second db
    added_tables => ['table2', ...],

    # list of modified tables, with details for each
    modified_tables => {
        table3 => {
            deleted_columns => [...],
            added_columns => [...],
            modified_columns => {
                column1 => {
                    old_type => '...',
                    new_type => '...',
                    ...
                },
            },
        },
    },
}

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $dbh1* => obj

    DBI database handle for the first database.

  • $dbh2* => obj

    DBI database handle for the second database.

Return value: (any)

diff_table_schema

Usage:

diff_table_schema($dbh1, $dbh2, $table1, $table2) -> any

Compare schema of two DBI tables.

This function compares schemas of two DBI tables. You supply two DBI database handles along with table name and this function will return a hash:

{
    deleted_columns => [...],
    added_columns => [...],
    modified_columns => {
        column1 => {
            old_type => '...',
            new_type => '...',
            ...
        },
    },
}

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $dbh1* => obj

    DBI database handle for the first database.

  • $dbh2* => obj

    DBI database handle for the second database.

  • $table1* => str

    Table name.

  • $table2 => str

    Second table name (assumed to be the same as first table name if unspecified).

Return value: (any)

list_columns

Usage:

list_columns($dbh, $table) -> any

List columns of a table.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $dbh* => obj

    DBI database handle.

  • $table* => str

    Table name.

Return value: (any)

list_indexes

Usage:

list_indexes($dbh, $table) -> any

List indexes for a table in a database.

General notes: information is retrieved from DBI's table_info().

SQLite notes: autoindex for primary key is also listed as the first index, if it exists. This information is retrieved using "SELECT * FROM sqlite_master". Autoindex is not listed using table_info().

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $dbh* => obj

    DBI database handle.

  • $table* => str

    Table name.

Return value: (any)

list_table_indexes

Usage:

list_table_indexes($dbh, $table) -> any

List indexes for a table in a database.

General notes: information is retrieved from DBI's table_info().

SQLite notes: autoindex for primary key is also listed as the first index, if it exists. This information is retrieved using "SELECT * FROM sqlite_master". Autoindex is not listed using table_info().

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $dbh* => obj

    DBI database handle.

  • $table* => str

    Table name.

Return value: (any)

list_tables

Usage:

list_tables($dbh) -> any

List table names in a database.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $dbh* => obj

    DBI database handle.

Return value: (any)

table_schema_eq

Usage:

table_schema_eq($dbh1, $dbh2, $table1, $table2) -> any

Return true if two DBI tables have the same schema.

This is basically just a shortcut for:

my $res = diff_table_schema(...);
!%res;

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $dbh1* => obj

    DBI database handle for the first database.

  • $dbh2* => obj

    DBI database handle for the second database.

  • $table1* => str

    Table name.

  • $table2 => str

    Second table name (assumed to be the same as first table name if unspecified).

Return value: (any)

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/DBIx-Diff-Schema.

SOURCE

Source repository is at https://github.com/perlancar/perl-DBIx-Diff-Schema.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=DBIx-Diff-Schema

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

DBIx::Compare to compare database contents.

diffdb from App::diffdb which can compare two database (schema as well as content) and display the result as the familiar colored unified-style diff.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2020, 2018, 2017, 2015, 2014 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.