/// <summary>
        /// Rename the specified column in the targeted table only if the column exists.
        /// <para>IMPORTANT: In Cassandra only the Primary key can be renamed.</para>
        /// </summary>
        ///
        /// <param name="self">The instance of the Cassandra Fluent Migrator helper.</param>
        /// <param name="table">The table where we want to rename the column.</param>
        /// <param name="old">The column to be renamed.</param>
        /// <param name="target">The new column name.</param>
        /// <returns>The Cassandra Fluent Migrator helper.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when one or all the specified arguments are invalid or null.</exception>
        /// <exception cref="ObjectNotFoundException">Thrown when the table doesn't exists.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the Column is not a primary key or the target column name already exists.</exception>
        public static async Task <ICassandraFluentMigrator> RenamePrimaryColumnAsync([NotNull] this ICassandraFluentMigrator self, [NotNull] string table, [NotNull] string old, [NotNull] string target)
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator object]");
            Check.NotEmptyNotNull(table, $"The argument [table]");
            Check.NotEmptyNotNull(old, $"The argument [old name]");
            Check.NotEmptyNotNull(target, $"The argument [new name]");

            table  = table.NormalizeString();
            old    = old.NormalizeString();
            target = target.NormalizeString();

            if (!self.DoesColumnExists(table, old))
            {
                return(self);
            }

            if (self.DoesColumnExists(table, target))
            {
                throw new InvalidOperationException(AppErrorsMessages.TYPE_COLUMN_EXISTS.NormalizeString(target));
            }

            if (!self.IsPrimaryKey(table, old))
            {
                throw new InvalidOperationException(AppErrorsMessages.CAN_NOT_RENAME_NONE_PRIMARY_KEY.NormalizeString(old));
            }

            return(await self.ExecuteRenameColumnQueryAsync(table, old, target));
        }
Exemplo n.º 2
0
 public CfmUdtExtensionsTests()
 {
     if (this.session is null)
     {
         this.session   = this.GetTestCassandraSession(KEYSPACE);
         this.cfmHelper = new CassandraFluentMigrator(this.session);
     }
 }
        /// <summary>
        /// Adds the specified column to the targeted table only if the column doesn't exist.
        /// </summary>
        ///
        /// <param name="self">The instance of the Cassandra Fluent Migrator helper.</param>
        /// <param name="table">The table to which we want to add the new column.</param>
        /// <param name="column">The new column.</param>
        /// <param name="type">The column type.</param>
        /// <param name="shouldBeFrozen">Define if the type should be treated as a frozen type or not.</param>
        /// <returns>The Cassandra Fluent Migrator helper.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when one or all the specified arguments are invalid or null.</exception>
        /// <exception cref="ObjectNotFoundException">Thrown when the table doesn't exists.</exception>
        public static async Task <ICassandraFluentMigrator> AddColumnAsync([NotNull] this ICassandraFluentMigrator self, [NotNull] string table, [NotNull] string column, [NotNull] Type type, bool shouldBeFrozen = false)
        {
            // Validate the parameters.
            Check.NotNull(self, $"The argument [cassandra fluent migrator]");
            Check.NotNull(type, $"The argument [{nameof(type)}]");

            Check.NotEmptyNotNull(table, $"The argument [table]");
            Check.NotEmptyNotNull(column, $"The argument [{nameof(column)}]");

            return(await self.ExecuteAddColumnAsync(table, column, type.GetCqlType(shouldBeFrozen)));
        }
        /// <summary>
        /// Adds the specified column to the targeted table only if the column doesn't exist.
        ///
        /// <para>Note: The method automatically resolve the type of the column.</para>
        /// </summary>
        ///
        /// <typeparam name="Table">The Table where we should search for the column type.</typeparam>
        /// <param name="self">The instance of the Cassandra Fluent Migrator helper.</param>
        /// <param name="table">The table to which we want to add the new column.</param>
        /// <param name="column">The new column.</param>
        /// <param name="shouldBeFrozen">Define if the type should be treated as a frozen type or not.</param>
        /// <returns>The Cassandra Fluent Migrator helper.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when one or all the specified arguments are invalid or null.</exception>
        /// <exception cref="ObjectNotFoundException">Thrown when the table doesn't exists.</exception>
        public static async Task <ICassandraFluentMigrator> AddColumnAsync <Table>([NotNull] this ICassandraFluentMigrator self, [NotNull] string table, [NotNull] string column, bool shouldBeFrozen = false)
            where Table : class
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator]");
            Check.NotEmptyNotNull(table, $"The argument [{nameof(table)}]");
            Check.NotEmptyNotNull(column, $"The argument [{nameof(column)}]");

            var typeName = self.GetColumnType <Table>(column, shouldBeFrozen);

            return(await self.ExecuteAddColumnAsync(table, column, typeName));
        }
