コード例 #1
0
ファイル: SqlServer.cs プロジェクト: ujwalbhattarai8/frapid
        public async Task CleanupDbAsync(string tenant, string database)
        {
            string sql = @"SET NOCOUNT ON;
                            DECLARE @sql nvarchar(MAX);
                            DECLARE @queries TABLE(id int identity, query nvarchar(500), done bit DEFAULT(0));
                            DECLARE @id int;
                            DECLARE @query nvarchar(500);


                            INSERT INTO @queries(query)
                            SELECT 
	                            'EXECUTE dbo.drop_schema ''' + sys.schemas.name + ''''+ CHAR(13) AS query
                            FROM sys.schemas
                            WHERE principal_id = 1
                            AND name != 'dbo'
                            ORDER BY schema_id;



                            WHILE(SELECT COUNT(*) FROM @queries WHERE done = 0) > 0
                            BEGIN
                                SELECT TOP 1 
		                            @id = id,
		                            @query = query
	                            FROM @queries 
	                            WHERE done = 0
	
	                            EXECUTE(@query);

                                UPDATE @queries SET done = 1 WHERE id=@id;
                            END;";

            string connectionString = FrapidDbServer.GetSuperUserConnectionString(tenant, database);

            using (var connection = new SqlConnection(connectionString))
            {
                using (var command = new SqlCommand(sql, connection))
                {
                    connection.Open();

                    var message = await command.ExecuteScalarAsync().ConfigureAwait(false);

                    if (message != null)
                    {
                        InstallerLog.Information($"Could not completely clean database \"{tenant}\" due to dependency issues. Trying again.");
                        await CleanupDbAsync(tenant, database).ConfigureAwait(false);
                    }
                }
            }
        }
コード例 #2
0
        public async Task <bool> InstallAsync()
        {
            string meta      = DbProvider.GetMetaDatabase(this.Tenant);
            var    inspector = new DbInspector(this.Tenant, meta);
            bool   hasDb     = await inspector.HasDbAsync().ConfigureAwait(false);

            bool isWellKnown = inspector.IsWellKnownDb();

            if (hasDb)
            {
                if (IsDevelopment())
                {
                    InstallerLog.Verbose("Cleaning up the database.");
                    await this.CleanUpDbAsync().ConfigureAwait(true);
                }
                else
                {
                    InstallerLog.Information("Warning: database already exists. Please remove the database first.");
                    InstallerLog.Verbose($"No need to create database \"{this.Tenant}\" because it already exists.");
                }
            }

            if (!isWellKnown)
            {
                InstallerLog.Verbose(
                    $"Cannot create a database under the name \"{this.Tenant}\" because the name is not a well-known tenant name.");
            }

            if (!hasDb && isWellKnown)
            {
                InstallerLog.Information($"Creating database \"{this.Tenant}\".");
                await this.CreateDbAsync().ConfigureAwait(false);

                return(true);
            }

            return(false);
        }