Tuesday, April 22, 2014

Entity Framework Migrations

Below are some commands which are useful when it comes to migrating your database schema when using Entity Framework.

These commands needs to be run through Tools > NuGet Package Manager > Package Manager Console , under Visual Studio.

The code for this can be found under this folder ...\packages\EntityFramework.6.1.0\tools\EntityFramework.psm1

Enable-Migrations: Enables Code First Migrations in a project.
Add-Migration: Scaffolds a migration script for any pending model changes.
Update-Database: Applies any pending migrations to the database.
Get-Migrations: Displays the migrations that have been applied to the target database.

Enable-Migrations
Enables Code First Migrations in a project.

Syntax
Enable-Migrations [-EnableAutomaticMigrations] [[-ProjectName] <String>]
  [-Force] [<CommonParameters>]
Description
Enables Migrations by scaffolding a migrations configuration class in the project. If the  target database was created by an initializer, an initial migration will be created (unless automatic migrations are enabled via the EnableAutomaticMigrations parameter).

Parameters
-EnableAutomaticMigrations
Specifies whether automatic migrations will be enabled in the scaffolded migrations configuration. If ommitted, automatic migrations will be disabled.

-ProjectName <String>
Specifies the project that the scaffolded migrations configuration class will be added to. If omitted, the default project selected in package manager console is used.

-Force
Specifies that the migrations configuration be overwritten when running more than once for given project.

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.


Add-Migration
Scaffolds a migration script for any pending model changes.

Syntax
Add-Migration [-Name] <String> [-Force]
  [-ProjectName <String>] [-StartUpProjectName <String>]
  [-ConfigurationTypeName <String>] [-ConnectionStringName <String>]
  [-IgnoreChanges] [<CommonParameters>]

Add-Migration [-Name] <String> [-Force]
  [-ProjectName <String>] [-StartUpProjectName <String>]
  [-ConfigurationTypeName <String>] -ConnectionString <String>
  -ConnectionProviderName <String> [-IgnoreChanges] [<Common Parameters>]
Description
Scaffolds a new migration script and adds it to the project.

Parameters
-Name <String>
Specifies the name of the custom script.

-Force
Specifies that the migration user code be overwritten when re-scaffolding an existing migration.

-ProjectName <String>
Specifies the project that contains the migration configuration type to be used. If ommitted, the default project selected in package manager console is used.

-StartUpProjectName <String>
Specifies the configuration file to use for named connection strings. If omitted, the specified project’s configuration file is used.

-ConfigurationTypeName <String>
Specifies the migrations configuration to use. If omitted, migrations will attempt to locate a single migrations configuration type in the target project.

-ConnectionStringName <String>
Specifies the name of a connection string to use from the application’s configuration file.

-ConnectionString <String>
Specifies the the connection string to use. If omitted, the context’s default connection will be used.

-ConnectionProviderName <String>
Specifies the provider invariant name of the connection string.

-IgnoreChanges
Scaffolds an empty migration ignoring any pending changes detected in the current model. This can be used to create an initial, empty migration to enable Migrations for an existing database. N.B. Doing this assumes that the target database schema is compatible with the current model.

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.


Update-Database
Applies any pending migrations to the database.

Syntax
Update-Database [-SourceMigration <String>]
  [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>]
  [-StartUpProjectName <String>] [-ConfigurationTypeName <String>]
  [-ConnectionStringName <String>] [<CommonParameters>]

Update-Database [-SourceMigration <String>] [-TargetMigration <String>]
  [-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>]
  [-ConfigurationTypeName <String>] -ConnectionString <String>
  -ConnectionProviderName <String> [<CommonParameters>]
Description
Updates the database to the current model by applying pending migrations.

Parameters
-SourceMigration <String>
Only valid with -Script. Specifies the name of a particular migration to use as the update’s starting point. If ommitted, the last applied migration in the database will be used.

-TargetMigration <String>
Specifies the name of a particular migration to update the database to. If ommitted, the current model will be used.

-Script
Generate a SQL script rather than executing the pending changes directly.

-Force
Specifies that data loss is acceptable during automatic migration of the
database.

-ProjectName <String>
Specifies the project that contains the migration configuration type to be used. If ommitted, the default project selected in package manager console is used.

-StartUpProjectName <String>
Specifies the configuration file to use for named connection strings. If omitted, the specified project’s configuration file is used.

-ConfigurationTypeName <String>
Specifies the migrations configuration to use. If omitted, migrations will attempt to locate a single migrations configuration type in the target project.

-ConnectionStringName <String>
Specifies the name of a connection string to use from the application’s configuration file.

-ConnectionString <String>
Specifies the the connection string to use. If omitted, the context’s default connection will be used.

-ConnectionProviderName <String>
Specifies the provider invariant name of the connection string.

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.


Get-Migrations
Displays the migrations that have been applied to the target database.

