MySQL to PostgreSQL

From Rixort Wiki
Jump to navigation Jump to search

Simple script for creating Result classes from an existing MySQL database, then producing a PostgreSQL schema file from those classes:

#!/usr/bin/perl

use Modern::Perl;
use DBIx::Class::Schema::Loader qw/ make_schema_at /;

my $dsn = 'dbi:mysql:dbname=database';
my $user = ;
my $pass = ;

make_schema_at(
  'MyDB::Schema',
  { debug => 1, dump_directory => './lib' },
  [ $dsn, $user, $pass
  ],
);

my $schema = MyDB::Schema->connect($dsn, $user, $pass);
$schema->create_ddl_dir(['PostgreSQL'], '0.1', './', undef, { add_drop_table => 0 });

Pre-migration checks

Two ways of testing to see whether data needs to be cleaned up before migrating:

  1. Quick scan of tables for obvious 'bad' values (e.g. DATE columns with '0000-00-00') using WHERE clauses - i.e. a blacklist.
  2. Exhaustive checks of every row - i.e. a whitelist.

Probably also a good idea to check foreign key relationships which have not explicitly been defined by the use of REFERENCES.

Reasons for pre-migration checks include:

  • MySQL is not as strict as PostgreSQL about validating data (or at least, not with the default MySQL configuration).
  • MySQL will usually enforce referential integrity if foreign keys are explicitly defined, but not all developers add the relevant REFERENCES constraints to their schemas.
  • Easier to identify and fix bad data whilst still using MySQL, and having a clean data set should make migration smoother.

Useful links