Skip to content

Commit c469f96

Browse files
committed
* Added deliver_at to db schema and got it working with the auto-update stuff in Storage::Default.
* Fixed Storage::Default adding indices to the message table during table creation. * Fixed bad sqlite index syntax (doesn't allow "(8)" after column name).
1 parent 9f08fcd commit c469f96

File tree

5 files changed

+111
-14
lines changed

5 files changed

+111
-14
lines changed

lib/POE/Component/MessageQueue/Storage/Default.pm

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ CREATE TABLE meta
2020
);
2121
EOF
2222

23-
use constant MESSAGES_SCHEMA => <<'EOF';
23+
use constant MESSAGES_SCHEMA_018 => <<'EOF';
2424
CREATE TABLE messages
2525
(
2626
message_id varchar(255) primary key,
@@ -32,20 +32,49 @@ CREATE TABLE messages
3232
size int
3333
);
3434
35-
CREATE INDEX id_index ON messages ( message_id(8) );
35+
EOF
36+
37+
use constant MESSAGES_SCHEMA => <<'EOF';
38+
CREATE TABLE messages
39+
(
40+
message_id varchar(255) primary key,
41+
destination varchar(255) not null,
42+
persistent char(1) default 'Y' not null,
43+
in_use_by int,
44+
body text,
45+
timestamp int,
46+
size int,
47+
deliver_at int
48+
);
49+
50+
CREATE INDEX id_index ON messages ( message_id );
3651
CREATE INDEX timestamp_index ON messages ( timestamp );
3752
CREATE INDEX destination_index ON messages ( destination );
3853
CREATE INDEX in_use_by_index ON messages ( in_use_by );
54+
CREATE INDEX deliver_at ON messages ( deliver_at );
3955
4056
EOF
4157

58+
sub _do_schema
59+
{
60+
my ($dbh, $schema) = @_;
61+
foreach my $stmt ( split(";", $schema) )
62+
{
63+
# strip leading/trailing whitespace
64+
$stmt =~ s/^\s*//;
65+
$stmt =~ s/\s*$//;
66+
67+
$dbh->do($stmt) if ($stmt);
68+
}
69+
}
70+
4271
# Hopefully, this will make adding new changes that break db compatability a
4372
# little easier. Change the database schema above, then add a check for your
4473
# version like the examples below.
4574
sub _upgrade
4675
{
4776
my $dbh = shift;
48-
my @versions = ('0.1.7', '0.1.8');
77+
my @versions = ('0.1.7', '0.1.8', '0.2.3');
4978

5079
# Funny lexical scoping rules require this to be an anonymous sub or weird
5180
# things will happen with $dbh
@@ -70,6 +99,7 @@ sub _upgrade
7099
return (!$@);
71100
},
72101
'0.1.8' => sub { $meta_version->('0.1.8') },
102+
'0.2.3' => sub { $meta_version->('0.2.3') },
73103
);
74104

75105
my %repairs = (
@@ -79,15 +109,15 @@ sub _upgrade
79109
},
80110
'0.1.8' => sub {
81111
# 0.1.8 adds a meta table for version info
82-
$dbh->do( META_SCHEMA );
112+
_do_schema($dbh, META_SCHEMA);
83113
$dbh->do(q{INSERT INTO meta (key, value) VALUES ('version', '0.1.8')});
84114

85115
# SQLite doesn't have a syntax for modifying column types on primary
86116
# keys, and 1.8->1.9 made message_id a text field.
87117

88118
# Rename old table and create new one
89119
$dbh->do('ALTER TABLE messages RENAME TO old_messages');
90-
$dbh->do( MESSAGES_SCHEMA );
120+
_do_schema($dbh, MESSAGES_SCHEMA_018);
91121

92122
# Dump old table into new table
93123
my $columns = q{
@@ -103,6 +133,31 @@ sub _upgrade
103133
# Delete old table
104134
$dbh->do('DROP TABLE old_messages');
105135
},
136+
'0.2.3' => sub {
137+
# we add the deliver_at column
138+
$dbh->do("ALTER TABLE messages ADD COLUMN deliver_at INT");
139+
$dbh->do("CREATE INDEX deliver_at_index ON messages ( deliver_at )");
140+
141+
# updated the version
142+
$dbh->do("UPDATE meta SET value = '0.2.3' where key = 'version'");
143+
144+
# databases created with 0.1.8 or later, didn't correctly add the indexes
145+
# to the table (because it feeds MESSAGE_SCHEMA as a single statement to
146+
# $db->do() rather than breaking it up);
147+
my $indices = {
148+
id_index => "message_id",
149+
timestamp_index => "timestamp",
150+
destination_index => "destination",
151+
in_use_by_index => "in_use_by"
152+
};
153+
while (my ($name, $column) = each %$indices)
154+
{
155+
eval
156+
{
157+
$dbh->do("CREATE INDEX $name ON messages ( $column )");
158+
};
159+
}
160+
}
106161
);
107162

