Seeding your rails database

I’ve been working on a project that requires a good deal of test data to verify functionality. It involves train timetables with many lines, trains, stations etc. To help in uploading data for testing and development I’ve been using the rake db:seed command.

[code language="css"]
rake db:seed RAILS_ENV=development
[/code]

OR

[code language="css"]
rake db:seed RAILS_ENV=test
[/code]

For the record I’m using a postgresql database but the use of the seeding implementation is database neutral. It will use the contents of the config/database.yml file to connect to whatever target environment you specify.

One of the problems with loading seed data is when dependencies exist between tables. You need to be able to identify records from table A to be able to create appropriate joins in table B. From a testing perspective there’s fixtures and factories. All well and good and each serves its’ purpose adequately. I wanted to build up a file that can be used to populate a database from scratch with the ability to add new records as your table set grows. I was also able to use it to sanity check my data model and joins as the project continued.

Here’s how the seeding works.

The relevant file is the seeds.rb file in your db folder.

To create single records you won’t need to refer to later in the seeding process, use something like this.

[code language="css"]
#firstly delete any existing data
User.delete_all
#now build up an array
users = [
  {email:'super@test.com', password:'@dmin123', password_confirmation: '@dmin123', admin:true, confirmed_at: '01/01/2011'},
  {email:'user@test.com', password:'user123', password_confirmation: 'user123', confirmed_at: '01/01/2011' }
]

#now process the array using an iterator
users.each { |user| User.create user }
[/code]

To create rows to which you can refer to later on (such as when establishing a table join).

[code language="css"]
Line.delete_all

@frankston_direct_line=Line.create({name:'Frankston Direct'})
@frankston_loop_line=Line.create({name:'Frankston Loop'})
@sandringham_line=Line.create({name:'Sandringham'})
[/code]

You can now refer to id of these records using the syntax @sandringham_line.id

So now I create a few train stations…

[code language="css"]

Station.delete_all

@aircraft=Station.create(name:'Aircraft' ,latitude:-37.866689 ,longitude:144.760795)
@alamein=Station.create(name:'Alamein' ,latitude: -37.86862  ,longitude: 145.08002)
@altona=Station.create(name:'Altona' ,latitude:-37.867231 ,longitude: 144.829609)
@armadale=Station.create(name:'Armadale', latitude:-37.85544, longitude: 145.018802)

#...
#list cut short for brevity
[/code]

Now I create the association between the lines and stations

LineStation.delete_all

[code language="css"]
sandringham_line_stations = [
  { line_id: @sandringham_line.id, station_id: @parliament.id, ordinal:1, time:0},
  { line_id: @sandringham_line.id, station_id: @melbourne_central.id, ordinal:2, time:2},
  { line_id: @sandringham_line.id, station_id: @southern_cross.id, ordinal:3, time:3},
  { line_id: @sandringham_line.id, station_id: @flinders_arrival.id, ordinal:4, time:4},

  #...
  #list cut short for brevity
]

#now create all the LineStations iterating over the array
sandringham_line_stations.each { |linestation| LineStation.create linestation }
[/code]

So there you have it. The ability to apply full referential integrity at database seeding time using the power of db:seed.

Angular 2 Beta

We’re ecstatic to announce that we’ve reached Angular 2 Beta.  You can read about many of the improvements over Angular 1 in a recent post.  Get started learning Angular 2 now at angular.io.

What does ‘beta’ mean?

Beta means we’re now confident that most developers can be successful building large applications using Angular 2.

Through developer preview and alpha we’ve worked closely with several large projects here at Google including AdWords, GreenTea (Google’s internal CRM system), and Google Fiber.  In fact, just a few weeks ago we saw Google Fiber launch on their new Angular 2 code base.

Externally, we’ve worked closely with several other teams integrating with Angular 2 including Ionic Framework onIonic 2, Telerik on NativeScript, Rangle.io on Batarangle, and many others.

