Ejemplo n.º 1
0
        public void BeforeEachTest()
        {
            DatabaseConnection = connectionFactory.Get();
            DB = Query.Db(TEST_DB_NAME);
            // Create DB if it does not exist
            if (!DatabaseConnection.Run(Query.DbList()).Contains(TEST_DB_NAME))
            {
                DatabaseConnection.Run(Query.DbCreate(TEST_DB_NAME));
            }

            IdentityContext = new IdentityContext(DatabaseConnection, DB);

            // Cleanup if needed
            if (DatabaseConnection.Run(DB.TableList()).Contains("IdentityUsers"))
            {
                DatabaseConnection.Run(DB.TableDrop("IdentityUsers"));
            }

            if (DatabaseConnection.Run(DB.TableList()).Contains("IdentityRoles"))
            {
                DatabaseConnection.Run(DB.TableDrop("IdentityRoles"));
            }

            // Create Tables
            DatabaseConnection.Run(DB.TableCreate("IdentityUsers"));
            DatabaseConnection.Run(DB.TableCreate("IdentityRoles"));
        }
        public ActionResponse Execute()
        {
            var         response = new ActionResponse();
            var         database = _output.Connection.Database;
            var         table    = _output.Entity.Alias;
            var         conn     = _factory.Get();
            DmlResponse res;

            if (!conn.Run(Query.DbList()).Contains(database))
            {
                res = conn.Run(Query.DbCreate(database));
                _output.Info($"Created {database}");
            }

            IDatabaseQuery db = Query.Db(database);

            if (conn.Run(db.TableList()).Contains(table))
            {
                res = conn.Run(db.TableDrop(table));
                _output.Info($"Dropped {table}");
            }

            var keys = _output.Entity.GetPrimaryKey();

            if (keys.Count() > 1)
            {
                _output.Error("You can't create a composite primary key in RethinkDB.");
            }
            else
            {
                if (keys.Any())
                {
                    conn.Run(db.TableCreate(table, primaryKey: keys.First().Alias));
                }
                else
                {
                    conn.Run(db.TableCreate(table));
                }
                _output.Info($"Created {table}");
            }

            return(response);
        }
        /// <summary>
        /// Makes sure that the Database and Table is created in RethinkDB.
        /// </summary>
        /// <param name="databaseName">The name of the database to create in RethinkDB if it doesn't exist.</param>
        /// <param name="tableName">The name of the table to create in RethinkDB if it doesn't exist.</param>
        private void EnsureDbCreated(string databaseName, string tableName)
        {
            using (IConnection connection = _connectionFactory.Get())
            {
                if (!connection.Run(Query.DbList()).Contains(databaseName))
                {
                    connection.Run(Query.DbCreate(databaseName));
                }

                if (!connection.Run(_db.TableList()).Contains(tableName))
                {
                    connection.Run(_db.TableCreate(tableName));
                }
            }
        }