Call EntityMigration SchemaMigration because that's what it's for and I want to

add a new DataMigration mechanism so that people can stop using schema
migrations to do data migration.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2448 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2008-09-25 22:27:58 +00:00
parent 0bf5b3fbd9
commit 296495a88f
4 changed files with 22 additions and 23 deletions
@@ -514,9 +514,9 @@ public class DepotMarshaller<T extends PersistentRecord>
* This is called by the persistence context to register a migration for the entity managed by * This is called by the persistence context to register a migration for the entity managed by
* this marshaller. * this marshaller.
*/ */
protected void registerMigration (EntityMigration migration) protected void registerMigration (SchemaMigration migration)
{ {
_migrations.add(migration); _schemaMigs.add(migration);
} }
/** /**
@@ -727,9 +727,9 @@ public class DepotMarshaller<T extends PersistentRecord>
log.info("Migrating " + getTableName() + " from " + currentVersion + " to " + log.info("Migrating " + getTableName() + " from " + currentVersion + " to " +
_schemaVersion + "..."); _schemaVersion + "...");
if (_migrations.size() > 0) { if (_schemaMigs.size() > 0) {
// run our pre-default-migrations // run our pre-default-migrations
for (EntityMigration migration : _migrations) { for (SchemaMigration migration : _schemaMigs) {
if (migration.runBeforeDefault() && if (migration.runBeforeDefault() &&
migration.shouldRunMigration(currentVersion, _schemaVersion)) { migration.shouldRunMigration(currentVersion, _schemaVersion)) {
migration.init(getTableName(), _fields); migration.init(getTableName(), _fields);
@@ -887,7 +887,7 @@ public class DepotMarshaller<T extends PersistentRecord>
}); });
} }
// we do not auto-remove columns but rather require that EntityMigration.Drop records be // we do not auto-remove columns but rather require that SchemaMigration.Drop records be
// registered by hand to avoid accidentally causing the loss of data // registered by hand to avoid accidentally causing the loss of data
// we don't auto-remove indices either because we'd have to sort out the potentially // we don't auto-remove indices either because we'd have to sort out the potentially
@@ -895,7 +895,7 @@ public class DepotMarshaller<T extends PersistentRecord>
// index was hand defined in a @Column clause) // index was hand defined in a @Column clause)
// run our post-default-migrations // run our post-default-migrations
for (EntityMigration migration : _migrations) { for (SchemaMigration migration : _schemaMigs) {
if (!migration.runBeforeDefault() && if (!migration.runBeforeDefault() &&
migration.shouldRunMigration(currentVersion, _schemaVersion)) { migration.shouldRunMigration(currentVersion, _schemaVersion)) {
migration.init(getTableName(), _fields); migration.init(getTableName(), _fields);
@@ -1107,8 +1107,8 @@ public class DepotMarshaller<T extends PersistentRecord>
/** Indicates that we have been initialized (created or migrated our tables). */ /** Indicates that we have been initialized (created or migrated our tables). */
protected boolean _initialized; protected boolean _initialized;
/** A list of hand registered entity migrations to run prior to doing the default migration. */ /** A list of hand registered schema migrations to run prior to doing the default migration. */
protected ArrayList<EntityMigration> _migrations = new ArrayList<EntityMigration>(); protected ArrayList<SchemaMigration> _schemaMigs = new ArrayList<SchemaMigration>();
/** The name of the table we use to track schema versions. */ /** The name of the table we use to track schema versions. */
protected static final String SCHEMA_VERSION_TABLE = "DepotSchemaVersion"; protected static final String SCHEMA_VERSION_TABLE = "DepotSchemaVersion";
@@ -189,19 +189,18 @@ public class PersistenceContext
} }
/** /**
* Registers a migration for the specified entity class. * Registers a schema migration for the specified entity class.
* *
* <p> This method must be called <b>before</b> an Entity is used by any repository. Thus you * <p> This method must be called <b>before</b> an Entity is used by any repository. Thus you
* should register all migrations immediately after creating your persistence context or if you * should register all migrations in the constructor of the repository that declares them in
* are careful to ensure that your Entities are only used by a single repository, you can * its {@link DepotRepository#getManagedRecords} method.
* register your migrations in the constructor for that repository.
* *
* <p> Note that the migration process is as follows: * <p> Note that the migration process is as follows:
* *
* <ul><li> Note the difference between the entity's declared version and the version recorded * <ul><li> Note the difference between the entity's declared version and the version recorded
* in the database. * in the database.
* <li> Run all registered pre-migrations * <li> Run all registered pre-migrations
* <li> Perform all default migrations (column additions and removals) * <li> Perform all default migrations (column and index additions, index removals)
* <li> Run all registered post-migrations </ul> * <li> Run all registered post-migrations </ul>
* *
* Thus you must either be prepared for the entity to be at <b>any</b> version prior to your * Thus you must either be prepared for the entity to be at <b>any</b> version prior to your
@@ -215,7 +214,7 @@ public class PersistenceContext
* guaranteed to be run in registration order and with predictable pre- and post-conditions. * guaranteed to be run in registration order and with predictable pre- and post-conditions.
*/ */
public <T extends PersistentRecord> void registerMigration ( public <T extends PersistentRecord> void registerMigration (
Class<T> type, EntityMigration migration) Class<T> type, SchemaMigration migration)
{ {
getRawMarshaller(type).registerMigration(migration); getRawMarshaller(type).registerMigration(migration);
} }
@@ -35,12 +35,12 @@ import static com.samskivert.jdbc.depot.Log.log;
* migrations. See {@link PersistenceContext#registerPreMigration} and {@link * migrations. See {@link PersistenceContext#registerPreMigration} and {@link
* PersistenceContext#registerPostMigration}. * PersistenceContext#registerPostMigration}.
*/ */
public abstract class EntityMigration extends Modifier public abstract class SchemaMigration extends Modifier
{ {
/** /**
* A convenient migration for dropping a column from an entity. * A convenient migration for dropping a column from an entity.
*/ */
public static class Drop extends EntityMigration public static class Drop extends SchemaMigration
{ {
public Drop (int targetVersion, String columnName) { public Drop (int targetVersion, String columnName) {
super(targetVersion); super(targetVersion);
@@ -64,7 +64,7 @@ public abstract class EntityMigration extends Modifier
/** /**
* A convenient migration for renaming a column in an entity. * A convenient migration for renaming a column in an entity.
*/ */
public static class Rename extends EntityMigration public static class Rename extends SchemaMigration
{ {
public Rename (int targetVersion, String oldColumnName, String newColumnName) { public Rename (int targetVersion, String oldColumnName, String newColumnName) {
super(targetVersion); super(targetVersion);
@@ -121,7 +121,7 @@ public abstract class EntityMigration extends Modifier
* instantiated with the name of a persistent field, not the name of a database column. These * instantiated with the name of a persistent field, not the name of a database column. These
* can be very different things for classes that use @Column annotations. * can be very different things for classes that use @Column annotations.
*/ */
public static class Retype extends EntityMigration public static class Retype extends SchemaMigration
{ {
public Retype (int targetVersion, String fieldName) { public Retype (int targetVersion, String fieldName) {
super(targetVersion); super(targetVersion);
@@ -158,7 +158,7 @@ public abstract class EntityMigration extends Modifier
* *
* @see Column#defaultValue * @see Column#defaultValue
*/ */
public static class Add extends EntityMigration public static class Add extends SchemaMigration
{ {
public Add (int targetVersion, String fieldName, String defaultValue) { public Add (int targetVersion, String fieldName, String defaultValue) {
super(targetVersion); super(targetVersion);
@@ -215,7 +215,7 @@ public abstract class EntityMigration extends Modifier
return (currentVersion < _targetVersion); return (currentVersion < _targetVersion);
} }
protected EntityMigration (int targetVersion) protected SchemaMigration (int targetVersion)
{ {
super(); super();
_targetVersion = targetVersion; _targetVersion = targetVersion;
@@ -27,7 +27,7 @@ import java.util.Set;
import com.samskivert.jdbc.StaticConnectionProvider; import com.samskivert.jdbc.StaticConnectionProvider;
import com.samskivert.util.RandomUtil; import com.samskivert.util.RandomUtil;
import com.samskivert.jdbc.depot.DepotRepository; import com.samskivert.jdbc.depot.DepotRepository;
import com.samskivert.jdbc.depot.EntityMigration; import com.samskivert.jdbc.depot.SchemaMigration;
import com.samskivert.jdbc.depot.PersistenceContext; import com.samskivert.jdbc.depot.PersistenceContext;
import com.samskivert.jdbc.depot.PersistentRecord; import com.samskivert.jdbc.depot.PersistentRecord;
import com.samskivert.jdbc.depot.expression.LiteralExp; import com.samskivert.jdbc.depot.expression.LiteralExp;
@@ -46,11 +46,11 @@ public class TestRepository extends DepotRepository
perCtx.init("test", new StaticConnectionProvider("depot.properties"), null); perCtx.init("test", new StaticConnectionProvider("depot.properties"), null);
// tests a bogus rename migration // tests a bogus rename migration
// perCtx.registerMigration(TestRecord.class, new EntityMigration.Rename(1, "foo", "bar")); // perCtx.registerMigration(TestRecord.class, new SchemaMigration.Rename(1, "foo", "bar"));
// tests a custom add column migration // tests a custom add column migration
perCtx.registerMigration(TestRecord.class, perCtx.registerMigration(TestRecord.class,
new EntityMigration.Add(2, TestRecord.HOME_TOWN, "'Anytown USA'")); new SchemaMigration.Add(2, TestRecord.HOME_TOWN, "'Anytown USA'"));
TestRepository repo = new TestRepository(perCtx); TestRepository repo = new TestRepository(perCtx);