diff --git a/database.cc b/database.cc index 0a1758d038..9bdbd0c8fc 100644 --- a/database.cc +++ b/database.cc @@ -1590,7 +1590,7 @@ void database::add_column_family(schema_ptr schema, column_family::config cfg) { if (_ks_cf_to_uuid.count(kscf) != 0) { throw std::invalid_argument("Column family " + schema->cf_name() + " exists"); } - ks->second.add_column_family(schema); + ks->second.add_or_update_column_family(schema); cf->start(); _column_families.emplace(uuid, std::move(cf)); _ks_cf_to_uuid.emplace(std::move(kscf), uuid); diff --git a/database.hh b/database.hh index 26f35cad93..f9c56c7d21 100644 --- a/database.hh +++ b/database.hh @@ -761,8 +761,8 @@ public: const lw_shared_ptr& user_types() const { return _user_types; } - void add_column_family(const schema_ptr& s) { - _cf_meta_data.emplace(s->cf_name(), s); + void add_or_update_column_family(const schema_ptr& s) { + _cf_meta_data[s->cf_name()] = s; } void remove_column_family(const schema_ptr& s) { _cf_meta_data.erase(s->cf_name()); @@ -822,8 +822,8 @@ public: const locator::abstract_replication_strategy& get_replication_strategy() const; column_family::config make_column_family_config(const schema& s) const; future<> make_directory_for_column_family(const sstring& name, utils::UUID uuid); - void add_column_family(const schema_ptr& s) { - _metadata->add_column_family(s); + void add_or_update_column_family(const schema_ptr& s) { + _metadata->add_or_update_column_family(s); } void add_user_type(const user_type ut) { _metadata->add_user_type(ut); diff --git a/db/schema_tables.cc b/db/schema_tables.cc index 3c3f6e8311..4ba0faa0df 100644 --- a/db/schema_tables.cc +++ b/db/schema_tables.cc @@ -696,12 +696,14 @@ future> merge_keyspaces(distributed& p static future<> update_column_family(database& db, schema_ptr new_schema) { column_family& cfm = db.find_column_family(new_schema->id()); + keyspace& ks = db.find_keyspace(new_schema->ks_name()); bool columns_changed = !cfm.schema()->equal_columns(*new_schema); auto s = local_schema_registry().learn(new_schema); s->registry_entry()->mark_synced(); cfm.set_schema(std::move(s)); + ks.metadata()->add_or_update_column_family(new_schema); return service::get_local_migration_manager().notify_update_column_family(cfm.schema(), columns_changed); } diff --git a/tests/schema_change_test.cc b/tests/schema_change_test.cc index 77e020ca89..c2f2c7e2e8 100644 --- a/tests/schema_change_test.cc +++ b/tests/schema_change_test.cc @@ -64,6 +64,38 @@ SEASTAR_TEST_CASE(test_new_schema_with_no_structural_change_is_propagated) { }); } +SEASTAR_TEST_CASE(test_schema_is_updated_in_keyspace) { + return do_with_cql_env([](cql_test_env& e) { + return seastar::async([&] { + auto builder = schema_builder("tests", "table") + .with_column("pk", bytes_type, column_kind::partition_key) + .with_column("v1", bytes_type); + + e.execute_cql("create keyspace tests with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };").get(); + + auto old_schema = builder.build(); + + service::get_local_migration_manager().announce_new_column_family(old_schema, false).get(); + + auto s = e.local_db().find_schema(old_schema->id()); + BOOST_REQUIRE_EQUAL(*old_schema, *s); + BOOST_REQUIRE_EQUAL(864000, s->gc_grace_seconds().count()); + BOOST_REQUIRE_EQUAL(*s, *e.local_db().find_keyspace(s->ks_name()).metadata()->cf_meta_data().at(s->cf_name())); + + builder.set_gc_grace_seconds(1); + auto new_schema = builder.build(); + + service::get_local_migration_manager().announce_column_family_update(new_schema, false).get(); + + s = e.local_db().find_schema(old_schema->id()); + BOOST_REQUIRE_NE(*old_schema, *s); + BOOST_REQUIRE_EQUAL(*new_schema, *s); + BOOST_REQUIRE_EQUAL(1, s->gc_grace_seconds().count()); + BOOST_REQUIRE_EQUAL(*s, *e.local_db().find_keyspace(s->ks_name()).metadata()->cf_meta_data().at(s->cf_name())); + }); + }); +} + SEASTAR_TEST_CASE(test_tombstones_are_ignored_in_version_calculation) { return do_with_cql_env([](cql_test_env& e) { return seastar::async([&] {