MojoMojo SQLite to PostgreSQL Sequences SQL Helper Script
This perl code can assist with the creation of sequences when migrating a MojoMojo database from SQLite to PostgreSQL.
#!/usr/bin/env perl use Modern::Perl; my %tables = ( attachment => 39, comment => 1, entry => 1, 'link' => 197, page => 333, person => 33, photo => 38, role => 1, tag => 24, wanted_page => 5, ); my $table_suffix = '_id_seq'; # Create the SQL statements for getting max id, # creating and applying the sequence. foreach my $table ( sort keys %tables ) { #say select_max_id($table); my $start = $tables{$table}; say create_sequence( $table, $start ); print "\n"; say make_serial_id($table); print "\n"; } sub select_max_id { my $table = shift; return <<"SQL"; SELECT max(id) FROM public.$table; SQL } sub create_sequence { my $table = shift; my $start = shift; my $table_id_seq = $table . $table_suffix; return <<"SQL"; CREATE SEQUENCE $table_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 START $start CACHE 1; ALTER TABLE $table_id_seq OWNER TO mojomojo; SQL } sub make_serial_id { my $table = shift; my $table_id_seq = $table . $table_suffix; return <<"SQL"; ALTER TABLE public.$table ALTER COLUMN id SET DEFAULT nextval('$table_id_seq'); SQL } __END__ =head1 %tables = Table and Start Values Data Hash Hash of table names and max id to be used for creating sequences to serialize the id column which doesn't happen for free when importing a SQLite database into PostgreSQL. NOTE: The values below are specific to my installation and the fact some tables already have records (where value > 1). Adjust the values to your situation. See select_max_id to the generate the SQL to get at these values. =head1 Subroutines =head2 select_max_id Given a table name, get the maximum id value for the table. =head2 create_sequence Take the table name and start number (nextval) as input and construct the SQL statement to create the sequence. =head2 make_serial_id Take the table name as input and construct the SQL statement to apply the sequence by setting the default value of id column to be the nextval in the sequence. =cut
Showing changes from previous revision. Removed | Added
