Merge 'trunk' into 'dbicadmin_refactor'
Peter Rabbitson [Sat, 13 Feb 2010 08:41:10 +0000 (08:41 +0000)]
r8394@Thesaurus (orig r8381):  frew | 2010-01-19 17:34:10 +0100
add test to ensure no tabs in perl files

r8397@Thesaurus (orig r8384):  frew | 2010-01-19 18:00:12 +0100
fix test to be an author dep
r8398@Thesaurus (orig r8385):  ribasushi | 2010-01-19 18:19:40 +0100
First round of detabification
r8399@Thesaurus (orig r8386):  frew | 2010-01-19 23:42:50 +0100
Add EOL test

r8401@Thesaurus (orig r8388):  ribasushi | 2010-01-20 08:32:39 +0100
Fix minor RSC bug
r8402@Thesaurus (orig r8389):  roman | 2010-01-20 15:47:26 +0100
Added a FAQ entry titled: How do I override a run time method (e.g. a relationship accessor)?
r8403@Thesaurus (orig r8390):  roman | 2010-01-20 16:31:41 +0100
Added myself as a contributor.
r8408@Thesaurus (orig r8395):  jhannah | 2010-01-21 06:48:14 +0100
Added FAQ: Custom methods in Result classes

r8413@Thesaurus (orig r8400):  frew | 2010-01-22 04:17:20 +0100
add _is_numeric to ::Row
r8418@Thesaurus (orig r8405):  ribasushi | 2010-01-22 11:00:05 +0100
Generalize autoinc/count test
r8420@Thesaurus (orig r8407):  ribasushi | 2010-01-22 11:11:49 +0100
Final round of detabify
r8421@Thesaurus (orig r8408):  ribasushi | 2010-01-22 11:12:54 +0100
Temporarily disable whitespace checkers
r8426@Thesaurus (orig r8413):  ribasushi | 2010-01-22 11:35:15 +0100
Moev failing regression test away from trunk
r8431@Thesaurus (orig r8418):  frew | 2010-01-22 17:05:12 +0100
fix name of _is_numeric to _is_column_numeric

r8437@Thesaurus (orig r8424):  ribasushi | 2010-01-26 09:33:42 +0100
Switch to Test::Exception
r8438@Thesaurus (orig r8425):  ribasushi | 2010-01-26 09:48:30 +0100
Test txn_scope_guard regression
r8439@Thesaurus (orig r8426):  ribasushi | 2010-01-26 10:10:11 +0100
Fix txn_begin on external non-AC coderef regression
r8443@Thesaurus (orig r8430):  ribasushi | 2010-01-26 14:19:50 +0100
 r8304@Thesaurus (orig r8292):  nigel | 2010-01-13 16:05:48 +0100
 Branch to extend ::Schema::Versioned to handle series of upgrades
 r8320@Thesaurus (orig r8308):  nigel | 2010-01-14 16:52:50 +0100
 Changes to support multiple step schema version updates
 r8321@Thesaurus (orig r8309):  nigel | 2010-01-14 17:05:21 +0100
 Changelog for Changes to support multiple step schema version updates
 r8393@Thesaurus (orig r8380):  ribasushi | 2010-01-19 13:59:51 +0100
 Botched merge (tests still fail)
 r8395@Thesaurus (orig r8382):  ribasushi | 2010-01-19 17:37:07 +0100
 More cleanup
 r8396@Thesaurus (orig r8383):  ribasushi | 2010-01-19 17:48:09 +0100
 Fix last pieces of retardation and UNtodo the quick cycle
 r8442@Thesaurus (orig r8429):  ribasushi | 2010-01-26 14:18:53 +0100
 No need for 2 statements to get the version

r8445@Thesaurus (orig r8432):  ribasushi | 2010-01-26 14:22:16 +0100
 r8161@Thesaurus (orig r8149):  ovid | 2009-12-18 15:59:56 +0100
 Prefetch queries make inefficient SQL when combined with a pager.  This branch
 is to try to isolate some of the join conditions and figure out if we can fix
 this.

 r8166@Thesaurus (orig r8154):  ovid | 2009-12-18 18:17:55 +0100
 Refactor internals to expose some join logic. Awful method and args :(

 r8319@Thesaurus (orig r8307):  ovid | 2010-01-14 15:37:35 +0100
 Attempt to factor our alias handling has mostly failed.

 r8330@Thesaurus (orig r8318):  ribasushi | 2010-01-15 03:02:21 +0100
 Better refactor
 r8332@Thesaurus (orig r8320):  ribasushi | 2010-01-15 03:14:39 +0100
 Better varnames
 r8347@Thesaurus (orig r8335):  ribasushi | 2010-01-17 11:33:55 +0100
 More mangling
 r8348@Thesaurus (orig r8336):  ribasushi | 2010-01-17 13:44:00 +0100
 Getting warmer
 r8349@Thesaurus (orig r8337):  ribasushi | 2010-01-17 14:00:20 +0100
 That was tricky :)
 r8352@Thesaurus (orig r8340):  ribasushi | 2010-01-17 15:57:06 +0100
 Turned out to be much trickier
 r8354@Thesaurus (orig r8342):  ribasushi | 2010-01-17 16:29:20 +0100
 This is made out of awesome
 r8355@Thesaurus (orig r8343):  ribasushi | 2010-01-17 16:46:02 +0100
 Changes
 r8400@Thesaurus (orig r8387):  ribasushi | 2010-01-20 08:17:44 +0100
 Whoops - need to dsable quoting

r8459@Thesaurus (orig r8446):  ribasushi | 2010-01-27 11:56:15 +0100
Clean up some stuff
r8463@Thesaurus (orig r8450):  ribasushi | 2010-01-27 12:08:04 +0100
Merge some cleanups from the prefetch branch
r8466@Thesaurus (orig r8453):  ribasushi | 2010-01-27 12:33:33 +0100
DSNs can not be empty
r8471@Thesaurus (orig r8458):  frew | 2010-01-27 21:38:42 +0100
fix silly multipk bug
r8472@Thesaurus (orig r8459):  ribasushi | 2010-01-28 11:13:16 +0100
Consolidate insert_bulk guards (and make them show up correctly in the trace)
r8473@Thesaurus (orig r8460):  ribasushi | 2010-01-28 11:28:30 +0100
Fix bogus test DDL
r8480@Thesaurus (orig r8467):  ribasushi | 2010-01-28 22:11:59 +0100
 r8381@Thesaurus (orig r8368):  moses | 2010-01-18 16:41:38 +0100
 Test commit
 r8425@Thesaurus (orig r8412):  ribasushi | 2010-01-22 11:25:01 +0100
 Informix test + cleanups
 r8428@Thesaurus (orig r8415):  ribasushi | 2010-01-22 11:59:25 +0100
 Initial informix support