Syntax
Get-Migrations [-ProjectName <String>] [-StartUpProjectName <String>]
  [-ConfigurationTypeName <String>] [-ConnectionStringName <String>]
  [<CommonParameters>]

Get-Migrations [-ProjectName <String>] [-StartUpProjectName <String>]
  [-ConfigurationTypeName <String>] -ConnectionString <String>
  -ConnectionProviderName <String> [<CommonParameters>]
Description
Displays the migrations that have been applied to the target database.

Parameters
-ProjectName <String>
Specifies the project that contains the migration configuration type to be used. If ommitted, the default project selected in package manager console is used.

-StartUpProjectName <String>
Specifies the configuration file to use for named connection strings. If omitted, the specified project’s configuration file is used.

-ConfigurationTypeName <String>
Specifies the migrations configuration to use. If omitted, migrations will attempt to locate a single migrations configuration type in the target project.

-ConnectionStringName <String>
Specifies the name of a connection string to use from the application’s configuration file.

-ConnectionString <String>
Specifies the the connection string to use. If omitted, the context’s default connection will be used.

-ConnectionProviderName <String>
Specifies the provider invariant name of the connection string.

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.

Visualise XML beautifully

While looking for a way on how to show and traverse XML in a decent way, I came across this site: http://xmlgrid.net/ .

This is a really useful site, where you can upload your own XML, look at or edit the data, and also get the XPath of the current node.

Kudos to the devs!

Tuesday, October 22, 2013

Changing which Xcode version to use on a Mac

On a Mac you can have two different versions of Xcode happily living side by side.
In my case I needed to be able to use both Xcode 4.6, and Xcode 5.0.
My build machine had version 5.0, and I needed to use version 4.6 temporarily.
This is how I carried it out.

 Install the old Xcode under /Applications/XCode4/
·         Check which Xcode version the xcodebuild version uses
o   Run xcodebuild –version
o   This for now will be Version 5.0
·         Check which path Xcodebuild uses
o   Run xcode-select --print-path
o   This should be /Applications/Xcode.app/Contents/Developer
·         Change which xcode version to use
o   Sudo xcode-select –switch /Applications/XCode4/Xcode.app/Contents/Developer
o   Enter the account/admin password
·         Check which Xcode version the xcodebuild version now uses
o   Run xcodebuild –version
o   This for now will be Version 4.6.3
·         Check which path Xcodebuild uses
o   Run xcode-select –print-path
o   This should be /Applications/XCode4/Xcode.app/Contents/Developer
·         Call Xbuildcode as normal  

To revert this change, we will need to :
·         Check which Xcode version the xcodebuild version uses
o   Run xcodebuild –version
o   This for now will be Version 4.6.3
·         Check which path Xcodebuild uses
o   Run xcode-select –print-path
o   This should be /Applications/XCode4/Xcode.app/Contents/Developer
·         Change which xcode version to use
o   Sudo xcode-select –switch /Applications /Xcode.app/Contents/Developer
o   Enter the account/admin password
·         Check which Xcode version the xcodebuild version now uses
o   Run xcodebuild –version
o   This for now will be Version 5.0
·         Check which path Xcodebuild uses
o   Run xcode-select –print-path
o   This should be /Applications/Xcode.app/Contents/Developer

·         Call Xbuildcode as normal  

Wednesday, August 28, 2013

Send email using Powershell

Sending an email using Powershell couldn't be easier. You just need to use the Send-MailMessage commandlet.

It is used as follows.

Send-MailMessage
 -From "ReubenBartolo@test.com"
-To "ReubenBartolo@test.com"
-Subject "Hello"
-Body "World"
-SmtpServer "mail.test.com"


To add more than one recepient, just add as follows.

-To "Receiver1@test.com","Receiver2@test.com"

Thursday, August 22, 2013

Force Powershell to always create an array

Imagine you have a file called buildLog.txt, which contains

1
2
3
22

Now, also imagine you want to find out if a line contains a specific number in this text file.

You can run a command like this:


$allWarnings = Select-String build.log.txt -Pattern "2"  | Select-Object Line,LineNumber,FileName | Sort-Object -Property Line -Unique ; if ($allWarnings.Count -gt 0) { $allWarnings | Format-List; }

This works correctly and returns:

Line       : 2
LineNumber : 2
Filename   : build.log.txt

Line       : 22
LineNumber : 4
Filename   : build.log.txt


But if you run it to check if any sentences contain a 1, something weird happens:

$allWarnings = Select-String build.log.txt -Pattern "1"  | Select-Object Line,LineNumber,FileName | Sort-Object -Property Line -Unique ; if ($allWarnings.Count -gt 0) { $allWarnings | Format-List; }

This returns no results. This is very weird and unexpected behaviour, to say the least. But the reason can be understood once you break the steps down.

