Skip to content

Instantly share code, notes, and snippets.

@throughnothing
Created June 2, 2012 14:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save throughnothing/2858724 to your computer and use it in GitHub Desktop.
Save throughnothing/2858724 to your computer and use it in GitHub Desktop.
DBIx::Class::DeploymentHandler Pkey + Ordering Test

Primary Key/Ordering test for DBIx::Class::DeploymentHandler

  • Create the version 1 tables in a postgres schema, then create DBIC classes with dbicdump.sh
  • run ./gen_migration.pl 1
  • Drop tables, and create version 2 tables from db.sql
  • run ./gen_migration.pl 2
  • Check sql/PostgreSQL/upgrade/1-2/001-auto.sql
# Original Tables (Version 1)
CREATE TABLE test (
id SERIAL PRIMARY KEY,
code VARCHAR(255) UNIQUE NOT NULL,
discount INT NOT NULL DEFAULT 0
);
CREATE TABLE test2 (
id SERIAL PRIMARY KEY,
test_code VARCHAR(255)
);
# Version 2, after changes
CREATE TABLE test (
code VARCHAR(255) PRIMARY KEY,
discount INT NOT NULL DEFAULT 0
);
CREATE TABLE test2 (
id SERIAL PRIMARY KEY,
test_code VARCHAR(255),
FOREIGN KEY (test_code) REFERENCES test (code)
);
#!/bin/bash
dbicdump Test::Schema 'dbi:Pg:dbname=test'
#!/usr/bin/env perl
use strict;
use warnings;
use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
use Test::Schema;
my $schema = Test::Schema->connect('dbi:Pg:dbname=test', 'postgres');
my $version = schema->VERSION || $ARGV[0];
print "Version: $version\n";
my $dh = DH->new({
schema => $schema,
force_overwrite => 1,
databases => [qw/ PostgreSQL /],
sql_translator_args => { add_drop_table => 0 },
});
$dh->prepare_install;
if( $version > 1 ) {
$dh->prepare_upgrade({
from_version => $version - 1,
to_version => $version,
version_set => [ $version - 1, $version ],
});
$dh->prepare_downgrade({
from_version => $version,
to_version => $version - 1,
version_set => [ $version - 1, $version ],
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment