»
S
I
D
E
B
A
R
«
Evejob Development Environment Installation Part #8: Configuring applications to work with MySQL
Nov 2nd, 2009 by evereq

Django

We will need to use MySQL for Python project for connection from Django Python code to MySQL database. Because I (and hope you also) use Windows 7 64 bit with Python 64 bit, needs to found and install 64 bit version of such library. On original project site, for some reason I could not found (at least in the moment when I write post), so I get it from different site:  http://www.codegood.com/download/3/. Just download and install it (it will automatically found your Python installation if you follow all steps that I write in previous posts).

Let’s configure now, EvejobDJ project to use MySQL database.

First, needs to create “EvejobDJ” database schema using one of the administration tools, for example, described in Part #7 – just create it visually or issue following SQL command:

CREATE DATABASE EvejobDJ;

Open “settings.py” file in root folder of EvejobDJ project inside IntelliJ IDEA IDE and change database engine parameters to following:

DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'evejobdj'         # Or path to database file if using sqlite3.
DATABASE_USER = 'root'                # Not used with sqlite3.
DATABASE_PASSWORD = ''             # Not used with sqlite3.
DATABASE_HOST = ''                     # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''                     # Set to empty string for default. Not used with sqlite3.

Now it is time to test connection and create default tables in database (for authentication, sessions etc):

cd C:\Evejob\EvejobDJ
python manage.py syncdb

Hopefully you will get something like this in response:

EvejobDJDatabaseCreate

As you see you will need to create default “superuser” (I just put “admin” as his username and “info at evejob.com” as his email) because Django authentication app enabled by default.

Now you can check in MySQL database administration tool that you really create tables (screenshot from free MySQL Administrator that you probably install with MySQL GUI Tools):

MySQLEvejobDJDatabaseJustCreated

Ruby On Rails

To use MySQL in Ruby On Rails (RoR) we need to install MySQL Adapter, for example:

ruby -S gem install mysql

But here we get into trouble – binaries of this gem (mysql) does not work correctly on Windows 64 bit and on project site there is no version for 64 bit OS…
While on some “Unix” OS it is possible to fix this issue (for example build gem from source…), on Windows it is simply not possible…
Ah, OK, OK… it is possible, but just too complex / time consuming – don’t want to spend time on this NOW and leave this time to focus on coding of Evejob… maybe somebody or author of gem will fix this issue later!

Note: other available adapters for MySQL from Ruby seems also have same problems… at least by now.

So let’s just temporary switch to PostgreSQL for Ruby on Rails version of Evejob.

To setup PostgreSQL for EvejobRoR development it takes just few minutes:

  • Download and install latest PostgreSQL from http://www.enterprisedb.com/products/pgdownload.do#windows. Installation is trivial same like for MySQL (sure same – configuration is NOT trivial for production usage!). Make sure that you remember password that you put for “postgres” login role (default database user name).
  • Create “EvejobRoR_development” database in PgAdmin (this app comes with PostgreSQL… very similar to MySQL Administrator) and select “postgres” role as owner for this database.
  • Open EvejobRoR project in IntelliJ IDEA and copy file “config\database.yml” to “config\database_mysql.yml” (we will need it again when fix issue with MySQL + Ruby)
  • Edit file “config\database.yml” and enter here following information (make sure you put your password instead of my fake “admin”):
    development:
      adapter: postgresql
      encoding: utf8
      reconnect: false
      database: EvejobRoR_development
      pool: 5
      username: postgres
      password: admin
      host: localhost
     
    test:
      adapter: postgresql
      encoding: utf8
      reconnect: false
      database: EvejobRoR_test
      pool: 5
      username: postgres
      password: admin
      host: localhost
     
    production:
      adapter: postgresql
      encoding: utf8
      reconnect: false
      database: EvejobRoR_production
      pool: 5
      username: postgres
      password:  admin
      host: localhost
  • Execute following commands:
    ruby -S gem install postgres-pr

    This will install PostgreSQL adapter for Ruby (gem “postgres-pr” works on Windows x64)

    Now you can try to execute database “migration” that will hopefully create some stuff in development database:

    cd c:\Evejob\EvejobRoR
    c:\ruby\bin\rake db:migrate

    It will be to simple if this will work for you from first attempt! On my machine, I was need to made small “hack” to made this work, i.e. to add following code into new_rails_defaults.rb file in “config\initializers” folder and run db:migrate again:

    def PGconn.quote_ident(name)
        %("#{name}")
    end

    (look for more info about this “hack” here)