Exemplo n.º 5
0
        public CassandraMigratorTests()
        {
            if (this.migrator is null)
            {
                var serviceProvider = this.GetTestInMemoryProvider();
                var logger          = serviceProvider.GetTestService <ILogger <CassandraMigrator> >();

                this.migrator = new CassandraMigrator(serviceProvider, logger);
                this.cfm      = serviceProvider.GetTestService <ICassandraFluentMigrator>();
            }
        }
        private static async Task <ICassandraFluentMigrator> ExecuteAddColumnAsync([NotNull] this ICassandraFluentMigrator self, [NotNull] string table, [NotNull] string column, [NotNull] string type)
        {
            table  = table.NormalizeString();
            column = column.NormalizeString();
            type   = type.NormalizeString();

            if (self.DoesColumnExists(table, column))
            {
                return(self);
            }

            return(await self.ExecuteCreateColumnQueryAsync(table, column, type));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Check if the specified column is a [PrimaryKey].
        /// </summary>
        ///
        /// <param name="self">The Cassandra Fluent Migrator.</param>
        /// <param name="table">The Cassandra table name.</param>
        /// <param name="column">The Column to check.</param>
        /// <returns>True if Primary, False Otherwise.</returns>
        ///
        /// <exception cref="ApplicationException">Thrown when the Column is not a primary key.</exception>
        internal static bool IsPrimaryKey([NotNull] this ICassandraFluentMigrator self, [NotNull] string table, [NotNull] string column)
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator object]");
            Check.NotEmptyNotNull(table, $"The argument [table]");
            Check.NotEmptyNotNull(column, $"The argument [{nameof(column)}]");

            var session = self.GetCassandraSession();

            return(session
                   .Cluster
                   .Metadata
                   .GetTable(session.Keyspace, table.NormalizeString())
                   .PartitionKeys
                   .Any(x => x.Name.NormalizeString() == column.NormalizeString()));
        }
        /// <summary>
        /// Alter the specified User-Defined type by adding a new column only if it doesn't exist.
        /// If the UDT doesn't exists, the method throws an exception.
        /// If the column exists, the method skips the action.
        /// </summary>
        ///
        /// <param name="self">The Cassandra Fluent Migrator.</param>
        /// <param name="udt">The name of the User-Defined type.</param>
        /// <param name="column">The name of the column to be added.</param>
        /// <param name="type">The type of the new column.</param>
        /// <param name="shouldBeFrozen">Define if the type should be treated as a frozen type or not.</param>
        /// <returns>The Cassandra Fluent Migrator helper.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when one or all the specified arguments are invalid or null.</exception>
        /// <exception cref="ObjectNotFoundException">Thrown when the User-Defined type doesn't exists.</exception>
        public static async Task <ICassandraFluentMigrator> AlterUdtAddColumnAsync([NotNull] this ICassandraFluentMigrator self, [NotNull] string udt, string column, Type type, bool shouldBeFrozen = false)
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator]");
            Check.NotNull(type, $"The argument [{nameof(type)}]");

            Check.NotEmptyNotNull(udt, $"The argument [User-Defined type (udt)]");
            Check.NotNull(column, $"The argument [{nameof(column)}]");

            if (self.DoesUdtColumnExists(udt, column))
            {
                return(self);
            }

            return(await self.ExecuteAlterUdtAddColumnQuery(udt, column, type.GetCqlType(shouldBeFrozen)));
        }
        /// <summary>
        /// Drops the specified column from the targeted table only if the column exists.
        /// </summary>
        ///
        /// <param name="self">The instance of the Cassandra Fluent Migrator helper.</param>
        /// <param name="table">The table from which we want to delete the column.</param>
        /// <param name="column">The column to be deleted.</param>
        /// <returns>The table Instance.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when one or all the specified arguments are invalid or null.</exception>
        /// <exception cref="ObjectNotFoundException">Thrown when the table doesn't exists.</exception>
        public static async Task <ICassandraFluentMigrator> DropColumnAsync(this ICassandraFluentMigrator self, [NotNull] string table, [NotNull] string column)
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator object]");
            Check.NotEmptyNotNull(table, $"The argument [{nameof(table)}]");
            Check.NotEmptyNotNull(column, $"The argument [{nameof(column)}]");

            table  = table.NormalizeString();
            column = column.NormalizeString();

            if (!self.DoesColumnExists(table, column))
            {
                return(self);
            }

            return(await self.ExecuteDropColumnQueryAsync(table, column));
        }