r8482@Thesaurus (orig r8469):  ribasushi | 2010-01-28 22:19:23 +0100
Informix changes
r8483@Thesaurus (orig r8470):  ribasushi | 2010-01-29 12:01:41 +0100
Require non-warning-spewing MooseX::Types
r8484@Thesaurus (orig r8471):  ribasushi | 2010-01-29 12:15:15 +0100
Enhance warning test a bit (seems to fail on 5.8)
r8485@Thesaurus (orig r8472):  ribasushi | 2010-01-29 13:00:54 +0100
Fugly 5.8 workaround
r8494@Thesaurus (orig r8481):  frew | 2010-01-31 06:47:42 +0100
cleanup (3 arg open, 1 grep instead of 3)
r8496@Thesaurus (orig r8483):  ribasushi | 2010-01-31 10:04:43 +0100
better skip message
r8510@Thesaurus (orig r8497):  caelum | 2010-02-01 12:07:13 +0100
throw exception on attempt to insert a blob with DBD::Oracle == 1.23
r8511@Thesaurus (orig r8498):  caelum | 2010-02-01 12:12:48 +0100
add RT link for Oracle blob bug in DBD::Oracle == 1.23
r8527@Thesaurus (orig r8514):  caelum | 2010-02-02 23:20:17 +0100
 r22968@hlagh (orig r8502):  caelum | 2010-02-02 05:30:47 -0500
 branch to support Sybase SQL Anywhere
 r22971@hlagh (orig r8505):  caelum | 2010-02-02 07:21:13 -0500
 ASA last_insert_id and limit support, still needs BLOB support
 r22972@hlagh (orig r8506):  caelum | 2010-02-02 08:33:57 -0500
 deref table name if needed, check all columns for identity column not just PK
 r22973@hlagh (orig r8507):  caelum | 2010-02-02 08:48:11 -0500
 test blobs, they work, didn't have to do anything
 r22974@hlagh (orig r8508):  caelum | 2010-02-02 09:15:44 -0500
 fix stupid identity bug, test empty insert (works), test DTs (not working yet)
 r22976@hlagh (orig r8510):  caelum | 2010-02-02 14:31:00 -0500
 rename ::Sybase::ASA to ::SQLAnywhere, per mst
 r22978@hlagh (orig r8512):  caelum | 2010-02-02 17:02:29 -0500
 DT inflation now works
 r22979@hlagh (orig r8513):  caelum | 2010-02-02 17:18:06 -0500
 minor POD update

r8528@Thesaurus (orig r8515):  caelum | 2010-02-02 23:23:26 +0100
 r22895@hlagh (orig r8473):  caelum | 2010-01-30 03:57:26 -0500
 branch to fix computed columns in Sybase ASE
 r22911@hlagh (orig r8489):  caelum | 2010-01-31 07:18:33 -0500
 empty insert into a Sybase table with computed columns and either data_type => undef or default_value => SCALARREF works now
 r22912@hlagh (orig r8490):  caelum | 2010-01-31 07:39:32 -0500
 add POD about computed columns and timestamps for Sybase
 r22918@hlagh (orig r8496):  caelum | 2010-02-01 05:09:07 -0500
 update POD about Schema::Loader for Sybase

r8531@Thesaurus (orig r8518):  ribasushi | 2010-02-02 23:57:27 +0100
 r8512@Thesaurus (orig r8499):  boghead | 2010-02-01 23:38:13 +0100
 - Creating a branch for adding _post_inflate_datetime and _pre_deflate_datetime to
   InflateColumn::DateTime

 r8513@Thesaurus (orig r8500):  boghead | 2010-02-01 23:42:14 +0100
 - Add _post_inflate_datetime and _pre_deflate_datetime to InflateColumn::DateTime to allow
   for modifying DateTime objects after inflation or before deflation.

 r8524@Thesaurus (orig r8511):  boghead | 2010-02-02 22:59:28 +0100
 - Simplify by allowing moving column_info depreciated {extra}{timezone} data to
   {timezone} (and the same with locale)

r8533@Thesaurus (orig r8520):  caelum | 2010-02-03 05:19:59 +0100
support for Sybase SQL Anywhere through ODBC
r8536@Thesaurus (orig r8523):  ribasushi | 2010-02-03 08:27:54 +0100
Changes
r8537@Thesaurus (orig r8524):  ribasushi | 2010-02-03 08:31:20 +0100
Quote fail
r8538@Thesaurus (orig r8525):  caelum | 2010-02-03 13:21:37 +0100
test DT inflation for Sybase SQL Anywhere over ODBC too
r8539@Thesaurus (orig r8526):  caelum | 2010-02-03 17:36:39 +0100
minor code cleanup for SQL Anywhere last_insert_id
r8540@Thesaurus (orig r8527):  ribasushi | 2010-02-04 11:28:33 +0100
Fix bug reported by tommyt
r8548@Thesaurus (orig r8535):  ribasushi | 2010-02-04 14:34:45 +0100
Prepare for new SQLA release
r8560@Thesaurus (orig r8547):  ribasushi | 2010-02-05 08:59:04 +0100
Refactor some evil code
r8565@Thesaurus (orig r8552):  ribasushi | 2010-02-05 17:00:12 +0100
Looks like RSC is finally (halfway) fixed
r8566@Thesaurus (orig r8553):  ribasushi | 2010-02-05 17:07:13 +0100
RSC subquery can not include the prefetch
r8567@Thesaurus (orig r8554):  ribasushi | 2010-02-05 17:10:29 +0100
Fix typo and borked test
r8569@Thesaurus (orig r8556):  ribasushi | 2010-02-05 17:33:12 +0100
Release 0.08116
r8571@Thesaurus (orig r8558):  ribasushi | 2010-02-05 18:01:33 +0100
No idea how I missed all these fails...
r8572@Thesaurus (orig r8559):  ribasushi | 2010-02-05 18:13:34 +0100
Release 0.08117
r8574@Thesaurus (orig r8561):  ribasushi | 2010-02-05 18:51:12 +0100
Try to distinguish trunk from official versions
r8580@Thesaurus (orig r8567):  gshank | 2010-02-05 22:29:24 +0100
add doc on 'where' attribute

r8587@Thesaurus (orig r8574):  frew | 2010-02-07 21:07:03 +0100
add as_subselect_rs
r8588@Thesaurus (orig r8575):  frew | 2010-02-07 21:13:04 +0100
fix longstanding unmentioned bug ("me")
r8589@Thesaurus (orig r8576):  frew | 2010-02-08 06:17:43 +0100
another example of as_subselect_rs
r8590@Thesaurus (orig r8577):  frew | 2010-02-08 06:23:58 +0100
fix bug in UTF8Columns
r8591@Thesaurus (orig r8578):  ribasushi | 2010-02-08 09:31:01 +0100
Extend utf8columns test to trap fixed bug
r8592@Thesaurus (orig r8579):  ribasushi | 2010-02-08 12:03:23 +0100
Cleanup rel accessor type handling
r8593@Thesaurus (orig r8580):  ribasushi | 2010-02-08 12:20:47 +0100
Fix some fallout
r8595@Thesaurus (orig r8582):  ribasushi | 2010-02-08 12:38:19 +0100
Merge some obsolete code cleanup from the prefetch branch
r8596@Thesaurus (orig r8583):  ribasushi | 2010-02-08 12:42:09 +0100
Merge fix of RT54039 from prefetch branch
r8598@Thesaurus (orig r8585):  ribasushi | 2010-02-08 12:48:31 +0100
Release 0.08118
r8600@Thesaurus (orig r8587):  ribasushi | 2010-02-08 12:52:33 +0100
Bump trunk version
r8606@Thesaurus (orig r8593):  ribasushi | 2010-02-08 16:16:44 +0100
cheaper lookup
r8609@Thesaurus (orig r8596):  ribasushi | 2010-02-10 12:40:37 +0100
Consolidate last_insert_id handling with a fallback-attempt on DBI::last_insert_id
r8614@Thesaurus (orig r8601):  caelum | 2010-02-10 21:29:51 +0100
workaround for Moose bug affecting Replicated storage
r8615@Thesaurus (orig r8602):  caelum | 2010-02-10 21:40:07 +0100
revert Moose bug workaround, bump Moose dep for Replicated to 0.98
r8616@Thesaurus (orig r8603):  caelum | 2010-02-10 22:48:34 +0100
add a couple proxy methods to Replicated so it can run
r8628@Thesaurus (orig r8615):  caelum | 2010-02-11 11:35:01 +0100
 r21090@hlagh (orig r7836):  caelum | 2009-11-02 06:40:52 -0500
 new branch to fix unhandled methods in Storage::DBI::Replicated
 r21091@hlagh (orig r7837):  caelum | 2009-11-02 06:42:00 -0500
 add test to display unhandled methods
 r21092@hlagh (orig r7838):  caelum | 2009-11-02 06:55:34 -0500
 minor fix to last committed test
 r21093@hlagh (orig r7839):  caelum | 2009-11-02 09:26:00 -0500
 minor test code cleanup
 r23125@hlagh (orig r8607):  caelum | 2010-02-10 19:25:51 -0500
 add unimplemented Storage::DBI methods to ::DBI::Replicated
 r23130@hlagh (orig r8612):  ribasushi | 2010-02-11 05:12:48 -0500
 Podtesting exclusion