We’ve incorporated the majority of feedback from these teams that would create breaking changes.  Given this, we’re looking forward to other teams developing in earnest and telling us how we can help.

Getting Started

Get going now with the updated and expanded Quickstart and Tutorial on angular.io.  From there, you can check out several developer guides and a handy cheatsheet covering the main features in Angular 2.

While the many in-progress Angular 2 books and courses will likely take a few weeks to catch up to the latest changes, we can recommend taking a look at the clear explanations and examples on the thoughtram blog and this in depthprimer on Angular 2.

Upgrading from Angular 1

You may have an Angular 1 app today and you want to start writing Angular 2 code in that app, without changing your working/tested angular 1 code. This is where ngUpgrade comes in.

While you can upgrade apps in a “big bang” approach where you halt production until everything is rewritten, we’re supporting two paths for where teams want to upgrade their Angular 1 apps to Angular 2.

ngUpgrade

We know many of you made large investments in Angular 1 and have created some awesome apps. We created ngUpgrade for all of you to make it possible to leverage your existing apps and move forward with Angular 2.

ngUpgrade lets you mix Angular 2 into your existing Angular 1 application.  You’ll get to take advantage of Angular 2’s improved speed and APIs immediately as you replace components a bit at a time over the course of your releases.  Learn more in this article from thoughtram and the upgrade guide in our docs.

ngForward

Some teams, with apps that are more sensitive to download size, will want to avoid having both Angular 1 and Angular 2 libraries running in their app simultaneously.  For this, we have ngForward which lets you write Angular 1 applications in the syntax of Angular 2.  This lets your team get used Angular 2 conventions and styles in your apps today and shorten the distance to doing the full upgrade to Angular 2 when you’re ready.

Providing Feedback

As always, please submit issues via GitHub, questions on Stack Overflow, and join the live conversation on Gitter.

We’ve also recently added a mechanism for submitting feedback for all pages.  Just click on the exclamation point icon in the upper right of the page and tell us what could be improved.

What comes next?

We’re already hard at work on the set of improvements to move Angular 2 to its full and final release.  While we will make many small improvements, the big ones for final are:

  1. Reducing Angular 2’s payload size.
  2. Making the Angular CLI usable end to end throughout the development process.
  3. Creating a more developer-friendly route definition and link API for the Component Router.
  4. Support for animations.
  5. I18n and L10n support.

And there are even more cool things to come — some we’ve already started on including:

  1. More documentation, particularly around ES5/ES6 usage.
  2. Even better startup and runtime performance
  3. An architectural style guide
  4. Unit and end-to-end testing improvements
  5. More support for mobile web and installable mobile apps
  6. Material Design components for Angular 2
  7. A tools platform for deep IDE support
  8. Better support for ES6 and Babel

Thanks, and we’re looking forward to seeing the apps you build appear on madewithangular.com!

How does Kafka work in a nutshell?

Kafka is a distributed system consisting of servers and clients that communicate via a high-performance TCP network protocol. It can be deployed on bare-metal hardware, virtual machines, and containers in on-premise as well as cloud environments.

Servers: Kafka is run as a cluster of one or more servers that can span multiple datacenters or cloud regions. Some of these servers form the storage layer, called the brokers. Other servers run Kafka Connect to continuously import and export data as event streams to integrate Kafka with your existing systems such as relational databases as well as other Kafka clusters. To let you implement mission-critical use cases, a Kafka cluster is highly scalable and fault-tolerant: if any of its servers fails, the other servers will take over their work to ensure continuous operations without any data loss.

Clients: They allow you to write distributed applications and microservices that read, write, and process streams of events in parallel, at scale, and in a fault-tolerant manner even in the case of network problems or machine failures. Kafka ships with some such clients included, which are augmented by dozens of clients provided by the Kafka community: clients are available for Java and Scala including the higher-level Kafka Streams library, for Go, Python, C/C++, and many other programming languages as well as REST APIs.