Exemplo n.º 10
0
        private static async Task <ICassandraFluentMigrator> ExecuteStatementAsync([NotNull] this ICassandraFluentMigrator self, [NotNull] string query, [NotNull] string errorMessage)
        {
            try
            {
                IStatement statement = new SimpleStatement(query);
                await self
                .GetCassandraSession()
                .ExecuteAsync(statement);
            }
            catch (Exception ex)
            {
                // Return the error if the message is other than the column exists.
                if (!ex.Message.NormalizeString().Contains(errorMessage.NormalizeString()))
                {
                    throw ex;
                }
            }

            return(self);
        }
Exemplo n.º 11
0
        public void DoesUdtExists_InvalidArguments_Faield()
        {
            try
            {
                this.cfmHelper.DoesUdtExists(null);
            }
            catch (NullReferenceException ex)
            {
                var expected = string.Format(INVALID_OR_EMPTY_ARGUMENT, "udt").ToLower();
                Assert.Contains(expected, ex.Message.ToLower());
            }

            try
            {
                ICassandraFluentMigrator tempHelper = null;
                tempHelper.DoesUdtExists(null);
            }
            catch (NullReferenceException ex)
            {
                Assert.Contains("Object reference not set to an instance of an object.".ToLower(), ex.Message.ToLower());
            }
        }
 public InitialMigration(ILogger <InitialMigration> logger, ICassandraFluentMigrator cfm)
 {
     this.cfm    = cfm;
     this.logger = logger;
 }
Exemplo n.º 13
0
        /// <summary>
        /// Build and execute the Reanme column query statement.
        /// </summary>
        ///
        /// <param name="self">The Cassandra Fluent Migrator.</param>
        /// <param name="table">The Cassandra table name.</param>
        /// <param name="column">Old Column to be renamed.</param>
        /// <param name="target">New Column name.</param>
        /// <returns>Cassandra Fluent Migrator.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when the arguments are empty or null.</exception>
        internal static async Task <ICassandraFluentMigrator> ExecuteRenameColumnQueryAsync([NotNull] this ICassandraFluentMigrator self, [NotNull] string table, [NotNull] string column, [NotNull] string target)
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator object]");
            Check.NotEmptyNotNull(table, $"The argument [table]");
            Check.NotEmptyNotNull(column, $"The argument [old name]");
            Check.NotEmptyNotNull(target, $"The argument [new name]");

            var query = TableCqlStatements.TABLE_RENAME_COLUMN_STATEMENT.NormalizeString(table, column, target);

            return(await self.ExecuteStatementAsync(query, AppErrorsMessages.COLUMN_EXISTS_FOR_RENAME.NormalizeString(column, target, self.GetCassandraSession().Keyspace)));
        }
        /// <summary>
        /// Delete the User-Defined type if exists.
        /// If the UDT doesn't exists, the method skips the deletion.
        ///
        /// <para>Note: If the User-Defined type name is [Null || Empty] the method will take the generic type {TEntity} name.</para>
        /// </summary>
        ///
        /// <typeparam name="TEntity">The calss where the method should look for the properties and their types.</typeparam>
        /// <param name="self">The Cassandra Fluent Migrator.</param>
        /// <param name="name">The name of the udt (Optional).</param>
        /// <returns>The Cassandra Fluent Migrator helper.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when one or all the specified arguments are invalid or null.</exception>
        public static async Task <ICassandraFluentMigrator> DeleteUserDefinedTypeAsync <TEntity>([NotNull] this ICassandraFluentMigrator self, [NotNull] string name = default)
            where TEntity : class
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator]");

            name = string.IsNullOrWhiteSpace(name) ? typeof(TEntity).Name.NormalizeString() : name.NormalizeString();

            if (!self.DoesUdtExists(name))
            {
                return(self);
            }

            return(await self.ExecuteDeleteUdtAsync <TEntity>(name));
        }
        /// <summary>
        /// Alter the specified User-Defined type by renaming the column name by the target name.
        /// If the UDT doesn't exists, the method throws an exception.
        /// If the new column exists, the method throws an exception.
        /// </summary>
        ///
        /// <param name="self">The Cassandra Fluent Migrator.</param>
        /// <param name="udt">The name of the User-Defined type.</param>
        /// <param name="column">The name of the column to be renamed.</param>
        /// <param name="target">The new column name.</param>
        /// <returns>The Cassandra Fluent Migrator helper.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when one or all the specified arguments are invalid or null.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the target column name exists.</exception>
        /// <exception cref="ObjectNotFoundException">Thrown when the User-Defined type doesn't exists.</exception>
        public static async Task <ICassandraFluentMigrator> AlterUdtRenameColumnAsync([NotNull] this ICassandraFluentMigrator self, [NotNull] string udt, [NotNull] string column, [NotNull] string target)
        {
            Check.NotNull(self, "The argument [Cassandra Fluent Migrator]");
            Check.NotEmptyNotNull(udt, $"The argument [User-Defined type]");
            Check.NotEmptyNotNull(column, $"The argument [{nameof(column)}]");
            Check.NotEmptyNotNull(target, $"The argument [{nameof(target)}]");

            if (!self.DoesUdtColumnExists(udt, column))
            {
                return(self);
            }

            if (self.DoesUdtColumnExists(udt, target))
            {
                throw new InvalidOperationException(AppErrorsMessages.TYPE_UDT_COLUMN_EXISTS.NormalizeString(target));
            }

            return(await self.ExecuteAlterUdtRenameColumnQuery(udt, column, target));
        }
        /// <summary>
        /// Alter the specified User-Defined type by adding a new column only if it doesn't exist.
        /// If the UDT doesn't exists, the method throws an exception.
        /// If the column exists, the method skips the action.
        /// </summary>
        ///
        /// <typeparam name="TEntity">The calss where the method should search for the properties and their types.</typeparam>
        /// <param name="self">The Cassandra Fluent Migrator.</param>
        /// <param name="udt">The name of the User-Defined type.</param>
        /// <param name="column">The name of the column to be added.</param>
        /// <returns>The Cassandra Fluent Migrator helper.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when one or all the specified arguments are invalid or null.</exception>
        /// <exception cref="ObjectNotFoundException">Thrown when the User-Defined type doesn't exists.</exception>
        public static async Task <ICassandraFluentMigrator> AlterUdtAddColumnAsync <TEntity>([NotNull] this ICassandraFluentMigrator self, [NotNull] string udt, string column)
            where TEntity : class
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator]");

            Check.NotEmptyNotNull(udt, $"The argument [User-Defined type (udt)]");
            Check.NotNull(column, $"The argument [{nameof(column)}]");

            if (self.DoesUdtColumnExists(udt, column))
            {
                return(self);
            }

            var typeName = self.GetColumnType <TEntity>(column);

            return(await self.ExecuteAlterUdtAddColumnQuery(udt, column, typeName));
        }
