Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Created November 16, 2012 20:31
Show Gist options
  • Save sycobuny/4090645 to your computer and use it in GitHub Desktop.
Save sycobuny/4090645 to your computer and use it in GitHub Desktop.
Example Migration in Perl
PLModel::Migration {
$migration->table users => sub {
$table->primary_key;
$table->text name => {null => FALSE};
$table->text login => {null => FALSE, unique => TRUE};
$table->text salt => {null => FALSE};
$table->text password => {null => FALSE};
};
$migration->table cards => sub {
my ($expr) = PLModel::SQL::Function('NOW');
$table->primary_key;
$table->integer user_id => {null => FALSE};
$table->datetime time_in => {null => FALSE, default => $expr};
$table->datetime time_out => {};
$table->foreign_key 'users';
};
};
BEGIN;
CREATE TABLE "users" ("id" SERIAL PRIMARY KEY, "name" TEXT NOT NULL, "login" TEXT UNIQUE NOT NULL, "salt" TEXT NOT NULL, "password" TEXT NOT NULL) WITHOUT OIDS;
CREATE TABLE "cards" ("id" SERIAL PRIMARY KEY, "user_id" INTEGER NOT NULL, "time_in" TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW(), "time_out" TIMESTAMP WITHOUT TIME ZONE, FOREIGN KEY ("user_id") REFERENCES "users" ("id")) WITHOUT OIDS;
CREATE OR REPLACE FUNCTION "schema_version" RETURNS INTEGER IMMUTABLE COST 1 ROWS 1 LANGUAGE SQL AS $FUNC1$SELECT 1$FUNC1$;
COMMIT;
# behave properly and enable strict and warnings
use warnings;
use strict;
# silence warnings on global vars we intend to use
local ($migration, $table);
PGModel::Migration {
$migration->table users => sub {
$table->primary_key;
$table->text name => {null => FALSE};
$table->text login => {null => FALSE, unique => TRUE};
$table->text salt => {null => FALSE};
$table->text password => {null => FALSE};
};
$migration->table cards => sub {
my ($expr) = PGModel::SQL::Function('NOW');
$table->primary_key;
$table->integer user_id => {null => FALSE};
$table->datetime time_in => {null => FALSE, default => $expr};
$table->datetime time_out => {};
$table->foreign_key 'users';
};
};
# behave properly and enable strict and warnings
use warnings;
use strict;
PGModel::Migration {
my ($migration) = shift; # don't use global vars at all
$migration->table users => sub {
my ($table) = shift; # don't use global vars at all
$table->primary_key;
$table->text name => {null => FALSE};
$table->text login => {null => FALSE, unique => TRUE};
$table->text salt => {null => FALSE};
$table->text password => {null => FALSE};
};
$migration->table cards => sub {
my ($expr) = PGModel::SQL::Function('NOW');
$table->primary_key;
$table->integer user_id => {null => FALSE};
$table->datetime time_in => {null => FALSE, default => $expr};
$table->datetime time_out => {};
$table->foreign_key 'users';
};
};
PLModel::Migration {
$migration->table(users => sub {
$table->primary_key;
$table->text(name => {null => FALSE});
$table->text(login => {null => FALSE, unique => TRUE});
$table->text(salt => {null => FALSE});
$table->text(password => {null => FALSE});
});
$migration->table(cards => sub {
my ($expr) = PLModel::SQL::Function('NOW');
$table->primary_key;
$table->integer (user_id => {null => FALSE});
$table->datetime(time_in => {null => FALSE, default => $expr});
$table->datetime(time_out => {});
$table->foreign_key('users');
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment