Existing-Database.txt 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. = Working With Existing Databases =
  2. The following topics are targeted for developers who already have a working database solution in place, but would like to use Propel to work with the data. For this case, Propel provides a number of command-line utilities helping with migrations of data and data structures.
  3. == Working with Database Structures ==
  4. Propel uses an abstract XML schema file to represent databases (the [wiki:Documentation/1.5/Schema schema]). Propel builds the SQL specific to a database based on this schema. Propel also provides a way to reverse-engineer the generic schema file based on database metadata.
  5. === Creating an XML Schema from a DB Structure ===
  6. To generate a schema file, create a new directory for your project & specify the connection information in your `build.properties` file for that project. For example, to create a new project, `legacyapp`, follow these steps:
  7. 1. Create the `legacyapp` project directory anywhere on your filesystem:
  8. {{{
  9. > mkdir legacyapp
  10. > cd legacyapp
  11. }}}
  12. 1. Create a `build.properties` file in `legacyapp/` directory with the DB connection parameters for your existing database, e.g.:
  13. {{{
  14. propel.project = legacyapp
  15. # The Propel driver to use for generating SQL, etc.
  16. propel.database = mysql
  17. # This must be a PDO DSN
  18. propel.database.url = mysql:dbname=legacyapp
  19. propel.database.user = root
  20. # propel.database.password =
  21. }}}
  22. 1. Run the `reverse` task to generate the `schema.xml`:
  23. {{{
  24. > propel-gen reverse
  25. }}}
  26. 1. Pay attention to any errors/warnings issued by Phing during the task execution and then examine the generated `schema.xml` file to make any corrections needed.
  27. 1. '''You're done! ''' Now you have a `schema.xml` file in the `legacyapp/` project directory. You can now run the default Propel build to generate all the classes.
  28. The generated `schema.xml` file should be used as a guide, not a final answer. There are some datatypes that Propel may not be familiar with; also some datatypes are simply not supported by Propel (e.g. arrays in PostgreSQL). Unfamiliar datatypes will be reported as warnings and substituted with a default VARCHAR datatype.
  29. Tip: The reverse engineering classes may not be able to provide the same level of detail for all databases. In particular, metadata information for SQLite is often very basic since SQLite is a typeless database.
  30. === Migrating Structure to a New RDBMS ===
  31. Because Propel has both the ability to create XML schema files based on existing database structures and to create RDBMS-specific DDL SQL from the XML schema file, you can use Propel to convert one database into another.
  32. To do this you would simply:
  33. 1. Follow the steps above to create the `schema.xml` file from existing db.
  34. 1. Then you would change the target database type and specify connection URL for new database in the project's `build.properties` file:
  35. {{{
  36. propel.database = pgsql
  37. propel.database.url = pgsql://unix+localhost/newlegacyapp
  38. }}}
  39. 1. And then run the `sql` task to generate the new DDL:
  40. {{{
  41. > propel-gen sql
  42. }}}
  43. 1. And (optionally) the `insert-sql` task to create the new database:
  44. {{{
  45. > propel-gen insert-sql
  46. }}}
  47. == Working with Database Data ==
  48. Propel also provides several tasks to facilitate data import/export. The most important of these are `datadump` and `datasql`. The first dumps data to XML and the second converts the XML data dump to a ready-to-insert SQL file.
  49. Tip: Both of these tasks require that you already have generated the `schema.xml` for your database.
  50. === Dumping Data to XML ===
  51. Once you have created (or reverse-engineered) your `schema.xml` file, you can run the `datadump` task to dump data from the database into a `data.xml` file.
  52. {{{
  53. > propel-gen datadump
  54. }}}
  55. The task transfers database records to XML using a simple format, where each row is an element, and each column is an attribute. So for instance, the XML representation of a row in a `publisher` table:
  56. ||'''publisher_id'''||'''name'''||
  57. ||1||William Morrow||
  58. ... is rendered in the `data.xml` as follows:
  59. {{{
  60. <dataset name="all">
  61. ...
  62. <Publisher PublisherId="1" Name="William Morrow"/>
  63. ...
  64. </dataset>
  65. }}}
  66. === Creating SQL from XML ===
  67. To create the SQL files from the XML, run the `datasql` task:
  68. {{{
  69. > propel-gen datasql
  70. }}}
  71. The generated SQL is placed in the `build/sql/` directory and will be inserted when you run the `insert-sql` task.