Exemplo n.º 17
0
        /// <summary>
        /// Build and execute the Drop column query statement.
        /// </summary>
        ///
        /// <param name="self">The Cassandra Fluent Migrator.</param>
        /// <param name="table">The Cassandra table name.</param>
        /// <param name="column">The Column to be deleted.</param>
        /// <returns>Cassandra Fluent Migrator.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when the arguments are empty or null.</exception>
        internal static async Task <ICassandraFluentMigrator> ExecuteDropColumnQueryAsync([NotNull] this ICassandraFluentMigrator self, [NotNull] string table, [NotNull] string column)
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator object]");
            Check.NotEmptyNotNull(table, $"The argument [{nameof(table)}]");
            Check.NotEmptyNotNull(column, $"The argument [{nameof(column)}]");

            var query = TableCqlStatements.TABLE_DROP_COLUMN_STATEMENT.NormalizeString(table, column);

            return(await self.ExecuteStatementAsync(query, AppErrorsMessages.COLUMN_NOT_FOUND.NormalizeString(column, table)));
        }
 public AddActiveColumnToUsersMigration(ILogger <AddActiveColumnToUsersMigration> logger, ICassandraFluentMigrator cfm)
 {
     this.cfm    = cfm;
     this.logger = logger;
 }
        /// <summary>
        /// Build and execute the Add Column query statement for the User-Defined types.
        /// </summary>
        ///
        /// <param name="self">The Cassandra Fluent Migrator.</param>
        /// <param name="udt">The name of the User-Defined type.</param>
        /// <param name="column">The name of the column to be added.</param>
        /// <param name="type">The type of the column.</param>
        /// <returns>The Cassandra CQL query.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when the arguments are empty or null.</exception>
        internal static async Task <ICassandraFluentMigrator> ExecuteAlterUdtAddColumnQuery([NotNull] this ICassandraFluentMigrator self, [NotNull] string udt, [NotNull] string column, [NotNull] string type)
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator]");

            Check.NotEmptyNotNull(udt, $"The argument [User-Defined type (udt)]");
            Check.NotEmptyNotNull(type, $"The argument [{nameof(type)}]");
            Check.NotEmptyNotNull(column, $"The argument [{nameof(column)}]");

            var query = UdtCqlStatements.TYPE_ADD_COLUMN_STATEMENT.NormalizeString(udt, column, type);

            return(await self.ExecuteUdtStatementAsync(query, AppErrorsMessages.TYPE_UDT_COLUMN_EXISTS.NormalizeString(column)));
        }
        /// <summary>
        /// Build and execute the delete User-Defined type query statement.
        /// </summary>
        ///
        /// <typeparam name="TEntity">The calss where the that represent the User-Defined type.</typeparam>
        /// <param name="self">The Cassandra Fluent Migrator.</param>
        /// <param name="name">The name of the User-Defined type (Optional: will be taken from the {TEntity} if not specified).</param>
        /// <returns>The Cassandra CQL query.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when the arguments are empty or null.</exception>
        internal static async Task <ICassandraFluentMigrator> ExecuteDeleteUdtAsync <TEntity>([NotNull] this ICassandraFluentMigrator self, [NotNull] string name)
            where TEntity : class
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator]");
            Check.NotEmptyNotNull(name, $"The argument [{nameof(name)}]");

            var query = UdtCqlStatements.TYPE_DROP_STATEMENT.NormalizeString(name);

            return(await self.ExecuteUdtStatementAsync(query));
        }
        /// <summary>
        /// Build and execute the create User-Defined type query statement.
        /// The method automatically get the properties and their types based on the
        /// {TEntity} class.
        /// </summary>
        ///
        /// <typeparam name="TEntity">The class where the method should look for the properties and their types.</typeparam>
        /// <param name="self">The Cassandra Fluent Migrator.</param>
        /// <param name="name">The name of the User-Defined type.</param>
        /// <param name="shouldBeFrozen">Define if the type should be treated as a frozen type or not.</param>
        /// <returns>The Cassandra CQL query.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when the arguments are empty or null.</exception>
        internal static async Task <ICassandraFluentMigrator> ExecuteBuildUdtAsync <TEntity>([NotNull] this ICassandraFluentMigrator self, [NotNull] string name, bool shouldBeFrozen)
            where TEntity : class
        {
            Check.NotNull(self, $"The argument [cassandra fluent migrator]");
            Check.NotEmptyNotNull(name, $"The argument [{nameof(name)}]");

            var count = 0;

            var properties = typeof(TEntity).GetProperties();

            var query = new StringBuilder(UdtCqlStatements.TYPE_CREATE_STATEMENT.NormalizeString(name));

            foreach (var property in properties)
            {
                var propName = property.Name.NormalizeString();
                var propType = property.PropertyType.GetCqlType(shouldBeFrozen);

                query.Append($"{propName} {propType}");

                if (count < properties.Length - 1)
                {
                    query.Append(", ");
                    count++;
                }
            }

            var statement = query
                            .Append(");")
                            .ToString()
                            .NormalizeString();

            return(await self.ExecuteUdtStatementAsync(statement));
        }
