3.0.0~1.sql 19.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
-- Add function who are not in mysql 5 to be able ton convert ipv6 to decimal (and reverse it)
DELIMITER //
CREATE FUNCTION INET_ATON6(n CHAR(39))
RETURNS DECIMAL(39) UNSIGNED
DETERMINISTIC
BEGIN
    RETURN CAST(CONV(SUBSTRING(n FROM  1 FOR 4), 16, 10) AS DECIMAL(39))
                       * 5192296858534827628530496329220096 -- 65536 ^ 7
         + CAST(CONV(SUBSTRING(n FROM  6 FOR 4), 16, 10) AS DECIMAL(39))
                       *      79228162514264337593543950336 -- 65536 ^ 6
         + CAST(CONV(SUBSTRING(n FROM 11 FOR 4), 16, 10) AS DECIMAL(39))
                       *          1208925819614629174706176 -- 65536 ^ 5
13
         + CAST(CONV(SUBSTRING(n FROM 16 FOR 4), 16, 10) AS DECIMAL(39))
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
                       *               18446744073709551616 -- 65536 ^ 4
         + CAST(CONV(SUBSTRING(n FROM 21 FOR 4), 16, 10) AS DECIMAL(39))
                       *                    281474976710656 -- 65536 ^ 3
         + CAST(CONV(SUBSTRING(n FROM 26 FOR 4), 16, 10) AS DECIMAL(39))
                       *                         4294967296 -- 65536 ^ 2
         + CAST(CONV(SUBSTRING(n FROM 31 FOR 4), 16, 10) AS DECIMAL(39))
                       *                              65536 -- 65536 ^ 1
         + CAST(CONV(SUBSTRING(n FROM 36 FOR 4), 16, 10) AS DECIMAL(39))
         ;
END;
//
DELIMITER ;
DELIMITER //
CREATE FUNCTION INET_NTOA6(n DECIMAL(39) UNSIGNED)
RETURNS CHAR(39)
DETERMINISTIC
BEGIN
  DECLARE a CHAR(39)             DEFAULT '';
  DECLARE i INT                  DEFAULT 7;
  DECLARE q DECIMAL(39) UNSIGNED DEFAULT 0;
  DECLARE r INT                  DEFAULT 0;
  WHILE i DO
    -- DIV doesn't work with nubers > bigint
    SET q := FLOOR(n / 65536);
    SET r := n MOD 65536;
    SET n := q;
    SET a := CONCAT_WS(':', LPAD(CONV(r, 10, 16), 4, '0'), a);

    SET i := i - 1;
  END WHILE;

  SET a := TRIM(TRAILING ':' FROM CONCAT_WS(':',
                                            LPAD(CONV(n, 10, 16), 4, '0'),
                                            a));

  RETURN a;

END;
//
DELIMITER ;

-- New table for the authorised IP
CREATE TABLE IF NOT EXISTS `authorised_ip` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `uid` int(11) unsigned NOT NULL default '0',
  `ip` varchar(40) not null,
  `subnet` integer(3) not null default 32,
  `infos` varchar(255) not null default '',
  PRIMARY KEY  (`id`),
  KEY `uid` (`uid`)
) ENGINE=MyISAM COMMENT='Table with list of authorised ip and subnet';

-- Who have authorised IP ?
CREATE TABLE IF NOT EXISTS `authorised_ip_affected` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `authorised_ip_id` int(10) unsigned not null,
  `protocol` varchar(15) not null,
  `parameters` varchar(30) default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM COMMENT='Table with list of protocol <-> authorised ip and subnet';

INSERT IGNORE INTO `variable` (`name` ,`value` ,`comment`)
VALUES (
'auth_ip_ftp_default_yes', '1',
'This variable set if you want to allow all IP address to access FTP by default. If the user start to define some IP or subnet in the allow list, only those he defined will be allowed. This variable can take two value : 0 or 1.'
);

Alan Garcia's avatar
Alan Garcia committed
81 82 83 84
-- Main address table.
--
-- Addresses for domain.

85
CREATE TABLE IF NOT EXISTS `address` (
Alan Garcia's avatar
Alan Garcia committed
86
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, -- Technical id.
87
  `domain_id` bigint(20) unsigned NOT NULL REFERENCES `domaines`(`id`), -- FK to sub_domains.
Alan Garcia's avatar
Alan Garcia committed
88
  `address` varchar(255) NOT NULL, -- The address.
89
  `type` char(8) NOT NULL, -- standard emails are '', other may be 'mailman' or 'sympa' ...
Alan Garcia's avatar
Alan Garcia committed
90 91 92 93
  `password` varchar(255) DEFAULT NULL, -- The password associated to the address.
  `enabled` int(1) unsigned NOT NULL DEFAULT '1', -- Enabled flag.
  `expire_date` datetime DEFAULT NULL, -- Expiration date, used for temporary addresses.
  `update_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -- Update date, for technical usage only.
Alan Garcia's avatar
Alan Garcia committed
94
  `mail_action` enum('OK','DELETE','DELETING') NOT NULL default 'OK', -- mail_action is DELETE or DELETING when deleting a mailbox by cron
Alan Garcia's avatar
Alan Garcia committed
95
  PRIMARY KEY (`id`),
96
  UNIQUE INDEX `fk_domain_id` (`domain_id`,`address`)
97
) ENGINE=MyISAM COMMENT = 'This is the main address table. It represents an address as in RFC2822';
Alan Garcia's avatar
Alan Garcia committed
98 99 100

--
-- Mailbox table.
101
--
Alan Garcia's avatar
Alan Garcia committed
102 103
-- Local delivered mailboxes.

104
CREATE TABLE IF NOT EXISTS `mailbox` (
Alan Garcia's avatar
Alan Garcia committed
105 106 107 108 109 110
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, -- Technical id.
  `address_id` bigint(20) unsigned NOT NULL REFERENCES `address`(`id`), -- Reference to address.
  `path` varchar(255) NOT NULL, -- Relative path to the mailbox.
  `quota` bigint(20) unsigned DEFAULT NULL, -- Quota for this mailbox.
  `delivery` varchar(255) NOT NULL, -- Delivery transport.
  `update_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -- Update date, for technical usage only.
111
  `bytes` bigint(20) NOT NULL DEFAULT '0', -- number of bytes in the mailbox, filled by dovecot
112
  `messages` int(11) NOT NULL DEFAULT '0', -- number of messages in the mailbox, filled by dovecot
113
  `lastlogin` datetime NOT NULL, -- Last login, filled by dovecot
Alan Garcia's avatar
Alan Garcia committed
114
  `mail_action` enum('OK','DELETE','DELETING') NOT NULL default 'OK', -- mail_action is DELETE or DELETING when deleting a mailbox by cron
Alan Garcia's avatar
Alan Garcia committed
115 116
  PRIMARY KEY (`id`),
  UNIQUE KEY `address_id` (`address_id`)
117
) ENGINE=MyISAM COMMENT = 'Table containing local deliverd mailboxes.';
Alan Garcia's avatar
Alan Garcia committed
118 119 120 121 122 123

--
-- Other recipients.
--
-- Other recipients for an address (aliases)

124
CREATE TABLE IF NOT EXISTS `recipient` (
Alan Garcia's avatar
Alan Garcia committed
125 126 127 128 129 130
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, -- Technical id.
  `address_id` bigint(20) unsigned NOT NULL REFERENCES `address`(`id`), -- Reference to address
  `recipients` text NOT NULL, -- Recipients
  `update_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -- Update date, for technical usage only.
  PRIMARY KEY (`id`),
  UNIQUE KEY `address_id` (`address_id`)
131
) ENGINE=MyISAM COMMENT = 'Table containing other recipients (aliases) for an address.';
Alan Garcia's avatar
Alan Garcia committed
132

133

134 135 136 137
-- Regenerate apache conf to enable mpm-itk
update sub_domaines set web_action = 'UPDATE';
update domaines     set dns_action = 'UPDATE';

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
--
-- Scheduled tasks
--
CREATE TABLE IF NOT EXISTS `cron` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL,
  `url` varchar(2100) NOT NULL,
  `user` varchar(64) NOT NULL,
  `password` varchar(64) NOT NULL,
  `schedule` int(11) NOT NULL,
  `email` varchar(255) NOT NULL,
  `next_execution` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `uid` (`uid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
153

154 155


156
--
157
-- Structure de la table `piwik_users`
158
--
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

CREATE TABLE IF NOT EXISTS `piwik_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL,
  `login` varchar(255) NOT NULL,
  `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_user` (`login`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

--
-- Structure de la table `piwik_sites`
--

CREATE TABLE IF NOT EXISTS `piwik_sites` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL,
  `piwik_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique_site_per_user` (`uid`,`piwik_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

181

182 183 184
-- No longer MySQL User quotas
DELETE FROM defquotas WHERE quota = 'mysql_users';
DELETE FROM quotas WHERE name = 'mysql_users';
Alan Garcia's avatar
Alan Garcia committed
185 186 187 188 189

-- Raw web statistics are deprecated since vlogger
DELETE FROM quotas WHERE name = 'sta2';
DELETE FROM defquotas WHERE quota = 'sta2';
DROP TABLE stats2;
190 191 192

-- With Dovecot, no more use of size_mail
DROP TABLE size_mail;
193 194 195 196

-- now that we have separate packages for the webmails, we can't serve webmail domainetype anymore
DELETE FROM domaines_type WHERE name='webmail';
UPDATE domaines_type SET compatibility=REPLACE(compatibility,'webmail,','');
197 198 199 200 201 202

-- Edit domains_type
ALTER TABLE `domaines_type` ADD create_tmpdir BOOLEAN NOT NULL DEFAULT FALSE ;
ALTER TABLE `domaines_type` ADD create_targetdir BOOLEAN NOT NULL DEFAULT FALSE ;
UPDATE domaines_type SET create_tmpdir=true, create_targetdir=true WHERE target='DIRECTORY';

203 204
-- Allow 255 caracters in the tld
ALTER IGNORE TABLE domaines MODIFY domaine VARCHAR(255);
205
ALTER IGNORE TABLE sub_domaines MODIFY domaine VARCHAR(255);
206

207 208 209 210 211 212 213 214
-- Defaults subdomains to create when a domain is added
CREATE TABLE IF NOT EXISTS `default_subdomains` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `sub` varchar(255) NOT NULL,
  `domain_type` varchar(255) NOT NULL,
  `domain_type_parameter` varchar(255) NOT NULL,
  `concerned` enum('BOTH','MAIN','SLAVE') NOT NULL DEFAULT 'MAIN',
  `enabled` boolean not null default true,
215 216
  PRIMARY KEY  (`id`),
  UNIQUE KEY `unique_row` (`sub`,`domain_type`,`domain_type_parameter`,`concerned`)
217
) ENGINE=MyISAM COMMENT='Contains the defaults subdomains created on domains creation';
218

219
INSERT IGNORE INTO `default_subdomains` (`sub`, `domain_type`, `domain_type_parameter`, `concerned`) VALUES
220 221 222 223 224 225
('www',  'VHOST',   '%%DOMAINDIR%%',             'MAIN'),
('mail', 'WEBMAIL', '',                          'MAIN'),
('',     'URL',     'http://www.%%DOMAIN%%',     'MAIN'),
('www',  'URL',     'http://www.%%TARGETDOM%%',  'SLAVE'),
('mail', 'URL',     'http://mail.%%TARGETDOM%%', 'SLAVE'),
('',     'URL',     'http://%%TARGETDOM%%',      'SLAVE');
226 227


228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
-- -------------------------------------------------
-- Update domaines table.
alter table domaines
    drop primary key;

alter table domaines
    add column id bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key;

alter table domaines
    add unique key(domaine);
-- End of update domaines table.
-- -------------------------------------------------

-- -------------------------------------------------
-- Add column to follow the migration process and detect unmigrated entries
alter table mail_domain
    add column migration_state enum ('UNMIGRATED', 'WONT BE MIGRATED', 'DB MIGRATED', 'FS MIGRATED') not null default 'UNMIGRATED';

alter table mail_users
    add column migration_state enum ('UNMIGRATED', 'WONT BE MIGRATED', 'DB MIGRATED', 'FS MIGRATED') not null default 'UNMIGRATED';

alter table mail_alias
    add column migration_state enum ('UNMIGRATED', 'WONT BE MIGRATED', 'DB MIGRATED', 'FS MIGRATED') not null default 'UNMIGRATED';


-- Tag mail_domain addresses without @ to not migrated.
update mail_domain
    set mail_domain.migration_state='WONT BE MIGRATED'
    where locate('@', mail_domain.mail) = 0;

-- Tag mail_domain addresses with no corresponding domain in domaines to not migrate
update mail_domain
    set mail_domain.migration_state='WONT BE MIGRATED'
    where locate('@', mail_domain.mail) > 0
        and substring_index(mail_domain.mail, '@', -1) not in (select domaines.domaine from domaines);

-- Tag mail_domain addresses with two @
update mail_domain
    set mail_domain.migration_state='WONT BE MIGRATED'
    where locate('@', mail_domain.mail) > 0
        and mail_domain.mail like '%@%@%';

-- Tag mail
update mail_users
    set mail_users.migration_state='WONT BE MIGRATED'
    where alias in (select replace(mail_domain.mail, '@', '_')
        from mail_domain
        where mail_domain.migration_state='WONT BE MIGRATED');

update mail_users
    set mail_users.migration_state='WONT BE MIGRATED'
    where alias in (select mail_domain.mail
        from mail_domain
        where mail_domain.migration_state='WONT BE MIGRATED');


-- update mail_domain, domaines
--     set migration_state='WONT BE MIGRATED'
--     where mail = domaines.domaine;

-- End of add column to follow the migration process and detect unmigrated entries
-- -------------------------------------------------

-- -------------------------------------------------
-- Insert data into new model from old model.

-- Insert local managed mailboxes
insert into address (domain_id, address, type, password)
    select domaines.id as domain_id,
    substring_index(mail_domain.mail, '@', 1) as address,
    '' as type,
    mail_users.password
    from domaines
        join mail_domain
            on domaines.domaine = substring_index(mail_domain.mail, '@', -1)
        join mail_users
            on locate(concat(trim(mail_users.alias), ' '), concat(trim(replace(mail_domain.alias, '\n', ' ')), ' ')) = 1
    where mail_domain.migration_state='UNMIGRATED'
        and mail_domain.mail like '%@%'
        and mail_domain.type = 0
        and mail_domain.pop = 1;

insert into mailbox (address_id, path, delivery, lastlogin)
    select address.id,
    mail_users.path,
    'dovecot' as delivery,
    0 as last_login
    from address
        join domaines
            on domaines.id = address.domain_id
        join mail_domain
            on mail_domain.mail = concat(address.address, '@', domaines.domaine)
        join mail_users
            on locate(concat(trim(mail_users.alias), ' '), concat(trim(replace(mail_domain.alias, '\n', ' ')), ' ')) = 1
    where mail_domain.migration_state='UNMIGRATED'
        and mail_domain.mail like '%@%'
        and mail_domain.type = 0
        and mail_domain.pop = 1;

insert into recipient (address_id, recipients)
    select address.id as address_id,
    recipients.recipients
    from address
        join domaines
            on domaines.id = address.domain_id
        join (select
                mail_domain.mail as mail,
                trim(both '\n' from trim(replace(mail_domain.alias, replace(mail_domain.mail, '@', '_'), ''))) as recipients
            from mail_domain
            where mail_domain.migration_state='UNMIGRATED'
                and mail_domain.mail like '%@%'
                and mail_domain.type = 0
                and mail_domain.pop = 1) as recipients
            on recipients.recipients != ''
                and recipients.mail = concat(address.address, '@', domaines.domaine);

update mail_domain, domaines, mail_users
    set mail_domain.migration_state = 'DB MIGRATED',
        mail_users.migration_state = 'DB MIGRATED'
    where mail_domain.migration_state='UNMIGRATED'
        and domaines.domaine = substring_index(mail_domain.mail, '@', -1)
        and locate(concat(trim(mail_users.alias), ' '), concat(trim(replace(mail_domain.alias, '\n', ' ')), ' ')) = 1
        and mail_domain.mail like '%@%'
        and mail_domain.type = 0
        and mail_domain.pop = 1;

update mail_domain, domaines, mail_users
    set mail_users.migration_state = 'DB MIGRATED'
    where mail_domain.migration_state='UNMIGRATED'
        and domaines.domaine = substring_index(mail_domain.mail, '@', -1)
        and mail_users.alias = mail_domain.mail
        and mail_domain.mail like '%@%'
        and mail_domain.type = 0
        and mail_domain.pop = 1;

-- Here we should have inserted all local managed mailboxes and their aliases.
364 365 366 367 368 369 370 371 372 373 374 375 376
-- Insert catchall alias only adresses.
insert into address (domain_id, address, type, password)
    select domaines.id as domain_id,
    substring_index(mail_domain.mail, '@', 1) as address,
    'catchall' as type,
    '' as password
    from domaines
        join mail_domain
            on domaines.domaine = substring_index(mail_domain.mail, '@', -1)
    where mail_domain.mail like '@%'
        and mail_domain.type = 0
        and mail_domain.pop = 0
        and substring_index(mail_domain.mail, '@', 1) ='';
377 378 379 380 381 382 383 384 385 386 387 388

-- Insert alias only adresses.
insert into address (domain_id, address, type, password)
    select domaines.id as domain_id,
    substring_index(mail_domain.mail, '@', 1) as address,
    '' as type,
    '' as password
    from domaines
        join mail_domain
            on domaines.domaine = substring_index(mail_domain.mail, '@', -1)
    where mail_domain.mail like '%@%'
        and mail_domain.type = 0
389 390
        and mail_domain.pop = 0
        and substring_index(mail_domain.mail, '@', 1) != '';
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

