Skip to content

Instantly share code, notes, and snippets.

@jjn1056
Created April 12, 2020 12:48
Show Gist options
  • Save jjn1056/2e44b79155f1cb82c587e0daae9abea0 to your computer and use it in GitHub Desktop.
Save jjn1056/2e44b79155f1cb82c587e0daae9abea0 to your computer and use it in GitHub Desktop.
unexpected stuff with SQLT
# The DBIC Resultclass
package Example::Schema::Result::Person;
use strict;
use warnings;
use base 'Example::Schema::Result';
__PACKAGE__->table("person");
__PACKAGE__->load_components(qw/EncodedColumn /);
__PACKAGE__->add_columns(
id => { data_type => 'bigint', is_nullable => 0, is_auto_increment => 1 },
username => { data_type => 'varchar', is_nullable => 0, size => 48 },
password => {
data_type => 'varchar',
is_nullable => 0,
size => 64,
encode_column => 1,
encode_class => 'Digest',
encode_args => { algorithm => 'MD5', format => 'base64' },
},
);
__PACKAGE__->set_primary_key("id");
1;
# Here's what is in the DB
jnapiorkowski=# \d+ person;
Table "public.person"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
----------+-----------------------+-----------+----------+------------------------------------+----------+--------------+-------------
id | bigint | | not null | nextval('person_id_seq'::regclass) | plain | |
username | character varying(48) | | not null | | extended | |
password | character varying(64) | | not null | | extended | |
Indexes:
"person_pkey" PRIMARY KEY, btree (id)
Access method: heap
# Diff and Dump method on Example::Schema
package Example::Schema;
use base 'DBIx::Class::Schema';
use strict;
use warnings;
use SQL::Translator;
use SQL::Translator::Diff;
our $VERSION = 1;
__PACKAGE__->load_components(qw/
Helper::Schema::QuoteNames
Helper::Schema::DidYouMean
Helper::Schema::DateTime/);
__PACKAGE__->load_namespaces(
default_resultset_class => "DefaultRS");
sub diff {
my $schema = shift;
my $dbic = SQL::Translator->new(
producer => 'PostgreSQL',
parser => 'SQL::Translator::Parser::DBIx::Class',
parser_args => { dbic_schema => $schema,},
);
my $database_current = SQL::Translator->new(
producer => 'PostgreSQL',
parser => 'DBI',
parser_args => { dbh => $schema->storage->dbh },
);
warn $dbic->translate;
warn $database_current->translate;
warn SQL::Translator::Diff->new({
output_db => 'PostgreSQL',
target_schema => $dbic->schema,
source_schema => $database_current->schema,
})->compute_differences->produce_diff_sql;
}
1;
# Ok So when I run this the result of $dbic->translate is
--
-- Created by SQL::Translator::Producer::PostgreSQL
-- Created on Sun Apr 12 07:39:12 2020
--
--
-- Table: person
--
CREATE TABLE "person" (
"id" bigserial NOT NULL,
"username" character varying(48) NOT NULL,
"password" character varying(64) NOT NULL,
PRIMARY KEY ("id")
);
# Not sure why its 'bigserial' although I guess its an alias
# And the result of $database_current->translate actually looks
# correct:
--
-- Created by SQL::Translator::Producer::PostgreSQL
-- Created on Sun Apr 12 07:39:12 2020
--
--
-- Table: person
--
CREATE TABLE "person" (
"id" bigint DEFAULT 'nextval(''person_id_seq''::regclass)' NOT NULL,
"username" character varying(48)(52) NOT NULL,
"password" character varying(64)(68) NOT NULL,
CONSTRAINT "person_pkey" UNIQUE ("id")
);
# But the result of the diff is totally off:
-- Convert schema '' to 'Example::Schema v1':;
BEGIN;
DROP INDEX person_pkey;
ALTER TABLE person ALTER COLUMN id TYPE bigserial;
ALTER TABLE person ALTER COLUMN id DROP DEFAULT;
ALTER TABLE person ALTER COLUMN username TYPE character varying(48);
ALTER TABLE person ALTER COLUMN password TYPE character varying(64);
ALTER TABLE person ADD PRIMARY KEY (id);
COMMIT;
# Not sure where all the ALTERs come from. I'm running this with a locally
# patched SQLT forPg12 compat (PR for that small change is here:
# https://github.com/dbsrgits/sql-translator/pull/127
# Is possible my diff method is wrong?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment