Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MySQL Workbench als Quelle für Perl-Code

Renee
March 07, 2019

MySQL Workbench als Quelle für Perl-Code

Ich nutze seit vielen Jahren die MySQL Workbench um z.B. mit Kunden das Datenbankdesign zu besprechen. Das nutze ich dann als Quelle um z.B. DBIx::Class-Klassen zu generieren und/oder eine Types::Tiny-Bibliothek zu erstellen.

Renee

March 07, 2019
Tweet

More Decks by Renee

Other Decks in Technology

Transcript

  1. • Visual Design Tool • (mainly) for MySQL • Exports

    for SVG, PDF, PNG • Can connect to (MySQL) databases • Reverse/Forward engineering • Scripting with Python • …
  2. *.mwb $ unzip project.mwb Archive: project.mwb MySQL Workbench Model archive

    1.0 model-schemas: mydb extracting: lock inflating: document.mwb.xml inflating: @db/data.db
  3. *.mwb $ unzip project.mwb Archive: project.mwb MySQL Workbench Model archive

    1.0 model-schemas: mydb extracting: lock inflating: document.mwb.xml inflating: @db/data.db
  4. $ unzip project.mwb Archive: project.mwb MySQL Workbench Model archive 1.0

    model-schemas: mydb extracting: lock inflating: document.mwb.xml inflating: @db/data.db MySQL::Workbench::Parser
  5. use File::Basename; use File::Spec; use MySQL::Workbench::DBIC; my $base_dir = dirname(

    __FILE__ ); my $mwb_file = File::Spec->catfile( $base_dir, 'projects.mwb' ); my $out_dir = File::Spec->catdir( $base_dir, 'lib' ); my $foo = MySQL::Workbench::DBIC->new( file => $mwb_file, output_path => $out_dir, namespace => 'GPW::DB', version_add => 1, schema_name => 'Schema', uppercase => 1, inherit_from_core => 1, ); $foo->create_schema; print sprintf "Version %s of DB created\n", $foo->version;
  6. use File::Basename; use File::Spec; use MySQL::Workbench::DBIC; my $base_dir = dirname(

    __FILE__ ); my $mwb_file = File::Spec->catfile( $base_dir, 'projects.mwb' ); my $out_dir = File::Spec->catdir( $base_dir, 'lib' ); my $foo = MySQL::Workbench::DBIC->new( file => $mwb_file, output_path => $out_dir, namespace => 'GPW::DB', version_add => 1, schema_name => 'Schema', uppercase => 1, inherit_from_core => 1, ); $foo->create_schema; print sprintf "Version %s of DB created\n", $foo->version;
  7. $ perl create_schema.pl Version 1 of DB created $ tree

    lib/ lib/ └── GPW └── DB ├── Schema │ └── Result │ ├── Person.pm │ ├── ProjectHasPerson.pm │ └── Project.pm └── Schema.pm 4 directories, 4 files
  8. package GPW::DB::Schema::Result::Project; # ABSTRACT: Result class for project use base

    qw(DBIx::Class::Core); our $VERSION = 1; __PACKAGE__->table( 'project' ); __PACKAGE__->add_columns( qw/ project_id project_title / ); __PACKAGE__->set_primary_key( qw/ project_id / ); __PACKAGE__->has_many(project_has_person => 'GPW::DB::Schema::Result::ProjectHasPerson', { 'foreign.project_project_id' => 'self.project_id' }); # --- # Put your own code below this comment # --- # --- 1;
  9. package GPW::DB::Schema::Result::Project; # ABSTRACT: Result class for project use base

    qw(DBIx::Class::Core); our $VERSION = 1; __PACKAGE__->table( 'project' ); __PACKAGE__->add_columns( qw/ project_id project_title / ); __PACKAGE__->set_primary_key( qw/ project_id / ); __PACKAGE__->has_many(project_has_person => 'GPW::DB::Schema::Result::ProjectHasPerson', { 'foreign.project_project_id' => 'self.project_id' }); # --- # Put your own code below this comment # --- # --- 1;
  10. package GPW::DB::Schema::Result::Project; # ABSTRACT: Result class for project use base

    qw(DBIx::Class::Core); our $VERSION = 1; __PACKAGE__->table( 'project' ); __PACKAGE__->add_columns( qw/ project_id project_title / ); __PACKAGE__->set_primary_key( qw/ project_id / ); __PACKAGE__->has_many(project_has_person => 'GPW::DB::Schema::Result::ProjectHasPerson', { 'foreign.project_project_id' => 'self.project_id' }); # --- # Put your own code below this comment # --- # --- 1;
  11. use File::Basename; use File::Spec; use MySQL::Workbench::DBIC; my $base_dir = dirname(

    __FILE__ ); my $mwb_file = File::Spec->catfile( $base_dir, 'projects.mwb' ); my $out_dir = File::Spec->catdir( $base_dir, 'lib' ); my $foo = MySQL::Workbench::DBIC->new( file => $mwb_file, output_path => $out_dir, namespace => 'GPW::DB', version_add => 1, schema_name => 'Schema', uppercase => 1, inherit_from_core => 1, ); $foo->create_schema; print sprintf "Version %s of DB created\n", $foo->version;
  12. use File::Basename; use File::Spec; use MySQL::Workbench::DBIC; my $base_dir = dirname(

    __FILE__ ); my $mwb_file = File::Spec->catfile( $base_dir, 'projects.mwb' ); my $out_dir = File::Spec->catdir( $base_dir, 'lib' ); my $foo = MySQL::Workbench::DBIC->new( file => $mwb_file, output_path => $out_dir, namespace => 'GPW::DB', version_add => 1, schema_name => 'Schema', column_details => 1, # default 1 uppercase => 1, inherit_from_core => 1, ); $foo->create_schema; print sprintf "Version %s of DB created\n", $foo->version;
  13. $ perl create_schema.pl Version 2 of DB created $ tree

    lib/ lib/ └── GPW └── DB ├── Schema │ └── Result │ ├── Person.pm │ ├── ProjectHasPerson.pm │ └── Project.pm └── Schema.pm 4 directories, 4 files
  14. __PACKAGE__->table( 'project' ); __PACKAGE__->add_columns( project_id => { data_type => 'INT',

    is_numeric => 1, retrieve_on_insert => 1, }, project_title => { data_type => 'VARCHAR', is_nullable => 1, size => 45, }, ); __PACKAGE__->set_primary_key( qw/ project_id / ); __PACKAGE__->has_many(project_has_person => 'GPW::DB::Schema::Result::ProjectHasPerson', { 'foreign.project_project_id' => 'self.project_id' });
  15. __PACKAGE__->load_components( qw/InflateColumn::TimePiece/ ); __PACKAGE__->table( 'project' ); __PACKAGE__->add_columns( project_id => {

    data_type => 'INT', is_numeric => 1, retrieve_on_insert => 1, }, project_title => { data_type => 'VARCHAR', is_nullable => 1, size => 45, }, created => { data_type => 'INT', is_nullable => 1, is_numeric => 1, 'inflate_time_piece' => 1 }, );
  16. package Library; use strict; use warnings; use Type::Library -base, -declare

    => qw( Varchar45 CustomType ); use Type::Utils -all; use Types::Standard -types; extends 'Types::SQL'; our $VERSION = $version; declare Varchar45, as Varchar[45]; # From Types::SQL declare CustomType, as Str, where { length( $_ ) < 9999 } 1;
  17. package GPW::Model::Project; use Moo; use Library qw(:all); has project_id =>

    ( is => 'ro', isa => Int ); has project_title => ( is => 'ro', isa => Varchar45 ); 1;
  18. • Merge different versions / branches – commit only .xml

    file (for diffs/merges)? • Improve support for custom datatypes • ...