r8630@Thesaurus (orig r8617):  frew | 2010-02-11 11:45:54 +0100
Changes (from a while ago)
r8631@Thesaurus (orig r8618):  caelum | 2010-02-11 11:46:58 +0100
savepoints for SQLAnywhere
r8640@Thesaurus (orig r8627):  ribasushi | 2010-02-11 12:33:19 +0100
 r8424@Thesaurus (orig r8411):  ribasushi | 2010-01-22 11:19:40 +0100
 Chaining POC test

r8641@Thesaurus (orig r8628):  ribasushi | 2010-02-11 12:34:19 +0100
 r8426@Thesaurus (orig r8413):  ribasushi | 2010-01-22 11:35:15 +0100
 Moev failing regression test away from trunk

r8642@Thesaurus (orig r8629):  ribasushi | 2010-02-11 12:34:56 +0100

r8643@Thesaurus (orig r8630):  ribasushi | 2010-02-11 12:35:03 +0100
 r8507@Thesaurus (orig r8494):  frew | 2010-02-01 04:33:08 +0100
 small refactor to put select/as/+select/+as etc merging in it's own function

r8644@Thesaurus (orig r8631):  ribasushi | 2010-02-11 12:35:11 +0100
 r8514@Thesaurus (orig r8501):  frew | 2010-02-02 05:12:29 +0100
 revert actual changes from yesterday as per ribasushis advice

r8645@Thesaurus (orig r8632):  ribasushi | 2010-02-11 12:35:16 +0100
 r8522@Thesaurus (orig r8509):  frew | 2010-02-02 19:39:33 +0100
 delete +stuff if stuff exists

r8646@Thesaurus (orig r8633):  ribasushi | 2010-02-11 12:35:23 +0100
 r8534@Thesaurus (orig r8521):  frew | 2010-02-03 06:14:44 +0100
 change deletion/overriding to fix t/76

r8647@Thesaurus (orig r8634):  ribasushi | 2010-02-11 12:35:30 +0100
 r8535@Thesaurus (orig r8522):  frew | 2010-02-03 06:57:15 +0100
 some basic readability factorings (aka, fewer nested ternaries and long maps)

r8648@Thesaurus (orig r8635):  ribasushi | 2010-02-11 12:36:01 +0100
 r8558@Thesaurus (orig r8545):  frew | 2010-02-04 20:32:54 +0100
 fix incorrect test in t/76select.t and posit an incorrect solution

r8649@Thesaurus (orig r8636):  ribasushi | 2010-02-11 12:38:47 +0100

r8650@Thesaurus (orig r8637):  ribasushi | 2010-02-11 12:38:57 +0100
 r8578@Thesaurus (orig r8565):  ribasushi | 2010-02-05 19:11:09 +0100
 Should not be needed

r8651@Thesaurus (orig r8638):  ribasushi | 2010-02-11 12:39:03 +0100
 r8579@Thesaurus (orig r8566):  ribasushi | 2010-02-05 19:13:24 +0100
 SQLA now fixed

r8652@Thesaurus (orig r8639):  ribasushi | 2010-02-11 12:39:10 +0100
 r8624@Thesaurus (orig r8611):  ribasushi | 2010-02-11 10:31:08 +0100
 MOAR testing

r8653@Thesaurus (orig r8640):  ribasushi | 2010-02-11 12:39:17 +0100
 r8626@Thesaurus (orig r8613):  frew | 2010-02-11 11:16:30 +0100
 fix bad test

r8654@Thesaurus (orig r8641):  ribasushi | 2010-02-11 12:39:23 +0100
 r8627@Thesaurus (orig r8614):  frew | 2010-02-11 11:21:52 +0100
 fix t/76, break rsc tests

r8655@Thesaurus (orig r8642):  ribasushi | 2010-02-11 12:39:30 +0100
 r8632@Thesaurus (orig r8619):  frew | 2010-02-11 11:53:50 +0100
 fix incorrect test

r8656@Thesaurus (orig r8643):  ribasushi | 2010-02-11 12:39:35 +0100
 r8633@Thesaurus (orig r8620):  frew | 2010-02-11 11:54:49 +0100
 make t/76s and t/88 pass by deleting from the correct attr hash

r8657@Thesaurus (orig r8644):  ribasushi | 2010-02-11 12:39:40 +0100
 r8634@Thesaurus (orig r8621):  frew | 2010-02-11 11:55:41 +0100
 fix a test due to ordering issues

r8658@Thesaurus (orig r8645):  ribasushi | 2010-02-11 12:39:45 +0100
 r8635@Thesaurus (orig r8622):  frew | 2010-02-11 11:58:23 +0100
 this is why you run tests before you commit them.

r8659@Thesaurus (orig r8646):  ribasushi | 2010-02-11 12:39:51 +0100
 r8636@Thesaurus (orig r8623):  frew | 2010-02-11 12:00:59 +0100
 fix another ordering issue

r8660@Thesaurus (orig r8647):  ribasushi | 2010-02-11 12:39:57 +0100
 r8637@Thesaurus (orig r8624):  frew | 2010-02-11 12:11:31 +0100
 fix for search/select_chains

r8661@Thesaurus (orig r8648):  ribasushi | 2010-02-11 12:40:03 +0100

r8662@Thesaurus (orig r8649):  caelum | 2010-02-11 12:40:07 +0100
test nanosecond precision for SQLAnywhere
r8663@Thesaurus (orig r8650):  ribasushi | 2010-02-11 12:40:09 +0100
 r8639@Thesaurus (orig r8626):  ribasushi | 2010-02-11 12:33:03 +0100
 Changes and small ommission

r8666@Thesaurus (orig r8653):  ribasushi | 2010-02-11 18:16:45 +0100
Changes
r8674@Thesaurus (orig r8661):  ribasushi | 2010-02-12 09:12:45 +0100
Fix moose dep
r8680@Thesaurus (orig r8667):  dew | 2010-02-12 18:05:11 +0100
Add is_ordered to DBIC::ResultSet
r8688@Thesaurus (orig r8675):  ribasushi | 2010-02-13 09:36:29 +0100
 r8667@Thesaurus (orig r8654):  ribasushi | 2010-02-11 18:17:35 +0100
 Try a dep-handling idea
 r8675@Thesaurus (orig r8662):  ribasushi | 2010-02-12 12:46:11 +0100
 Move optional deps out of the Makefile
 r8676@Thesaurus (orig r8663):  ribasushi | 2010-02-12 13:40:53 +0100
 Support methods to verify group dependencies
 r8677@Thesaurus (orig r8664):  ribasushi | 2010-02-12 13:45:18 +0100
 Move sqlt dephandling to Optional::Deps
 r8679@Thesaurus (orig r8666):  ribasushi | 2010-02-12 14:03:17 +0100
 Move replicated to Opt::Deps
 r8684@Thesaurus (orig r8671):  ribasushi | 2010-02-13 02:47:52 +0100
 Auto-POD for Optional Deps
 r8685@Thesaurus (orig r8672):  ribasushi | 2010-02-13 02:53:20 +0100
 Privatize the full list method
 r8686@Thesaurus (orig r8673):  ribasushi | 2010-02-13 02:59:51 +0100
 Scary warning
 r8687@Thesaurus (orig r8674):  ribasushi | 2010-02-13 09:35:01 +0100
 Changes

