Merge branch 'master' into 5197-collection-name-owner-unique
authorRadhika Chippada <radhika@curoverse.com>
Mon, 16 Feb 2015 20:49:24 +0000 (15:49 -0500)
committerRadhika Chippada <radhika@curoverse.com>
Mon, 16 Feb 2015 20:49:24 +0000 (15:49 -0500)
Conflicts:
services/api/db/structure.sql

services/api/db/migrate/20150216193428_collection_name_owner_unique_only_non_expired.rb [new file with mode: 0644]
services/api/db/structure.sql
services/api/test/unit/collection_test.rb

diff --git a/services/api/db/migrate/20150216193428_collection_name_owner_unique_only_non_expired.rb b/services/api/db/migrate/20150216193428_collection_name_owner_unique_only_non_expired.rb
new file mode 100644 (file)
index 0000000..d2c629a
--- /dev/null
@@ -0,0 +1,23 @@
+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?
+    add_index(:collections, [:owner_uuid, :name], unique: true,
+              name: 'collection_owner_uuid_name_unique')
+  end
+end
index 756dfc90c1e22b26ff16240694e55bf6057d4da8..1332995a867bdd146ebf5b7a5c435de360f76752 100644 (file)
@@ -169,6 +169,7 @@ CREATE TABLE collections (
     description character varying(524288),
     properties text,
     expires_at date,
+    redundancy_confirmed_by_client_uuid character varying(255),
     file_names character varying(8192)
 );
 
@@ -1303,7 +1304,7 @@ CREATE INDEX authorized_keys_search_index ON authorized_keys USING btree (uuid,
 -- 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);
 
 
 --
@@ -2358,4 +2359,6 @@ INSERT INTO schema_migrations (version) VALUES ('20150203180223');
 
 INSERT INTO schema_migrations (version) VALUES ('20150206210804');
 
-INSERT INTO schema_migrations (version) VALUES ('20150206230342');
\ No newline at end of file
+INSERT INTO schema_migrations (version) VALUES ('20150206230342');
+
+INSERT INTO schema_migrations (version) VALUES ('20150216193428');
index 37ab1d3172b2a72c488e5512776cec88dba3482f..d8b8365efa212f3447aceddec6decd2154520584 100644 (file)
@@ -233,4 +233,24 @@ class CollectionTest < ActiveSupport::TestCase
       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