--- /dev/null
+class CollectionNameOwnerUniqueOnlyNonExpired < ActiveRecord::Migration
+ def find_index
+ indexes = ActiveRecord::Base.connection.indexes('collections')
+ name_owner_index = indexes.select do |index|
+ index.name == 'collection_owner_uuid_name_unique'
+ end
+ name_owner_index
+ end
+
+ def up
+ remove_index :collections, :name => 'collection_owner_uuid_name_unique' if !find_index.empty?
+ add_index(:collections, [:owner_uuid, :name], unique: true,
+ where: 'expires_at is null',
+ name: 'collection_owner_uuid_name_unique')
+ end
+
+ def down
+ # it failed during up. is it going to pass now? should we do nothing?
+ remove_index :collections, :name => 'collection_owner_uuid_name_unique' if !find_index.empty?
+ # and this is failing ...
+ # add_index(:collections, [:owner_uuid, :name], unique: true,
+ # name: 'collection_owner_uuid_name_unique')
+ end
+end
modified_at timestamp without time zone,
portable_data_hash character varying(255),
redundancy integer,
- redundancy_confirmed_by_client_uuid character varying(255),
redundancy_confirmed_at timestamp without time zone,
redundancy_confirmed_as integer,
updated_at timestamp without time zone NOT NULL,
description character varying(524288),
properties text,
expires_at date,
+ redundancy_confirmed_by_client_uuid character varying(255),
file_names character varying(8192)
);
-- Name: collection_owner_uuid_name_unique; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
-CREATE UNIQUE INDEX collection_owner_uuid_name_unique ON collections USING btree (owner_uuid, name);
+CREATE UNIQUE INDEX collection_owner_uuid_name_unique ON collections USING btree (owner_uuid, name) WHERE (expires_at IS NULL);
--
-- Name: collections_full_text_search_idx; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
-CREATE INDEX collections_full_text_search_idx ON collections USING gin (to_tsvector('english'::regconfig, (((((((((((((((((((COALESCE(owner_uuid, ''::character varying))::text || ' '::text) || (COALESCE(modified_by_client_uuid, ''::character varying))::text) || ' '::text) || (COALESCE(modified_by_user_uuid, ''::character varying))::text) || ' '::text) || (COALESCE(portable_data_hash, ''::character varying))::text) || ' '::text) || (COALESCE(redundancy_confirmed_by_client_uuid, ''::character varying))::text) || ' '::text) || (COALESCE(uuid, ''::character varying))::text) || ' '::text) || (COALESCE(name, ''::character varying))::text) || ' '::text) || (COALESCE(description, ''::character varying))::text) || ' '::text) || COALESCE(properties, ''::text)) || ' '::text) || (COALESCE(file_names, ''::character varying))::text)));
+CREATE INDEX collections_full_text_search_idx ON collections USING gin (to_tsvector('english'::regconfig, (((((((((((((((((COALESCE(owner_uuid, ''::character varying))::text || ' '::text) || (COALESCE(modified_by_client_uuid, ''::character varying))::text) || ' '::text) || (COALESCE(modified_by_user_uuid, ''::character varying))::text) || ' '::text) || (COALESCE(portable_data_hash, ''::character varying))::text) || ' '::text) || (COALESCE(uuid, ''::character varying))::text) || ' '::text) || (COALESCE(name, ''::character varying))::text) || ' '::text) || (COALESCE(description, ''::character varying))::text) || ' '::text) || COALESCE(properties, ''::text)) || ' '::text) || (COALESCE(redundancy_confirmed_by_client_uuid, ''::character varying))::text)));
--
INSERT INTO schema_migrations (version) VALUES ('20150203180223');
-INSERT INTO schema_migrations (version) VALUES ('20150206210804');
\ No newline at end of file
+INSERT INTO schema_migrations (version) VALUES ('20150206210804');
+
+INSERT INTO schema_migrations (version) VALUES ('20150216193428');
\ No newline at end of file
assert_equal 'value_1', c.properties['property_1']
end
end
+
+ test 'create, delete, recreate collection with same name and owner' do
+ act_as_user users(:active) do
+ # create collection with name
+ c = Collection.create(manifest_text: '',
+ name: "test collection name")
+ assert c.valid?
+ uuid = c.uuid
+
+ # mark collection as expired
+ c.update_attribute 'expires_at', Time.new.strftime("%Y-%m-%d")
+ c = Collection.where(uuid: uuid)
+ assert_empty c, 'Should not be able to find expired collection'
+
+ # recreate collection with the same name
+ c = Collection.create(manifest_text: '',
+ name: "test collection name")
+ assert c.valid?
+ end
+ end
end