anguenot / roachize

Convert a pg_dump file in sql format to work against cockroachdb

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Roachize

Usage

$ pg_dump -h postgreshost -f mydb.sql mydb
$ perl roachize.pl --source mydb.sql --dest mydb_for_cockroach.sql
$ psql -h cockroachhost ... -d mydb < mydb_for_cockroach.sql

This is a HACK script to take a SQL (default) format output of pg_dump and emit a modified version that can be run against a cockroachdb instance. It is probably dependent on the versions of postgres I'm using and cockroach - 9.6 and 1.1.3

It depends on the structure of the SQL output from the pg_dump comand so isn't going to work on a random sql file of postgres commands.

What it does:

Processes the entire file looking for the primary key definitions to hoist into the CREATE TABLE expressions- pg_dump creates the tables and adds the primary key constraints later. Cockroachdb requires the primary key to be defined at table creation. It also notices the creation of foreign key constraints and remembers them to explicitly create indexes for them. Postgres auto-creates them but cockroach does not and fails creating the constraints if the indexes do not exist. It dumps the DDL for them as a block when it finds an explicit CREATE INDEX which means this is after all tables have been created in a pg_dump

It then rewinds and starts processing the the file again, emitting the altered CREATE TABLE commands and changing any primary keys that are integers into SERIAL.

It comments out:

  CREATE SEQUENCE
  ALTER TABLE ... PRIMARY KEY statemnts as they are already defined
  GRANT
  REVOKE
  COMMENT ON
  ALTER SEQUENCE
  SET
  CREATE EXTENSION
  ALTER TABLE \S+ OWNER
  ALTER TABLE.*nextval
  SELECT pg_catalog)/-- \1/;

It removes the following unsupported expressions:

  USING btree
   DEFERRABLE INITIALLY DEFERRED
   ON DELETE SET NULL;

Omitting the data

You can skip the COPY sections during debugging to speed up imports using the --omitdata option.

About

Convert a pg_dump file in sql format to work against cockroachdb

License:The Unlicense


Languages

Language:Perl 100.0%