Skip to content

Commit

Permalink
A working clone setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed Jun 23, 2011
1 parent 63d9cd5 commit ec82793
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
.bundle
db/*.sqlite3
db/*structure.sql
log/*.log
tmp/
2 changes: 2 additions & 0 deletions README
Expand Up @@ -16,6 +16,8 @@ http://msdn.microsoft.com/da-dk/magazine/gg535665%28en-us%29.aspx
Download and install the AdventureWorks database. A link can be found in the article
above.

ENV['ADVENTUREWORKS_SA_PASS']

Create an "hr" login in SQL Server with a password of "hr". Make the default database
AdventureWorks with a default schema of HumanResources. Make sure to also select
HumanResourcees as an owned schema while in the user properties for the AdventureWorks
Expand Down
7 changes: 3 additions & 4 deletions db/bin/smoscript
Expand Up @@ -12,16 +12,15 @@
args = ARGV.dup

host = args[1].dup
ip = `ping -c 1 -t 2 #{host} | grep ms | egrep -m1 -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'`.chomp
args[1] = ip # Safer on remote machine since host may not resolve remotely.
args[1] = 'localhost'

file = args[9].dup
file.sub! 'db', 'db/' # Fix filename from "dbdevelopment_structure.sql" to "db/development_structure.sql"
file.sub! 'db', 'db/'
args[9] = file

cmd = args.join(' ')
cdir = `pwd`.chomp

`ssh #{host} \"mkdir -p db && smoscript #{cmd}\"`
`scp #{host}:#{file} #{cdir}/#{file}`
`scp #{host}:#{file} #{cdir}/#{file}` if file =~ /structure/

29 changes: 29 additions & 0 deletions db/bin/sqlcmd
@@ -1,2 +1,31 @@
#!/usr/bin/env ruby
#
# A sqlcmd bin replacement for unix platforms for use by the Rails
# db:test:clone_structure rake task for the ActiveRecord SQL Server adapter.
#
# Make sure you have ssh access to your Windows box using the host
# name in your database.yml. If your windows ssh username is different
# than your unix username, setup a ~/.ssh/conf entry that fills in the
# details.
#

args = ARGV.dup

host_index = args.index('-S') + 1
host = args[host_index].dup
args[host_index] = 'localhost'

if file_index = args.index('-i')
findex = file_index + 1
args[findex] = args[findex].gsub /\\/, '/'
end

if query_index = args.index('-Q')
qindex = query_index + 1
args[qindex] = "\\\"#{args[qindex]}\\\""
end

cmd = args.join(' ')

`ssh #{host} \"sqlcmd #{cmd}\"`

88 changes: 78 additions & 10 deletions lib/tasks/databases.rake
Expand Up @@ -6,27 +6,95 @@ namespace :db do

override_task :dump => :environment do
abcs = ActiveRecord::Base.configurations
`smoscript -s #{abcs[Rails.env]['host']} -d #{abcs[Rails.env]['database']} -u #{abcs[Rails.env]['username']} -p #{abcs[Rails.env]['password']} -f db\\#{Rails.env}_structure.sql -A -U`
`smoscript -s #{abcs[Rails.env]['host']} -d #{abcs[Rails.env]['database']} -u sa -p #{ENV['ADVENTUREWORKS_SA_PASS']} -F db\\ -i -A -U`
`smoscript -s #{abcs[Rails.env]['host']} -d #{abcs[Rails.env]['database']} -u sa -p #{ENV['ADVENTUREWORKS_SA_PASS']} -f db\\#{Rails.env}_structure.sql -i -A -U`
end

end

namespace :test do

override_task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
abcs = ActiveRecord::Base.configurations
`sqlcmd -S #{abcs['test']['host']} -d #{abcs['test']['database']} -U #{abcs['test']['username']} -P #{abcs['test']['password']} -i db\\#{Rails.env}_structure.sql`
test = ActiveRecord::Base.configurations['test']
ordered_ddl_files = [
"Schema\\HumanResources.sql",
"Schema\\Person.sql",
"Schema\\Production.sql",
"Schema\\Purchasing.sql",
"Schema\\Sales.sql",
"UserDefinedDataType\\dbo.AccountNumber.sql",
"UserDefinedDataType\\dbo.Flag.sql",
"UserDefinedDataType\\dbo.Name.sql",
"UserDefinedDataType\\dbo.NameStyle.sql",
"UserDefinedDataType\\dbo.OrderNumber.sql",
"UserDefinedDataType\\dbo.Phone.sql",
"UserDefinedFunction\\dbo.ufnGetAccountingEndDate.sql",
"UserDefinedFunction\\dbo.ufnGetAccountingStartDate.sql",
"UserDefinedFunction\\dbo.ufnGetContactInformation.sql",
"UserDefinedFunction\\dbo.ufnGetDocumentStatusText.sql",
"UserDefinedFunction\\dbo.ufnGetProductDealerPrice.sql",
"UserDefinedFunction\\dbo.ufnGetProductListPrice.sql",
"UserDefinedFunction\\dbo.ufnGetProductStandardCost.sql",
"UserDefinedFunction\\dbo.ufnGetPurchaseOrderStatusText.sql",
"UserDefinedFunction\\dbo.ufnGetSalesOrderStatusText.sql",
"UserDefinedFunction\\dbo.ufnGetStock.sql",
"UserDefinedFunction\\dbo.ufnLeadingZeros.sql",
"XmlSchemaCollection\\HumanResources.HRResumeSchemaCollection.sql",
"XmlSchemaCollection\\Person.AdditionalContactInfoSchemaCollection.sql",
"XmlSchemaCollection\\Production.ManuInstructionsSchemaCollection.sql",
"XmlSchemaCollection\\Production.ProductDescriptionSchemaCollection.sql",
"XmlSchemaCollection\\Sales.IndividualSurveySchemaCollection.sql",
"XmlSchemaCollection\\Sales.StoreSurveySchemaCollection.sql",
"Table\\HumanResources.Department.sql",
"Table\\Person.Contact.sql",
"Table\\Person.CountryRegion.sql",
"Table\\Sales.SalesTerritory.sql",
"Table\\Person.StateProvince.sql",
"Table\\Person.Address.sql",
"Table\\HumanResources.Shift.sql",
"Table\\Production.ProductModel.sql",
"Table\\Production.ProductCategory.sql",
"Table\\Production.ProductSubcategory.sql",
"Table\\Production.UnitMeasure.sql",
"Table\\Production.Product.sql",
"StoredProcedure\\dbo.uspGetBillOfMaterials.sql",
"StoredProcedure\\dbo.uspGetEmployeeManagers.sql",
'StoredProcedure\\dbo.uspGetManagerEmployees.sql',
"StoredProcedure\\dbo.uspGetWhereUsedProductID.sql",
"StoredProcedure\\dbo.uspPrintError.sql",
"StoredProcedure\\dbo.uspLogError.sql",
"StoredProcedure\\HumanResources.uspUpdateEmployeeHireInfo.sql",
"StoredProcedure\\HumanResources.uspUpdateEmployeeLogin.sql",
"StoredProcedure\\HumanResources.uspUpdateEmployeePersonalInfo.sql",
"Table\\Purchasing.Vendor.sql",
"Table\\HumanResources.Employee.sql",
"Table\\Purchasing.ShipMethod.sql",
"Table\\Purchasing.PurchaseOrderHeader.sql",
"Table\\Sales.CreditCard.sql",
"Table\\Sales.Currency.sql",
"Table\\Sales.Customer.sql",
"Table\\Sales.CurrencyRate.sql",
"Table\\Sales.SalesPerson.sql",
"Table\\Sales.SalesOrderHeader.sql",
"Table\\Sales.SpecialOffer.sql",
"Table\\Sales.SpecialOfferProduct.sql",
"Table\\Sales.SalesReason.sql",
"development_structure.sql"
]
ordered_ddl_files.each do |file|
`sqlcmd -S #{test['host']} -d #{test['database']} -U sa -P #{ENV['ADVENTUREWORKS_SA_PASS']} -i \"db\\#{file}\"`
end
end

override_task :purge => :environment do
abcs = ActiveRecord::Base.configurations
test = abcs.deep_dup['test']
test_database = test['database']
test['database'] = 'master'
ActiveRecord::Base.establish_connection(test)
ActiveRecord::Base.connection.recreate_database!(test_database)
test = ActiveRecord::Base.configurations['test']
`sqlcmd -S #{test['host']} -d master -U sa -P #{ENV['ADVENTUREWORKS_SA_PASS']} -Q "DROP DATABASE #{test['database']}"`
`sqlcmd -S #{test['host']} -d master -U sa -P #{ENV['ADVENTUREWORKS_SA_PASS']} -Q "CREATE DATABASE #{test['database']}"`
`sqlcmd -S #{test['host']} -d #{test['database']} -U sa -P #{ENV['ADVENTUREWORKS_SA_PASS']} -Q "CREATE USER [#{test['username']}] FOR LOGIN [#{test['username']}] WITH DEFAULT_SCHEMA=[HumanResources]"`
end

end

end
end


0 comments on commit ec82793

Please sign in to comment.