If you complete everything, you can check that db:migrate actually create one table (”schema_migrations”) in your development database:

pgAdmin_Shema_migrations_table

Grails

Let’s configure last version of Evejob which going to use MySQL database – EvejobGoG.

First, needs to create “evejobGoG_development”, “evejobGoG_test” and “evejobGoG_production” empty databases. You can do this trivial task using MySQL Administrator GUI Tool for example.

Next step is to let know Grails how to connect to just created MySQL databases. Because Grails runs on Groovy and Groovy itself runs in JVM we can use MySQL Connector/J from MySQL site: http://dev.mysql.com/downloads/connector/j/

Download “zip” file, unpack it to temporary folder and copy mysql-connector-java-x.x.xx-bin.jar file to the C:\evejob\EvejobGoG\lib folder.

Now open EvejobGoG project in IntelliJ IDEA and found DataSource.groovy file located in “EvejobGoG\Grails-app\conf” folder. Let’s change some settings in this file:

dataSource {
	pooled = true
	driverClassName = "com.mysql.jdbc.Driver" //"org.hsqldb.jdbcDriver"
	username = "root"
	password = ""
    dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
}
hibernate {
    cache.use_second_level_cache=true
    cache.use_query_cache=true
    cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
// environment specific settings
environments {
	development {
		dataSource {
			dbCreate = "update" // one of 'create', 'create-drop','update'
			url = "jdbc:mysql://localhost/evejobGoG_development" // "jdbc:hsqldb:mem:devDB"
		}
	}
	test {
		dataSource {
			dbCreate = "update"
			url = "jdbc:mysql://localhost/evejobGoG_test" // "jdbc:hsqldb:mem:testDb"
		}
	}
	production {
		dataSource {
			dbCreate = "update"
			url = "jdbc:mysql://localhost/evejobGoG_production" // "jdbc:hsqldb:file:prodDb;shutdown=true"
		}
	}
}

Now Grails know how to connect to MySQL. Unfortunately, there is no build-in database migrations functionality in Grails framework yet, so our next step is to install some plug-ins that can help us to maintain database schema versions and migrations. While exists few of such plug-ins, I think most “featured” is LiquiBase (but also you can take a look into “Autobase” if you prefer to dial with DSL instead of XML… just take a note that this plugin works only with Grails, while LiquiBase can be used with Ant, Maven, Spring etc or even independently as database change management solution)

To install LiquiBase do following (you can read more regarding Grails integration with LiquidBase here):

cd C:\evejob\EvejobGoG
grails install-plugin liquibase

Now check that you have following folder (that is empty by now):

C:\evejob\EvejobGoG\grails-app\migrations

If you don’t have folder yet, just create it before continue with migrations – this folder will be used to save migrations change log etc..

Now it’s time to generate change log and migrate database (that is empty for now) to initial version:

grails generate-changelog grails-app\migrations\changelog.xml
grails migrate

After first command (”generate-changelog”) LiquiBase will create initial version of database change log and save it to the “C:\evejob\EvejobGoG\grails-app\migrations\changelog.xml” file. It will looks something like following:

Second command (”grails migrate”) will actually made migration in database. In our case because our domain is empty, LiquiBase will just create changes maintenance tables: “databasechangelog” and “databasechangeloglock”. You can check now this using MySQL Administrator:

LiquiBaseInitialTables

If all steps above completed successfully you finish setup EvejobGoG Grails project to use MySQL Database and have ability to migrate databases!

Evejob Development Environment Installation Part #4: Groovy, Grails + AppEngine Installation
Oct 18th, 2009 by evereq

Grails Installation

To install Grails follow steps:

1) Download and install JDK from http://java.sun.com/javase/downloads. Make sure that you use following version

Java SE Development Kit (JDK)

JDK 6 Update X

Note: You don’t need Bundles packages. Also make sure that you download Windows x64 version of JDK, like on screenshot bellow:

JDKDownload

Let’s assume that you download and install JDK to following default location: C:\Program Files\Java\jdk1.6.0_16

2) Download latest stable Grails Binary ZIP Release from http://www.grails.org/Download, for example: grails-bin-1.1.1.zip. We not going to install Grails from sources (build it) due to the fact that Grails support “grails update” command, which can update your Grails version to latest one.

3) Extract Grails archive to the following folder: C:\Grails

4) Create JAVA_HOME environment variable that points to the path where you have installed JDK and create a GRAILS_HOME environment variable that points to the C:\Grails folder:

JAVA_HOME_variable GRAILS_HOME_variable

Note, make sure you enter your system path to JDK and JDK version that you install before.

5) Append %GRAILS_HOME%\bin to the PATH variable:

GrailsPath

6) Check that Grails installed successfully - just type “Grails” in command prompt and check that you get a help message like:

GrailsWelcome

OK, installation of Grails finishes here, let’s move to the next task…

EvejobGoG and EvejobGoGG projects configuration

So let’s start to create EvejobGoG project that is build on  Grails and EvejobGoGG also build on Grails but specially adjusted to be hosted by Google App Engine.

First Start IntelliJ IDEA, open “Create New Project”, select “Grails Application” and enter project files location as “C:\evejob\EvejobGoG”:

EvejobGoGCreate

Press Next and on Grails SDK window, press “New” to add new one and in “Select Path” window select “C:\Grails” folder (where you install Grails before):

GrailsSDKSelectionInIDEA

You will get something like this (your version of Grails can be different):

GrailsSDKSelectionFinished

Press “Next” and on next window (Select the desired technologies) leave all options unselected. After you press “Finish” you will get new Grails project opened in IDEA (if during this phrase IDEA will ask you about some updates in Grails Tool Window, press “y” to do so):

GrailsEvejobGoGProjectInIDEA

Now you can repeat same steps to create EvejobGoGG project that will use Google App Engine.

But first let’s install Google App Engine SDK for Java.

Go to the http://code.google.com/appengine/downloads.html#Google_App_Engine_SDK_for_Java and download Zip file that contains SDK (usually file have name like “appengine-java-sdk-1.2.6.zip”). Than create folder C:\Google\google_appengine_java_sdk and unzip archive content to this folder. That’s it – Google App Engine Java SDK installed.

Let’s setup environment variable which is used by Grails plugin to locate App Engine Java SDK – APPENGINE_HOME (in our system we install it to C:\Google\google_appengine_java_sdk folder):

APPENGINE_HOME_variable

Let’s reload IDEA so environment changes takes effect (we will use APPENGINE_HOME variable in a second).

Now let’s continue with IDEA and setup EvejobGoGG project same way like we create and setup EvejobGoG (all step exactly same like above).

IntelliJ Idea provide great support of Grails development build-in. For example it is very simple to install Grails Plug-ins directly from IDE (make sure that you select EvejobGoGG project in IDEA window):

GrailsPluginsInIDEA

Ones you open Grails plugins window, probably you will need to press “Reload Grails Plugin” button, to get a full list of available plugins:

GrailsPluginsReload

Type “app” in search box and select “app-engine” plugin (”Grails AppEngine plugin”). Check check box “Enable” and press “Apply Changes”. Than select latest release version of the plugin, for example:

GrailsAppEnginePluginVersion

OK, let’s back to plugin installation – press “OK” on plugin installation window and in Grails window you will probably get question “Do you want to use JPA or JDO for persistence”. Let’s for now just answer “jpa” and finish plugin installation.

Finished… Ah, ups… Let’s just check that project structure is OK – go to “File / Project Structure” window:

ProjectStructureCheck

Sometimes (looks like due to some bug in IDEA currently???) you will not get Project SDK configured:

ProjectSDKNotConfigured

If you in this situation (hopefully not, but just in case), press “New” button,  select “JSDK” in drop down and browse to your JDK path:

JDKPath

After, press “Apply” and you will add JDK to EvejobGoGG project.

Do the same check (and probably fix) for EvejobGoG project.

»  Substance: WordPress   »  Style: Ahren Ahimsa
© Copyright 2008–2009 EvereQ.com All rights reserved.