Exemplo n.º 22
0
        /// <summary>
        /// Build and execute the Add column query statement.
        /// </summary>
        ///
        /// <param name="self">The Cassandra Fluent Migrator.</param>
        /// <param name="table">The Cassandra table name.</param>
        /// <param name="column">The column that we want to add.</param>
        /// <param name="type">The type of the columns.</param>
        /// <returns>Cassandra Fluent Migrator.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when the arguments are empty or null.</exception>
        internal static async Task <ICassandraFluentMigrator> ExecuteCreateColumnQueryAsync([NotNull] this ICassandraFluentMigrator self, [NotNull] string table, [NotNull] string column, [NotNull] string type)
        {
            Check.NotNull(self, $"The argument [table]");
            Check.NotEmptyNotNull(column, $"The argument [{nameof(column)}]");
            Check.NotEmptyNotNull(column, $"The argument [{nameof(type)}]");

            var query = TableCqlStatements.TABLE_ADD_COLUMN_STATEMENT.NormalizeString(table, column, type);

            return(await self.ExecuteStatementAsync(query, AppErrorsMessages.COLUMN_EXISTS.NormalizeString(column)));
        }
 public YetAnotherChangesMigration(ILogger <YetAnotherChangesMigration> logger, ICassandraFluentMigrator cfm)
 {
     this.cfm    = cfm;
     this.logger = logger;
 }
Exemplo n.º 24
0
 public AddingNewTypeMigration(ICassandraFluentMigrator cfmHelper)
 {
     this.cfmHelper = cfmHelper;
 }