Skip to content

Instantly share code, notes, and snippets.

@albfan
Forked from mix3/cpanfile
Last active March 3, 2016 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albfan/4fcf4240860fa73811b4 to your computer and use it in GitHub Desktop.
Save albfan/4fcf4240860fa73811b4 to your computer and use it in GitHub Desktop.
requires "SQL::Translator::Producer::PlantUML";
requires "DBD::Pg";
CREATE TABLE user (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(191) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;
CREATE TABLE item (
id INTEGER UNSIGNED NOT NULL,
name VARCHAR(191) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;
CREATE TABLE user_item (
user_id INTEGER UNSIGNED NOT NULL,
item_id INTEGER UNSIGNED NOT NULL,
amount INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (user_id, item_id),
FOREIGN KEY (user_id) REFERENCES user (id),
FOREIGN KEY (item_id) REFERENCES item (id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;
CREATE TABLE user_item_history (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INTEGER UNSIGNED NOT NULL,
item_id INTEGER UNSIGNED NOT NULL,
how_get INTEGER UNSIGNED NOT NULL,
how_out INTEGER UNSIGNED NOT NULL,
amount INTEGER UNSIGNED NOT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id, item_id) REFERENCES user_item (user_id, item_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use DBI;
use SQL::Translator;
my $dbh = DBI->connect("dbi:Pg:dbname=test;host=localhost;port=5432", "postgres", "postgres", {
AutoCommit => 1,
PrintError => 0,
RaiseError => 1,
ShowErrorStatement => 1,
AutoInactiveDestroy => 1,
mysql_enable_utf8 => 1,
mysql_auto_reconnect => 0,
});
my $got = SQL::Translator->new(
parser => 'DBI',
parser_args => { dbh => $dbh },
to => 'PlantUML',
)->translate;
$got =~ s/^\s*\n//mg;
print $got;
exit;
@albfan
Copy link
Author

albfan commented Mar 3, 2016

To use from scratch (ubuntu OS)

install cpan

$ sudo apt-get install cpan

config cpan

$ cpan

install plantUML

$ cpan cpan SQL::Translator::Producer::PlantUML

install carton

$ cpan Carton

install postgres DBI driver (9.1.1)

$ sudo apt-get install postgres postgresql-server-dev-all libpq-dev
$ cpan DBD::Pg

    - config (change to match your version)
        - major version: 9
        - minor version: 1
        - path version: 1
        - bin dir: /usr/lib/postgresql/9.1/bin
        - include dir: /usr/include/postgresql/

clone this

$ git clone <this>

install dependencies

$ carton install

execute

$ carton exec perl example.pl > schema

See result

copy contents of schema to http://www.planttext.com/planttext

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment