var Sequelize = require('sequelize');

module.exports = function(sequelize, DataTypes) {
	var Rom = sequelize.define('Rom', {
			timestamp: {
				type: Sequelize.DATE,
				allowNull: false,
			},
			md5sum: {
				type: Sequelize.STRING(32),
				allowNull: false,
				validate: {
					notEmpty: true,
				}
			},
			filename: {
				type: Sequelize.STRING(255),
				allowNull: false,
				validate: {
					notEmpty: true,
				},
			},
			updateChannel: {
				type: Sequelize.ENUM,
				values: [ 'stable', 'snapshot', 'RC', 'nightly', 'experimental' ],
				allowNull: false,
			},
			changelog: {
				type: Sequelize.TEXT,
			},
			apiLevel: {
				type: Sequelize.INTEGER.UNSIGNED,
				allowNull: false,
				validate: {
					min: 1,
				},
			},
			isActive: {
				type: Sequelize.BOOLEAN,
				allowNull: false,
			},
			incrementalId: {
				type: Sequelize.STRING(32),
				allowNull: true,
			},
			targetFilesZipName: {
				type: DataTypes.STRING(32),
				allowNull: true,
			},
			sourceCodeTimestamp: {
				type: Sequelize.DATE,
				allowNull: true,
			},
			fileSize: {
				type: Sequelize.BIGINT.UNSIGNED,
				allowNull: true,
			},
	}, {
		validate: {
			uniqueEnabledFilenamePerRomVariant : function() {
				var self = this;

				// Find all existing active roms for this filename with the same RomVariant.
				return Rom.count({
					where: {
						isActive: true,
						filename: self.filename,
					},
					include: [
						{
							model: sequelize.models.RomVariant,
							where: {
								id: self.RomVariantId,
							}
						}
					]
				}).then(function(totalExisting) {
					if (totalExisting > 0) {
						throw new Error('There are already ' + totalExisting + ' existing ROM matching ' + JSON.stringify(self.get({ plain: true })));
					}
				});
			},

			incrementalIdAndTargetFilesZipValid : function() {
				// Make sure that if incrementalId and targetfileszip are given that the latter one contains the first one.
				if (this.incrementalId && this.targetFilesZipName && this.targetFilesZipName.indexOf(this.incrementalId) == -1) {
					throw new Error(this.targetFilesZipName + " should contain the incrementalId: " + this.incrementalId);
				}
			}
		}
	});

	return Rom;
}

@@ -1337,24 +1337,66 @@
 %09%09%09where: %7B%0A
+%09%09%09%09%09%09id: %7B%0A%09%09%09%09%09%09%09$ne: self.id,%0A%09%09%09%09%09%09%7D,%0A
 %09%09%09%09%09%09isActi

Fix the "duplicate filename per RomVariant" check