108163
my $do_repairs = 0;
@@ -126,7 +181,7 @@ sub _upgrade
126181
if ($@)
127182
{
128183
$dbh->rollback();
129-
die "encountered errors: $!: rolling back.\n";
184+
die "encountered errors: $@: rolling back.\n";
130185
}
131186
}
132187
}
@@ -154,9 +209,9 @@ sub _make_db
154209
}
155210
else
156211
{
157-
$dbh->do( MESSAGES_SCHEMA );
158-
$dbh->do( META_SCHEMA );
159-
$dbh->do(q{INSERT INTO meta (key, value) VALUES ('version', '0.1.8')});
212+
_do_schema($dbh, MESSAGES_SCHEMA);
213+
_do_schema($dbh, META_SCHEMA);
214+
$dbh->do(q{INSERT INTO meta (key, value) VALUES ('version', '0.2.3')});
160215
}
161216
$dbh->disconnect();
162217
}

schema/mysql.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CREATE TABLE meta
44
key varchar(255) primary key,
55
value varchar(255)
66
);
7-
INSERT INTO meta (key, value) VALUES ('version', '0.1.8');
7+
INSERT INTO meta (key, value) VALUES ('version', '0.2.3');
88

99
CREATE TABLE messages
1010
(
@@ -14,12 +14,14 @@ CREATE TABLE messages
1414
in_use_by int,
1515
body text,
1616
timestamp int,
17-
size int
17+
size int,
18+
deliver_at int
1819
);
1920

2021
-- Improves performance some bit:
2122
CREATE INDEX id_index ON messages ( message_id(8) );
2223
CREATE INDEX timestamp_index ON messages ( timestamp );
2324
CREATE INDEX destination_index ON messages ( destination );
2425
CREATE INDEX in_use_by_index ON messages ( in_use_by );
26+
CREATE INDEX deliver_at ON messages ( deliver_at );
2527

schema/sqlite.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CREATE TABLE meta
44
key varchar(255) primary key,
55
value varchar(255)
66
);
7-
INSERT INTO meta (key, value) VALUES ('version', '0.1.8');
7+
INSERT INTO meta (key, value) VALUES ('version', '0.2.3');
88

99
CREATE TABLE messages
1010
(
@@ -14,12 +14,14 @@ CREATE TABLE messages
1414
in_use_by int,
1515
body text,
1616
timestamp int,
17-
size int
17+
size int,
18+
deliver_at int
1819
);
1920

2021
-- Improves performance some bit:
21-
CREATE INDEX id_index ON messages ( message_id(8) );
22+
CREATE INDEX id_index ON messages ( message_id );
2223
CREATE INDEX timestamp_index ON messages ( timestamp );
2324
CREATE INDEX destination_index ON messages ( destination );
2425
CREATE INDEX in_use_by_index ON messages ( in_use_by );
26+
CREATE INDEX deliver_at ON messages ( deliver_at );
2527

schema/upgrade-0.1.8-sqlite.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
CREATE TABLE meta
2+
(
3+
key varchar(255) primary key,
4+
value varchar(255)
5+
);
6+
INSERT INTO meta (key, value) VALUES ('version', '0.1.8');
7+
8+
ALTER TABLE messages RENAME TO old_messages;
9+
10+
CREATE TABLE messages
11+
(
12+
message_id varchar(255) primary key,
13+
destination varchar(255) not null,
14+
persistent char(1) default 'Y' not null,
15+
in_use_by int,
16+
body text,
17+
timestamp int,
18+
size int
19+
);
20+
21+
CREATE INDEX id_index ON messages ( message_id );
22+
CREATE INDEX timestamp_index ON messages ( timestamp );
23+
CREATE INDEX destination_index ON messages ( destination );
24+
CREATE INDEX in_use_by_index ON messages ( in_use_by );
25+
26+
INSERT INTO messages
27+
(message_id, destination, persistent, in_use_by,
28+
body, timestamp, size)
29+
SELECT message_id, destination, persistent, in_use_by,
30+
body, timestamp, size
31+
FROM old_messages;
32+

schema/upgrade-0.2.3.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
ALTER TABLE messages ADD COLUMN deliver_at INT;
3+
CREATE INDEX deliver_at_index ON messages ( deliver_at );
4+
5+
UPDATE meta SET value = '0.2.3' where key = 'version';
6+

0 commit comments

Comments
 (0)