On Mon, Mar 24, 2014 at 10:21 PM, D Tim Cummings <[email protected]>wrote:

> Thanks. I didn't think of that.
>
> So now I am putting the migrations into my app.
>
> 1. I have worked out I need to change the Cayenne Model schema update
> strategy in cayenne-project.xml from
>
>
> schema-update-strategy="org.apache.cayenne.access.dbsync.CreateIfNoSchemaStrategy"
>
> to
>
>
> schema-update-strategy="org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy"
>

Yep


>
> 2. I added the generated Datamap0.java into my project.
>
> 3. I added a method in my AppModule of my Tapestry project to run at
> startup to migrate to latest. Is this the correct way of getting domain?
>
>   @Startup
>   public static void initMigrateToLatest() {
>     try {
>       ServerRuntime sr = new ServerRuntime("cayenne-project.xml");
>       DataDomain domain = sr.getDataDomain();
>       new Migrator(domain.getDataNode("datanode"),
> Datamap0.class.getPackage().getName()).migrateToLatest();
>     } catch (SQLException e) {
>       throw new RuntimeException("Unable to migrate database to current
> version: " + e.getMessage(), e);
>     }
>   }
>

If you are using CayenneFilter or similar you can just do this:
ServerRuntime localRuntime = (ServerRuntime) WebUtil.getCayenneRuntime(
servletContext);


>
> 4. Now I am having a problem with auto_increment fields. For example I
> have a table called tbl_company. If it is created using Cayenne's
> CreateIfNoSchemaStrategy then it looks like this
>
> mysql> describe tbl_company;
> +-------------+--------------+------+-----+---------+----------------+
> | Field       | Type         | Null | Key | Default | Extra          |
> +-------------+--------------+------+-----+---------+----------------+
> | date_cancel | datetime     | YES  |     | NULL    |                |
> | date_create | datetime     | YES  |     | NULL    |                |
> | id_company  | int(11)      | NO   | PRI | NULL    | auto_increment |
> | str_company | varchar(255) | YES  |     | NULL    |                |
> +-------------+--------------+------+-----+---------+----------------+
>
>
> If I create it using cayenne-migrations and Datamap0 it looks like this.
> (Note no auto_increment on id).
>
> mysql> describe tbl_company;
> +-------------+--------------+------+-----+---------+-------+
> | Field       | Type         | Null | Key | Default | Extra |
> +-------------+--------------+------+-----+---------+-------+
> | date_cancel | datetime     | YES  |     | NULL    |       |
> | date_create | datetime     | YES  |     | NULL    |       |
> | id_company  | int(11)      | NO   | PRI | NULL    |       |
> | str_company | varchar(255) | YES  |     | NULL    |       |
> +-------------+--------------+------+-----+---------+-------+
>
>
> My Datamap0 for this table is
>
> MigrationTableNew tbl_company = db.createTable("tbl_company");
> tbl_company.addTimestampColumn("date_cancel", 19);
> tbl_company.addTimestampColumn("date_create", 19);
> tbl_company.addIntegerColumn("id_company", MANDATORY, null);
> tbl_company.addVarcharColumn("str_company", 255);
> tbl_company.addPrimaryKey("id_company");
>
>
> My datamap.map.xml for this table is (Note the isGenerated="true")
>
> <db-entity name="tbl_company">
> <db-attribute name="date_cancel" type="TIMESTAMP" length="19"/>
> <db-attribute name="date_create" type="TIMESTAMP" length="19"/>
>
> <db-attribute name="id_company" type="INTEGER" isPrimaryKey="true" 
> isGenerated="true" isMandatory="true" length="10"/>
> <db-attribute name="str_company" type="VARCHAR" length="255"/>
> </db-entity>
>
> So my question is, how do I get cayenne-migrations to create the
> auto_increment in MySQL.
>

I'll have to look into that.  You can do it manually by extending
SqlFileMigration and adding an sql file with the same name as your
migration class (YourClass.sql) with this content:

alter table tbl_company modify column id_company integer not nullauto_increment;


>
> Thanks
>
> Tim
>
> On 25 Mar 2014, at 0:11, John Huss <[email protected]> wrote:
>
> I usually run MigrationGenerator from eclipse.  You can just create a new
> "Java Application" launch configuration for the project and enter
> "org.apache.cayenne.migration.MigrationGenerator" as the main class.  And
> then on the arguments tab enter your cayenne project filename and the
> output directory.  For example "cayenne-MyDomain.xml ."  (dot for the
> current directory).
>
>
>

Reply via email to