lib/DBIx/Class.pm
lib/DBIx/Class/Admin.pm [new file with mode: 0644]
lib/DBIx/Class/Admin/Types.pm [new file with mode: 0644]
script/dbicadmin
t/admin/01load.t [new file with mode: 0644]
t/admin/02ddl.t [new file with mode: 0644]
t/admin/03data.t [new file with mode: 0644]
t/admin/10script.t [moved from t/89dbicadmin.t with 74% similarity]
t/lib/DBICTest/Schema/ForceForeign.pm

index 9f5bdd0..76b4490 100644 (file)
@@ -259,6 +259,8 @@ dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
 
 frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
 
+goraxe: Gordon Irving <goraxe@cpan.org>
+
 gphat: Cory G Watson <gphat@cpan.org>
 
 groditi: Guillermo Roditi <groditi@cpan.org>
diff --git a/lib/DBIx/Class/Admin.pm b/lib/DBIx/Class/Admin.pm
new file mode 100644 (file)
index 0000000..bd7cec6
--- /dev/null
@@ -0,0 +1,602 @@
+package DBIx::Class::Admin;
+
+# check deps
+BEGIN {
+    my @_deps = qw(
+        Moose MooseX::Types MooseX::Types::JSON MooseX::Types::Path::Class
+        Try::Tiny parent JSON::Any Class::C3::Componentised
+        namespace::autoclean
+    );
+
+    my @_missing_deps;
+    foreach my $dep (@_deps) {
+      eval "require $dep";
+      if ($@) {
+        push @_missing_deps, $dep;
+      }
+    }
+
+    if (@_missing_deps > 0) {
+      die "The following dependecies are missing " . join ",", @_missing_deps;
+    }
+}
+
+use Moose;
+use parent 'DBIx::Class::Schema';
+use Carp::Clan qw/^DBIx::Class/;
+
+use MooseX::Types::Moose qw/Int Str Any Bool/;
+use DBIx::Class::Admin::Types qw/DBICConnectInfo DBICHashRef/;
+use MooseX::Types::JSON qw(JSON);
+use MooseX::Types::Path::Class qw(Dir File);
+use Try::Tiny;
+use JSON::Any qw(DWIW XS JSON);
+use namespace::autoclean;
+
+=head1 NAME
+
+DBIx::Class::Admin - Administration object for schemas
+
+=head1 SYNOPSIS
+
+  $ dbicadmin --help
+
+  $ dbicadmin --schema=MyApp::Schema \
+    --connect='["dbi:SQLite:my.db", "", ""]' \
+    --deploy
+
+  $ dbicadmin --schema=MyApp::Schema --class=Employee \
+    --connect='["dbi:SQLite:my.db", "", ""]' \
+    --op=update --set='{ "name": "New_Employee" }'
+
+  use DBIx::Class::Admin;
+
+  # ddl manipulation
+  my $admin = DBIx::Class::Admin->new(
+    schema_class=> 'MY::Schema',
+    sql_dir=> $sql_dir,
+    connect_info => { dsn => $dsn, user => $user, password => $pass },
+  );
+
+  # create SQLite sql
+  $admin->create('SQLite');
+
+  # create SQL diff for an upgrade
+  $admin->create('SQLite', {} , "1.0");
+
+  # upgrade a database
+  $admin->upgrade();
+
+  # install a version for an unversioned schema
+  $admin->install("3.0");
+
+=head1 REQUIREMENTS
+
+The following CPAN modules are required to use C<dbicadmin> and this module:
+
+L<Moose>
+
+L<MooseX::Types>
+
+L<MooseX::Types::JSON>
+
+L<MooseX::Types::Path::Class>
+
+L<Try::Tiny>
+
+L<parent>
+
+L<JSON::Any>
+
+(L<JSON::DWIW> preferred for barekey support)
+
+L<namespace::autoclean>
+
+L<Getopt::Long::Descriptive>
+
+L<Text::CSV>
+
+=head1 Attributes
+
+=head2 schema_class
+
+the class of the schema to load
+
+=cut
+
+has 'schema_class' => (
+  is    => 'ro',
+  isa    => Str,
+);
+
+
+=head2 schema
+
+A pre-connected schema object can be provided for manipulation
+
+=cut
+
+has 'schema' => (
+  is      => 'ro',
+  isa      => 'DBIx::Class::Schema',
+  lazy_build  => 1,
+);
+
+sub _build_schema {
+  my ($self)  = @_;
+  $self->ensure_class_loaded($self->schema_class);
+
+  $self->connect_info->[3]->{ignore_version} =1;
+  return $self->schema_class->connect(@{$self->connect_info()} ); # ,  $self->connect_info->[3], { ignore_version => 1} );
+}
+
+
+=head2 resultset
+
+a resultset from the schema to operate on
+
+=cut
+
+has 'resultset' => (
+  is      => 'rw',
+  isa      => Str,
+);
+
+
+=head2 where
+
+a hash ref or json string to be used for identifying data to manipulate
+
+=cut
+
+has 'where' => (
+  is      => 'rw',
+  isa      => DBICHashRef,
+  coerce    => 1,
+);
+
+
+=head2 set
+
+a hash ref or json string to be used for inserting or updating data
+
+=cut
+
+has 'set' => (
+  is      => 'rw',
+  isa      => DBICHashRef,
+  coerce    => 1,
+);
+
+
+=head2 attrs
+
+a hash ref or json string to be used for passing additonal info to the ->search call
+
+=cut
+
+has 'attrs' => (
+  is       => 'rw',
+  isa      => DBICHashRef,
+  coerce    => 1,
+);
+
+
+=head2 connect_info
+
+connect_info the arguments to provide to the connect call of the schema_class
+
+=cut
+
+has 'connect_info' => (
+  is      => 'ro',
+  isa      => DBICConnectInfo,
+  lazy_build  => 1,
+  coerce    => 1,
+);
+
+sub _build_connect_info {
+  my ($self) = @_;
+  return $self->_find_stanza($self->config, $self->config_stanza);
+}
+
+
+=head2 config_file
+
+config_file provide a config_file to read connect_info from, if this is provided
+config_stanze should also be provided to locate where the connect_info is in the config
+The config file should be in a format readable by Config::General
+
+=cut
+
+has config_file => (
+  is      => 'ro',
+  isa      => File,
+  coerce    => 1,
+);
+
+
+=head2 config_stanza
+
+config_stanza for use with config_file should be a '::' deliminated 'path' to the connection information
+designed for use with catalyst config files
+
+=cut
+
+has 'config_stanza' => (
+  is      => 'ro',
+  isa      => Str,
+);
+
+
+=head2 config
+
+Instead of loading from a file the configuration can be provided directly as a hash ref.  Please note 
+config_stanza will still be required.
+
+=cut
+
+has config => (
+  is      => 'ro',
+  isa      => DBICHashRef,
+  lazy_build  => 1,
+);
+
+sub _build_config {
+  my ($self) = @_;
+  try { require Config::Any } catch { $self->throw_exception( "Config::Any is required to parse the config file"); };
+
+  my $cfg = Config::Any->load_files ( {files => [$self->config_file], use_ext =>1, flatten_to_hash=>1});
+
+  # just grab the config from the config file
+  $cfg = $cfg->{$self->config_file};
+  return $cfg;
+}
+
+
+=head2 sql_dir
+
+The location where sql ddl files should be created or found for an upgrade.
+
+=cut
+
+has 'sql_dir' => (
+  is      => 'ro',
+  isa      => Dir,
+  coerce    => 1,
+);
+
+
+=head2 version
+
+Used for install, the version which will be 'installed' in the schema
+
+=cut
+
+has version => (
+  is      => 'rw',
+  isa      => Str,
+);
+
+
+=head2 preversion
+
+Previouse version of the schema to create an upgrade diff for, the full sql for that version of the sql must be in the sql_dir
+
+=cut
+
+has preversion => (
+  is      => 'rw',
+  isa      => Str,
+);
+
+
+=head2 force
+
+Try and force certain operations.
+
+=cut
+
+has force => (
+  is      => 'rw',
+  isa      => Bool,
+);
+
+
+=head2 quiet
+
+Be less verbose about actions
+
+=cut
+
+has quiet => (
+  is      => 'rw',
+  isa      => Bool,
+);
+
+has '_confirm' => (
+  is    => 'bare',
+  isa    => Bool,
+);
+
+
+=head1 METHODS
+
+=head2 create
+
+=over 4
+
+=item Arguments: $sqlt_type, \%sqlt_args, $preversion
+
+=back
+
+L<create> will generate sql for the supplied schema_class in sql_dir.  The flavour of sql to 
+generate can be controlled by suppling a sqlt_type which should be a L<SQL::Translator> name.  
+
+Arguments for L<SQL::Translator> can be supplied in the sqlt_args hashref.
+
+Optional preversion can be supplied to generate a diff to be used by upgrade.
+
+=cut
+
+sub create {
+  my ($self, $sqlt_type, $sqlt_args, $preversion) = @_;
+
+  $preversion ||= $self->preversion();
+
+  my $schema = $self->schema();
+  # create the dir if does not exist
+  $self->sql_dir->mkpath() if ( ! -d $self->sql_dir);
+
+  $schema->create_ddl_dir( $sqlt_type, (defined $schema->schema_version ? $schema->schema_version : ""), $self->sql_dir->stringify, $preversion, $sqlt_args );
+}
+
+
+=head2 upgrade
+
+=over 4
+
+=item Arguments: <none>
+
+=back
+
+upgrade will attempt to upgrade the connected database to the same version as the schema_class.
+B<MAKE SURE YOU BACKUP YOUR DB FIRST>
+
+=cut
+
+sub upgrade {
+  my ($self) = @_;
+  my $schema = $self->schema();
+  if (!$schema->get_db_version()) {
+    # schema is unversioned
+    $self->throw_exception ("could not determin current schema version, please either install or deploy");
+  } else {
+    my $ret = $schema->upgrade();
+    return $ret;
+  }
+}
+
+
+=head2 install
+
+=over 4
+
+=item Arguments: $version
+
+=back
+
+install is here to help when you want to move to L<DBIx::Class::Schema::Versioned> and have an existing 
+database.  install will take a version and add the version tracking tables and 'install' the version.  No 
+further ddl modification takes place.  Setting the force attribute to a true value will allow overriding of 
+already versioned databases.
+
+=cut
+
+sub install {
+  my ($self, $version) = @_;
+
+  my $schema = $self->schema();
+  $version ||= $self->version();
+  if (!$schema->get_db_version() ) {
+    # schema is unversioned
+    print "Going to install schema version\n";
+    my $ret = $schema->install($version);
+    print "retun is $ret\n";
+  }
+  elsif ($schema->get_db_version() and $self->force ) {
+    carp "Forcing install may not be a good idea";
+    if($self->_confirm() ) {
+      $self->schema->_set_db_version({ version => $version});
+    }
+  }
+  else {
+    $self->throw_exception ("schema already has a version not installing, try upgrade instead");
+  }
+
+}
+
+
+=head2 deploy
+
+=over 4
+
+=item Arguments: $args
+
+=back
+
+deploy will create the schema at the connected database.  C<$args> are passed straight to 
+L<DBIx::Class::Schema/deploy>.
+
+=cut
+
+sub deploy {
+  my ($self, $args) = @_;
+  my $schema = $self->schema();
+  if (!$schema->get_db_version() ) {
+    # schema is unversioned
+    $schema->deploy( $args, $self->sql_dir)
+      or $self->throw_exception ("could not deploy schema");
+  } else {
+    $self->throw_exception("there already is a database with a version here, try upgrade instead");
+  }
+}
+
+=head2 insert
+
+=over 4
+
+=item Arguments: $rs, $set
+
+=back
+
+insert takes the name of a resultset from the schema_class and a hashref of data to insert
+into that resultset
+
+=cut
+
+sub insert {
+  my ($self, $rs, $set) = @_;
+
+  $rs ||= $self->resultset();
+  $set ||= $self->set();
+  my $resultset = $self->schema->resultset($rs);
+  my $obj = $resultset->create( $set );
+  print ''.ref($resultset).' ID: '.join(',',$obj->id())."\n" if (!$self->quiet);
+}
+
+
+=head2 update
+
+=over 4
+
+=item Arguments: $rs, $set, $where
+
+=back
+
+update takes the name of a resultset from the schema_class, a hashref of data to update and
+a where hash used to form the search for the rows to update.
+
+=cut
+
+sub update {
+  my ($self, $rs, $set, $where) = @_;
+
+  $rs ||= $self->resultset();
+  $where ||= $self->where();
+  $set ||= $self->set();
+  my $resultset = $self->schema->resultset($rs);
+  $resultset = $resultset->search( ($where||{}) );
+
+  my $count = $resultset->count();
+  print "This action will modify $count ".ref($resultset)." records.\n" if (!$self->quiet);
+
+  if ( $self->force || $self->_confirm() ) {
+    $resultset->update_all( $set );
+  }
+}
+
+
+=head2 delete
+
+=over 4
+
+=item Arguments: $rs, $where, $attrs
+
+=back
+
+delete takes the name of a resultset from the schema_class, a where hashref and a attrs to pass to ->search.
+The found data is deleted and cannot be recovered.
+
+=cut
+
+sub delete {
+  my ($self, $rs, $where, $attrs) = @_;
+
+  $rs ||= $self->resultset();
+  $where ||= $self->where();
+  $attrs ||= $self->attrs();
+  my $resultset = $self->schema->resultset($rs);
+  $resultset = $resultset->search( ($where||{}), ($attrs||()) );
+
+  my $count = $resultset->count();
+  print "This action will delete $count ".ref($resultset)." records.\n" if (!$self->quiet);
+
+  if ( $self->force || $self->_confirm() ) {
+    $resultset->delete_all();
+  }
+}
+
+
+=head2 select
+
+=over 4
+
+=item Arguments: $rs, $where, $attrs
+
+=back
+
+select takes the name of a resultset from the schema_class, a where hashref and a attrs to pass to ->search. 
+The found data is returned in a array ref where the first row will be the columns list.
+
+=cut
+
+sub select {
+  my ($self, $rs, $where, $attrs) = @_;
+
+  $rs ||= $self->resultset();
+  $where ||= $self->where();
+  $attrs ||= $self->attrs();
+  my $resultset = $self->schema->resultset($rs);
+  $resultset = $resultset->search( ($where||{}), ($attrs||()) );
+
+  my @data;
+  my @columns = $resultset->result_source->columns();
+  push @data, [@columns];# 
+
+  while (my $row = $resultset->next()) {
+    my @fields;
+    foreach my $column (@columns) {
+      push( @fields, $row->get_column($column) );
+    }
+    push @data, [@fields];
+  }
+
+  return \@data;
+}
+
+sub _confirm {
+  my ($self) = @_;
+  print "Are you sure you want to do this? (type YES to confirm) \n";
+  # mainly here for testing
+  return 1 if ($self->meta->get_attribute('_confirm')->get_value($self));
+  my $response = <STDIN>;
+  return 1 if ($response=~/^YES/);
+  return;
+}
+
+sub _find_stanza {
+  my ($self, $cfg, $stanza) = @_;
+  my @path = split /::/, $stanza;
+  while (my $path = shift @path) {
+    if (exists $cfg->{$path}) {
+      $cfg = $cfg->{$path};
+    }
+    else {
+      $self->throw_exception("could not find $stanza in config, $path did not seem to exist");
+    }
+  }
+  return $cfg;
+}
+
+=head1 AUTHOR
+
+See L<DBIx::Class/CONTRIBUTORS>.
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself
+
+=cut
+
+1;
diff --git a/lib/DBIx/Class/Admin/Types.pm b/lib/DBIx/Class/Admin/Types.pm
new file mode 100644 (file)
index 0000000..23af292
--- /dev/null
@@ -0,0 +1,48 @@
+package # hide from PAUSE
+    DBIx::Class::Admin::Types;
+
+use MooseX::Types -declare => [qw(
+    DBICConnectInfo
+    DBICArrayRef
+    DBICHashRef
+)];
+use MooseX::Types::Moose qw/Int HashRef ArrayRef Str Any Bool/;
+use MooseX::Types::JSON qw(JSON);
+
+subtype DBICArrayRef,
+    as ArrayRef;
+
+subtype DBICHashRef,
+    as HashRef;
+
+coerce DBICArrayRef,
+  from JSON,
+  via { _json_to_data ($_) };
+
+coerce DBICHashRef,
+  from JSON,
+  via { _json_to_data($_) };
+
+subtype DBICConnectInfo,
+  as ArrayRef;
+
+coerce DBICConnectInfo,
+  from JSON,
+   via { return _json_to_data($_) } ;
+
+coerce DBICConnectInfo,
+  from Str,
+    via { return _json_to_data($_) };
+
+coerce DBICConnectInfo,
+  from HashRef,
+   via { [ $_ ] };
+
+sub _json_to_data {
+  my ($json_str) = @_;
+  my $json = JSON::Any->new(allow_barekey => 1, allow_singlequote => 1, relaxed=>1);
+  my $ret = $json->jsonToObj($json_str);
+  return $ret;
+}
+
+1;
index d6c8ecd..35a600a 100755 (executable)
 #!/usr/bin/perl