In the first case, we have multiple results, which creates an array. Then we do a .Count on this array, and we get the result.
In the second case, we only have one result. This is an object, and not an array. Therefore this has no .Count property, so .Count will return null.

The fix is luckily very easy. Simply force the result to always be an array. This is done by simply adding one keyword in front.

[array]$allWarnings = Select-String build.log.txt -Pattern "1"  | Select-Object Line,LineNumber,FileName | Sort-Object -Property Line -Unique ; if ($allWarnings.Count -gt 0) { $allWarnings | Format-List; }

This now works as expected.


Line       : 1
LineNumber : 1
Filename   : build.log.txt


Success!

Monday, August 12, 2013

Perforce Merging Advanced Options

When you are a Perforce power user, you carry out a lot of complex merges between different branches. Many times you will need to make use of the advanced options, and I find myself having to consult the documentation every time. So I am pasting the definitions down here:


Limit revision rangePerforce keeps track of which revisions of a source file have already been integrated to a target file. Normally integrate opens the target if any source revisions have not yet been integrated. You can restrict which source files revisions will be considered by entering a revision range here.

  • You can use symbolic revision numbers (for example, "@12345") or absolute revision numbers(for example, "#45").
  • If you enter a "starting with" revision, no source revisions prior to it will be considered. If you don't supply the "@" or "#" prefix, "@" will be assumed.
  • If you enter an "ending with" revision, no higher source revisions higher are considered. If you omit the "@" or "#" prefix, it is assumed to be the same type of revision number as the starting revision you supplied, or "@" if you didn't specify a starting revision.
Put open files in changelistIf your client workspace has more than one pending changelist, you can select one in which to open the files selected for integration..
Enable baseless mergesNormally Perforce only opens a target file for integrate if it can find a common base between it and the source file to use as a base for doing a merge. A target file and source file not directly related to each other by branching have no common base. By default, the target are not opened in this case. You can use the "enable baseless merges" option to force target files to be opened even if they are not related to source files by branching. If you do this, Perforce arbitrarily chooses the #1 revision of the source files to use as a merge base.

Permit deletes/re-adds
If the ending revision of the source file is a deleted file, Perforce normally opens the target file for delete unless the target file has any revisions that have not themselves been integrated to the source file. If the source file exists and the target file does not, Perforce normally opens the target file for branch/add unless the target file is a deleted file. Checking the "permit deletes/re-adds" option makes the integrate command open the target file for delete or branch/add even if these conditions exist.


Re-branch source on top of deleted targets
If the source file exists and the target file does not, Perforce normally opens the target file for branch/add unless the target file is a deleted file. Checking this option opens the target file for branch/add even if it is deleted.


Delete modified targets if source is deleted
If the ending revision of the source file is a deleted file, Perforce normally opens the target file for delete unless the target file has any revisions that have not themselves been integrated to the source file. Checking this option opens the target file for delete even if it has been edited.


Force re-integration if previously integrated
Normally Perforce doesn't open a target file for integrate if all of the source file revisions under consideration have already been integrated to it. Checking the "force re-integration" option makes theintegrate command open the target file for integrate regardless of previous integration history. When you submit the file, integration history between the two files will be effectively rewritten to reflect the most recent of the duplicate integrations.


Don't copy newly branched files to workspace
Normally target files opened for branch/add are created in your workspace. However, because the content of the new target files is the same as that of the source files, your workspace files are not required to create the new files in the depot. If you don't need copies of the newly branched files in your workspace—for example, you are branching a large codeline but you don't plan to do work in the new codeline yourself—use this option.


Change target filetype to match source
Normally, Perforce doesn't change the filetype of target files when you integrate to them. Each newly branched file is given a filetype that matches that of the file it was branched from. Subsequent changes to the source filetype will not affect the target file during integration unless you use this option. When you use this option, the filetype of each target file will be changed to match that of the corresponding source file at the revision you are integrating from. The filetype change takes effect when you submit the target files to the depot. (Note that you can also explicitly change the filetype of opened files before submitting them.)


Don't sync target files to head revision
Normally, Perforce automatically syncs to the head revision before integrating. If you select this option, Perforce uses the revision that you have in your workspace, instead of the head revision. Note that this is only true if your server is 2002.1 or newer.

http://www.perforce.com/perforce/r11.1/manuals/p4web/help/integrate.html

Tuesday, July 23, 2013

Running a Java app as a daemon on Linux

There are many ways to run a Java app on a Linux device. The problem is that normally when you log out, this app will stop executing. This is a problem when you want the execution to out-live the session.


A command that can solve this issue is nohup. This command will ensure that the execution does not stop when you logout. Also it will not block the terminal.

This is used like this:

nohup java -jar MyJar.jar &

Note :

What will not work is adding an ampersand behind the command. This would be like this:

java -jar MyJar.jar &

The reason for this is that even though this would not block the terminal, it is still tied to the current session. So once the session ends, it will stop executing.