insert into recipient (address_id, recipients)
    select address.id as address_id,
    trim(both '\n' from recipients.recipients)
    from address
        join domaines
            on domaines.id = address.domain_id
        join (select
                mail_domain.mail as mail,
                mail_domain.alias as recipients
            from mail_domain
            where mail_domain.migration_state='UNMIGRATED'
                and mail_domain.mail like '%@%'
                and mail_domain.type = 0
                and mail_domain.pop = 0) as recipients
            on recipients.recipients != ''
                and recipients.mail = concat(address.address, '@', domaines.domaine);

update mail_domain, domaines
    set mail_domain.migration_state = 'DB MIGRATED'
    where mail_domain.migration_state='UNMIGRATED'
        and domaines.domaine = substring_index(mail_domain.mail, '@', -1)
        and mail_domain.mail like '%@%'
        and mail_domain.type = 0
        and mail_domain.pop = 0;

-- Insert procmail managed addresses.
-- Note: those addresses should be treated as local managed adresses.

-- Insert mailman list addresses
insert into address (domain_id, address, type, password)
    select domaines.id as domain_id,
    substring_index(mail_domain.mail, '@', 1) as address,
    'mailman' as type,
    '' as password
    from domaines
        join mail_domain
            on domaines.domaine = substring_index(mail_domain.mail, '@', -1)
        join mail_alias
            on mail_alias.mail = mail_domain.alias
    where mail_domain.migration_state='UNMIGRATED'
        and mail_domain.type = 1
        and mail_domain.pop = 0
        and mail_alias.alias like '"| /var/lib/mailman/mail/mailman %';

update mail_domain, domaines, mail_alias
    set mail_domain.migration_state = 'DB MIGRATED'
    where mail_domain.migration_state='UNMIGRATED'
        and domaines.domaine = substring_index(mail_domain.mail, '@', -1)
        and mail_alias.mail = mail_domain.alias
        and mail_domain.type = 1
        and mail_alias.alias like '"| /var/lib/mailman/mail/mailman %';

-- Insert sympa list addresses
insert into address (domain_id, address, type, password)
    select domaines.id as domain_id,
    substring_index(mail_domain.mail, '@', 1) as address,
    'sympa' as type,
    '' as password
    from domaines
        join mail_domain
            on domaines.domaine = substring_index(mail_domain.mail, '@', -1)
        join mail_alias
            on mail_alias.mail = mail_domain.alias
    where mail_domain.migration_state='UNMIGRATED'
        and mail_domain.type = 1
        and mail_domain.pop = 0
        and mail_alias.alias like '"|/usr/lib/sympa/bin/bouncequeue %';

update mail_domain, domaines, mail_alias
    set mail_domain.migration_state = 'DB MIGRATED'
    where mail_domain.migration_state='UNMIGRATED'
        and domaines.domaine = substring_index(mail_domain.mail, '@', -1)
        and mail_alias.mail = mail_domain.alias
        and mail_domain.type = 1
        and mail_alias.alias like '"|/usr/lib/sympa/bin/bouncequeue %';
-- End of insert data into new model from old model.
-- -------------------------------------------------

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
--
-- Structure de la vue `dovecot_view`
--
CREATE OR REPLACE VIEW `dovecot_view` AS
SELECT concat(`address`.`address`,'@',`domaines`.`domaine`) AS `user`,
concat('*:storage=',cast(`mailbox`.`quota` as char charset latin1),'M') AS `userdb_quota_rule`,
`address`.`password` AS `password`,
`mailbox`.`path` AS `userdb_home`,
`domaines`.`compte` AS `userdb_uid`,
`domaines`.`compte` AS `userdb_gid`,
`mailbox`.`bytes` AS `quota_dovecot`,
`mailbox`.`messages` AS `nb_messages`
from ((`mailbox`
join `address` on((`address`.`id` = `mailbox`.`address_id`)))
join `domaines` on((`domaines`.`id` = `address`.`domain_id`)))
where `address`.`enabled` = 1
;

--
-- Structure de la vue `alias_view`
--

CREATE OR REPLACE VIEW `alias_view` AS
select concat(`address`.`address`,'@',`domaines`.`domaine`) AS `mail`,
concat(if(isnull(`mailbox`.`id`),'',concat(concat(`address`.`address`,'@',`domaines`.`domaine`),'\n')),`recipient`.`recipients`) AS `alias`
from (((`recipient` join `address` on((`address`.`id` = `recipient`.`address_id`)))
left join `mailbox` on((`mailbox`.`address_id` = `address`.`id`)))
join `domaines` on((`domaines`.`id` = `address`.`domain_id`)))
where `address`.`enabled` = 1
union
select distinct concat(`m`.`login`,'@',`v`.`value`) AS `mail`,
`m`.`mail` AS `alias`
from ((`membres` `m` join `variable` `v`) join `domaines` `d`)
where (`v`.`name` = 'mailname_bounce');