+
 use strict;
 use warnings;
 
-use Getopt::Long;
-use Pod::Usage;
-use JSON::Any;
-
-
-my $json = JSON::Any->new(allow_barekey => 1, allow_singlequote => 1);
-
-GetOptions(
-    'schema=s'  => \my $schema_class,
-    'class=s'   => \my $resultset_class,
-    'connect=s' => \my $connect,
-    'op=s'      => \my $op,
-    'set=s'     => \my $set,
-    'where=s'   => \my $where,
-    'attrs=s'   => \my $attrs,
-    'format=s'  => \my $format,
-    'force'     => \my $force,
-    'trace'     => \my $trace,
-    'quiet'     => \my $quiet,
-    'help'      => \my $help,
-    'tlibs'      => \my $t_libs,
-);
-
-if ($t_libs) {
-    unshift( @INC, 't/lib', 'lib' );
-}
-
-pod2usage(1) if ($help);
-$ENV{DBIC_TRACE} = 1 if ($trace);
-
-die('No op specified') if(!$op);
-die('Invalid op') if ($op!~/^insert|update|delete|select$/s);
-my $csv_class;
-if ($op eq 'select') {
-    $format ||= 'tsv';
-    die('Invalid format') if ($format!~/^tsv|csv$/s);
-    $csv_class = 'Text::CSV_XS';
-    eval{ require Text::CSV_XS };
-    if ($@) {
-        $csv_class = 'Text::CSV_PP';
-        eval{ require Text::CSV_PP };
-        die('The select op requires either the Text::CSV_XS or the Text::CSV_PP module') if ($@);
-    }
-}
-
-die('No schema specified') if(!$schema_class);
-eval("require $schema_class");
-die('Unable to load schema') if ($@);
-$connect = $json->jsonToObj( $connect ) if ($connect);
-my $schema = $schema_class->connect(
-    ( $connect ? @$connect : () )
+use Getopt::Long::Descriptive;
+use DBIx::Class::Admin;
+
+my ($opts, $usage) = describe_options(
+  "%c: %o",
+  (
+    ['Actions'],
+    ["action" => hidden => { one_of => [
+      ['create|c' => 'Create version diffs needs preversion',],
+      ['upgrade|u' => 'Upgrade the database to the current schema '],
+      ['install|i' => 'Install the schema to the database',],
+      ['deploy|d' => 'Deploy the schema to the database',],
+      ['select|s'   => 'Select data from the schema', ],
+      ['insert|i'   => 'Insert data into the schema', ],
+      ['update|u'   => 'Update data in the schema', ], 
+      ['delete|D'   => 'Delete data from the schema',],
+      ['op:s' => 'compatiblity option all of the above can be suppied as --op=<action>'],
+      ['help|h' => 'display this help'],
+    ], required=> 1 }],
+    ['Options'],
+    ['schema-class|schema|C:s' => 'The class of the schema to load', { required => 1 } ],
+    ['resultset|resultset_class|class|r:s' => 'The resultset to operate on for data manipulation' ],
+    ['config-stanza|S:s' => 'Where in the config to find the connection_info, supply in form MyApp::Model::DB',],
+    ['config|f:s' => 'Supply the config file for parsing by Config::Any', { depends => 'config_stanza'} ],
+    ['connect-info|n:s%' => 'Supply the connect info as additonal options ie -I dsn=<dsn> user=<user> password=<pass> '],
+    ['connect:s' => 'Supply the connect info as a json string' ],
+    ['sql-dir|q:s' => 'The directory where sql diffs will be created'],
+    ['sql-type|t:s' => 'The RDBMs flavour you wish to use'],
+    ['version|v:i' => 'Supply a version install'],
+    ['preversion|p:s' => 'The previous version to diff against',],
+    ['set:s' => 'JSON data used to perform data operations' ],
+    ['lib|I:s' => 'Additonal library path to search in'], 
+    ['attrs:s' => 'JSON string to be used for the second argument for search'],
+    ['where:s' => 'JSON string to be used for the where clause of search'],
+    ['force' => 'Be forceful with some operations'],
+    ['trace' => 'Turn on DBIx::Class trace output'],
+    ['quiet' => 'Be less verbose'],
+  )
 );
 
-die('No class specified') if(!$resultset_class);
-my $resultset = eval{ $schema->resultset($resultset_class) };
-die('Unable to load the class with the schema') if ($@);
-
-$set = $json->jsonToObj( $set ) if ($set);
-$where = $json->jsonToObj( $where ) if ($where);
-$attrs = $json->jsonToObj( $attrs ) if ($attrs);
 
-if ($op eq 'insert') {
-    die('Do not use the where option with the insert op') if ($where);
-    die('Do not use the attrs option with the insert op') if ($attrs);
-    my $obj = $resultset->create( $set );
-    print ''.ref($resultset).' ID: '.join(',',$obj->id())."\n" if (!$quiet);
-}
-elsif ($op eq 'update') {
-    $resultset = $resultset->search( ($where||{}) );
-    my $count = $resultset->count();
-    print "This action will modify $count ".ref($resultset)." records.\n" if (!$quiet);
-    if ( $force || confirm() ) {
-        $resultset->update_all( $set );
-    }
-}
-elsif ($op eq 'delete') {
-    die('Do not use the set option with the delete op') if ($set);
-    $resultset = $resultset->search( ($where||{}), ($attrs||()) );
-    my $count = $resultset->count();
-    print "This action will delete $count ".ref($resultset)." records.\n" if (!$quiet);
-    if ( $force || confirm() ) {
-        $resultset->delete_all();
-    }
-}
-elsif ($op eq 'select') {
-    die('Do not use the set option with the select op') if ($set);
-    my $csv = $csv_class->new({
-        sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
-    });
-    $resultset = $resultset->search( ($where||{}), ($attrs||()) );
-    my @columns = $resultset->result_source->columns();
-    $csv->combine( @columns );
-    print $csv->string()."\n";
-    while (my $row = $resultset->next()) {
-        my @fields;
-        foreach my $column (@columns) {
-            push( @fields, $row->get_column($column) );
-        }
-        $csv->combine( @fields );
-        print $csv->string()."\n";
-    }
-}
+die "please only use one of --config or --connect-info" if ($opts->{config} and $opts->{connect_info});
 
-sub confirm {
-    print "Are you sure you want to do this? (type YES to confirm) ";
-    my $response = <STDIN>;
-    return 1 if ($response=~/^YES/);
-    return;
+# option compatability mangle
+if($opts->{connect}) {
+  $opts->{connect_info} = delete $opts->{connect};
 }
 
-__END__
-
-=head1 NAME
-
-dbicadmin - Execute operations upon DBIx::Class objects.
-
-=head1 SYNOPSIS
-
-  dbicadmin --op=insert --schema=My::Schema --class=Class --set=JSON
-  dbicadmin --op=update --schema=My::Schema --class=Class --set=JSON --where=JSON
-  dbicadmin --op=delete --schema=My::Schema --class=Class --where=JSON
-  dbicadmin --op=select --schema=My::Schema --class=Class --where=JSON --format=tsv
-
-=head1 DESCRIPTION
-
-This utility provides the ability to run INSERTs, UPDATEs, 
-DELETEs, and SELECTs on any DBIx::Class object.
-
-=head1 OPTIONS
-
-=head2 op
-
-The type of operation.  Valid values are insert, update, delete, 
-and select.
-
-=head2 schema
+my $admin = DBIx::Class::Admin->new( %$opts );
 
-The name of your schema class.
 
-=head2 class
+my $action = $opts->{action};
 
-The name of the class, within your schema, that you want to run 
-the operation on.
+$action = $opts->{op} if ($action eq 'op');
+my $res = $admin->$action();
 
-=head2 connect
+print "going to perform action $action\n";
+if ($action eq 'select') {
 
-A JSON array to be passed to your schema class upon connecting.  
-The array will need to be compatible with whatever the DBIC 
-->connect() method requires.
+  my $csv_class;
+  my $format = $opts->{format} || 'tsv';
+  die('Invalid format') if ($format!~/^tsv|csv$/s);
+  $csv_class = 'Text::CSV_XS';
+  eval{ require Text::CSV_XS };
+  if ($@) {
+    $csv_class = 'Text::CSV_PP';
+    eval{ require Text::CSV_PP };
+    die('The select op requires either the Text::CSV_XS or the Text::CSV_PP module') if ($@);
+  }
 
-=head2 set
-
-This option must be valid JSON data string and is passed in to 
-the DBIC update() method.  Use this option with the update 
-and insert ops.
-
-=head2 where
-
-This option must be valid JSON data string and is passed in as 
-the first argument to the DBIC search() method.  Use this 
-option with the update, delete, and select ops.
-
-=head2 attrs
-
-This option must be valid JSON data string and is passed in as 
-the second argument to the DBIC search() method.  Use this 
-option with the update, delete, and select ops.
-
-=head2 help
-
-Display this help page.
-
-=head2 force
-
-Suppresses the confirmation dialogues that are usually displayed 
-when someone runs a DELETE or UPDATE action.
-
-=head2 quiet
-
-Do not display status messages.
-
-=head2 trace
-
-Turns on tracing on the DBI storage, thus printing SQL as it is 
-executed.
-
-=head2 tlibs
-
-This option is purely for testing during the DBIC installation.  Do 
-not use it.
-
-=head1 JSON
-
-JSON is a lightweight data-interchange format.  It allows you 
-to express complex data structures for use in the where and 
-set options.
-
-This module turns on L<JSON>'s BareKey and QuotApos options so 
-that your data can look a bit more readable.
-
-  --where={"this":"that"} # generic JSON
-  --where={this:'that'}   # with BareKey and QuoteApos
+  my $csv = $csv_class->new({
+      sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
+    });
+  foreach my $row (@$res) {
+    $csv->combine( @$row );
+    print $csv->string()."\n";
+  }
+}
 
-Consider wrapping your JSON in outer quotes so that you don't 
-have to escape your inner quotes.
 
-  --where={this:\"that\"} # no outer quote
-  --where='{this:"that"}' # outer quoted
 
 =head1 AUTHOR
 
 Aran Deltac <bluefeet@cpan.org>
 
+refactored by
+Gordon Irving <goraxe@cpan.org>
+
 =head1 LICENSE
 
 You may distribute this code under the same terms as Perl itself.
-
diff --git a/t/admin/01load.t b/t/admin/01load.t
new file mode 100644 (file)
index 0000000..0b5d802
--- /dev/null
@@ -0,0 +1,15 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+
+BEGIN {
+    eval "use DBIx::Class::Admin";
+    plan skip_all => "Deps not installed: $@" if $@;
+}
+
+use_ok 'DBIx::Class::Admin';
+
+
+done_testing;
diff --git a/t/admin/02ddl.t b/t/admin/02ddl.t
new file mode 100644 (file)
index 0000000..fb0f74c
--- /dev/null
@@ -0,0 +1,131 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Exception;
+use Test::Warn;
+
+
+BEGIN {
+    eval "use DBIx::Class::Admin";
+    plan skip_all => "Deps not installed: $@" if $@;
+}
+
+
+use lib qw(t/lib);
+use DBICTest;
+
+use Path::Class;
+
+use ok 'DBIx::Class::Admin';
+
+
+my $sql_dir = dir(qw/t var/);
+my @connect_info = DBICTest->_database(
+  no_deploy=>1,
+  no_populate=>1,
+  sqlite_use_file  => 1,
+);
+{ # create the schema
+
+#  make sure we are  clean
+clean_dir($sql_dir);
+
+
+my $admin = DBIx::Class::Admin->new(
+  schema_class=> "DBICTest::Schema",
+  sql_dir=> $sql_dir,
+  connect_info => \@connect_info, 
+);
+isa_ok ($admin, 'DBIx::Class::Admin', 'create the admin object');
+lives_ok { $admin->create('MySQL'); } 'Can create MySQL sql';
+lives_ok { $admin->create('SQLite'); } 'Can Create SQLite sql';
+}
+
+{ # upgrade schema
+
+#my $schema = DBICTest->init_schema(
+#  no_deploy    => 1,
+#  no_populat    => 1,
+#  sqlite_use_file  => 1,
+#);
+
+clean_dir($sql_dir);
+require DBICVersionOrig;
+
+my $admin = DBIx::Class::Admin->new(
+  schema_class => 'DBICVersion::Schema', 
+  sql_dir =>  $sql_dir,
+  connect_info => \@connect_info,
+);
+
+my $schema = $admin->schema();
+
+lives_ok { $admin->create($schema->storage->sqlt_type(), {add_drop_table=>0}); } 'Can create DBICVersionOrig sql in ' . $schema->storage->sqlt_type;
+lives_ok { $admin->deploy(  ) } 'Can Deploy schema';
+
+# connect to now deployed schema
+lives_ok { $schema = DBICVersion::Schema->connect(@{$schema->storage->connect_info()}); } 'Connect to deployed Database';
+
+is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema deployed and versions match');
+
+
+require DBICVersionNew;
+
+$admin = DBIx::Class::Admin->new(
+  schema_class => 'DBICVersion::Schema', 
+  sql_dir =>  $sql_dir,
+  connect_info => \@connect_info
+);
+
+lives_ok { $admin->create($schema->storage->sqlt_type(), {}, "1.0" ); } 'Can create diff for ' . $schema->storage->sqlt_type;
+# sleep required for upgrade table to hold a distinct time of upgrade value
+# otherwise the returned of get_db_version can be undeterministic
+sleep 1;
+{
+  local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DB version .+? is lower than the schema version/ };
+  lives_ok {$admin->upgrade();} 'upgrade the schema';
+}
+
+is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema and db versions match');
+
+}
+
+{ # install
+
+clean_dir($sql_dir);
+
+my $admin = DBIx::Class::Admin->new(
+  schema_class  => 'DBICVersion::Schema', 
+  sql_dir      => $sql_dir,
+  _confirm    => 1,
+  connect_info  => \@connect_info,
+);
+
+$admin->version("3.0");
+lives_ok { $admin->install(); } 'install schema version 3.0';
+is($admin->schema->get_db_version, "3.0", 'db thinks its version 3.0');
+dies_ok { $admin->install("4.0"); } 'cannot install to allready existing version';
+sleep 1;
+$admin->force(1);
+warnings_exist ( sub {
+  lives_ok { $admin->install("4.0") } 'can force install to allready existing version'
+}, qr/Forcing install may not be a good idea/, 'Force warning emitted' );
+is($admin->schema->get_db_version, "4.0", 'db thinks its version 4.0');
+#clean_dir($sql_dir);
+}
+
+sub clean_dir {
+  my ($dir) = @_;
+  $dir = $dir->resolve;
+  if ( ! -d $dir ) {
+    $dir->mkpath();
+  }
+  foreach my $file ($dir->children) {
+    # skip any hidden files
+    next if ($file =~ /^\./); 
+    unlink $file;
+  }
+}
+
+done_testing;
diff --git a/t/admin/03data.t b/t/admin/03data.t
new file mode 100644 (file)
index 0000000..3bbdcc5
--- /dev/null
@@ -0,0 +1,64 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+use Test::Exception;
+use Test::Deep;
+
+BEGIN {
+    eval "use DBIx::Class::Admin";
+    plan skip_all => "Deps not installed: $@" if $@;
+}
+
+use lib 't/lib';
+use DBICTest;
+
+use ok 'DBIx::Class::Admin';
+
+
+{ # test data maniplulation functions
+
+  # create a DBICTest so we can steal its connect info
+  my $schema = DBICTest->init_schema(
+    sqlite_use_file => 1,
+  );
+
+  my $admin = DBIx::Class::Admin->new(
+    schema_class=> "DBICTest::Schema",
+    connect_info => $schema->storage->connect_info(),
+    quiet  => 1,
+    _confirm=>1,
+  );
+  isa_ok ($admin, 'DBIx::Class::Admin', 'create the admin object');
+
+  $admin->insert('Employee', { name => 'Matt' });
+  my $employees = $schema->resultset('Employee');
+  is ($employees->count(), 1, "insert okay" );
+
+  my $employee = $employees->find(1);
+  is($employee->name(),  'Matt', "insert valid" );
+
+  $admin->update('Employee', {name => 'Trout'}, {name => 'Matt'});
+
+  $employee = $employees->find(1);
+  is($employee->name(),  'Trout', "update Matt to Trout" );
+
+  $admin->insert('Employee', {name =>'Aran'});
+
+  my $expected_data = [ 
+    [$employee->result_source->columns() ],
+    [1,1,undef,undef,undef,'Trout'],
+    [2,2,undef,undef,undef,'Aran']
+  ];
+  my $data;
+  lives_ok { $data = $admin->select('Employee')} 'can retrive data from database';
+  cmp_deeply($data, $expected_data, 'DB matches whats expected');
+
+  $admin->delete('Employee', {name=>'Trout'});
+  my $del_rs  = $employees->search({name => 'Trout'});
+  is($del_rs->count(), 0, "delete Trout" );
+  is ($employees->count(), 1, "left Aran" );
+}
+
+done_testing;
similarity index 74%
rename from t/89dbicadmin.t
rename to t/admin/10script.t
index 1729d2d..a53ecf1 100644 (file)
@@ -1,21 +1,30 @@
 # vim: filetype=perl
 use strict;
-use warnings;  
+use warnings;
 
 use Test::More;
+use Config;
 use lib qw(t/lib);
+$ENV{PERL5LIB} = join ($Config{path_sep}, @INC);
 use DBICTest;
 
 
-eval 'require JSON::Any';
-plan skip_all => 'Install JSON::Any to run this test' if ($@);
+BEGIN {
+    eval "require DBIx::Class::Admin";
+    plan skip_all => "Deps not installed: $@" if $@;
 
-eval 'require Text::CSV_XS';
-if ($@) {
-    eval 'require Text::CSV_PP';
-    plan skip_all => 'Install Text::CSV_XS or Text::CSV_PP to run this test' if ($@);
-}
+    eval "require Getopt::Long::Descriptive";
+    plan skip_all => 'Install Getopt::Long::Descriptive to run this test' if ($@);
+
+    eval 'require JSON::Any';
+    plan skip_all => 'Install JSON::Any to run this test' if ($@);
 
+    eval 'require Text::CSV_XS';
+    if ($@) {
+        eval 'require Text::CSV_PP';
+        plan skip_all => 'Install Text::CSV_XS or Text::CSV_PP to run this test' if ($@);
+    }
+}
 my @json_backends = qw/XS JSON DWIW/;
 my $tests_per_run = 5;
 
@@ -34,6 +43,8 @@ for my $js (@json_backends) {
 }
 
 sub test_dbicadmin {
+#    $ENV{PERL5LIB} = join ':', @INC;
+
     my $schema = DBICTest->init_schema( sqlite_use_file => 1 );  # reinit a fresh db for every run
 
     my $employees = $schema->resultset('Employee');
@@ -56,7 +67,9 @@ sub test_dbicadmin {
         open(my $fh, "-|",  _prepare_system_args( qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!;
         my $data = do { local $/; <$fh> };
         close($fh);
-        ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" );
+        if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) {
+          diag ("data from select is $data")
+        };
     }
 
     system( _prepare_system_args( qw|--op=delete --where={"name":"Trout"}| ) );
@@ -71,10 +84,11 @@ sub test_dbicadmin {
 #
 sub _prepare_system_args {
     my $perl = $^X;
+
     my @args = (
-        qw|script/dbicadmin --quiet --schema=DBICTest::Schema --class=Employee --tlibs|,
+        qw|script/dbicadmin --quiet --schema=DBICTest::Schema --class=Employee|,
         q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|,
-        qw|--force --tlibs|,
+        qw|--force|,
         @_,
     );
 
index 8e2daeb..c340d8b 100644 (file)
@@ -12,30 +12,21 @@ __PACKAGE__->set_primary_key(qw/artist/);
 
 # Normally this would not appear as a FK constraint
 # since it uses the PK
-__PACKAGE__->might_have(
-                       'artist_1', 'DBICTest::Schema::Artist', {
-                           'foreign.artistid' => 'self.artist',
-                       }, {
-                           is_foreign_key_constraint => 1,
-                       },
+__PACKAGE__->might_have('artist_1', 'DBICTest::Schema::Artist',
+  { 'foreign.artistid' => 'self.artist' },
+  { is_foreign_key_constraint => 1 },
 );
 
 # Normally this would appear as a FK constraint
-__PACKAGE__->might_have(
-                       'cd_1', 'DBICTest::Schema::CD', {
-                           'foreign.cdid' => 'self.cd',
-                       }, {
-                           is_foreign_key_constraint => 0,
-                       },
+__PACKAGE__->might_have('cd_1', 'DBICTest::Schema::CD',
+  { 'foreign.cdid' => 'self.cd' },
+  { is_foreign_key_constraint => 0 },
 );
 
 # Normally this would appear as a FK constraint
-__PACKAGE__->belongs_to(
-                       'cd_3', 'DBICTest::Schema::CD', {
-                           'foreign.cdid' => 'self.cd',
-                       }, {
-                           is_foreign_key_constraint => 0,
-                       },
+__PACKAGE__->belongs_to('cd_3', 'DBICTest::Schema::CD',
+  { 'foreign.cdid' => 'self.cd' },
+  { is_foreign_key_constraint => 0 },
 );
 
 1;