authorails 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/AUTHORAILS_README +20 -0
- data/CHANGELOG +1366 -0
- data/MIT-LICENSE +20 -0
- data/README +182 -0
- data/Rakefile +334 -0
- data/bin/about +3 -0
- data/bin/authorails +19 -0
- data/bin/breakpointer +3 -0
- data/bin/console +3 -0
- data/bin/destroy +3 -0
- data/bin/generate +3 -0
- data/bin/performance/benchmarker +3 -0
- data/bin/performance/profiler +3 -0
- data/bin/plugin +3 -0
- data/bin/process/inspector +3 -0
- data/bin/process/reaper +3 -0
- data/bin/process/spawner +3 -0
- data/bin/runner +3 -0
- data/bin/server +3 -0
- data/builtin/rails_info/rails/info.rb +123 -0
- data/builtin/rails_info/rails/info_controller.rb +9 -0
- data/builtin/rails_info/rails/info_helper.rb +2 -0
- data/builtin/rails_info/rails_info_controller.rb +2 -0
- data/configs/apache.conf +40 -0
- data/configs/databases/frontbase.yml +28 -0
- data/configs/databases/mysql.yml +48 -0
- data/configs/databases/oracle.yml +39 -0
- data/configs/databases/postgresql.yml +44 -0
- data/configs/databases/sqlite2.yml +16 -0
- data/configs/databases/sqlite3.yml +19 -0
- data/configs/empty.log +0 -0
- data/configs/lighttpd.conf +54 -0
- data/configs/routes.rb +23 -0
- data/controllers/accounts_controller.rb +11 -0
- data/controllers/admin_controller.rb +76 -0
- data/controllers/begin_controller.rb +5 -0
- data/controllers/login_controller.rb +101 -0
- data/controllers/permission_controller.rb +91 -0
- data/controllers/roles_controller.rb +58 -0
- data/controllers/tables_controller.rb +714 -0
- data/dispatches/dispatch.fcgi +24 -0
- data/dispatches/dispatch.rb +10 -0
- data/dispatches/gateway.cgi +97 -0
- data/doc/README_FOR_APP +2 -0
- data/environments/boot.rb +45 -0
- data/environments/development.rb +21 -0
- data/environments/environment.rb +63 -0
- data/environments/production.rb +18 -0
- data/environments/test.rb +19 -0
- data/fresh_rakefile +10 -0
- data/helpers/application.rb +607 -0
- data/helpers/application_helper.rb +315 -0
- data/helpers/permission_helper.rb +27 -0
- data/helpers/test_helper.rb +28 -0
- data/html/404.html +30 -0
- data/html/500.html +30 -0
- data/html/favicon.ico +0 -0
- data/html/images/ArrowDn.png +0 -0
- data/html/images/ArrowUp.png +0 -0
- data/html/images/arrow.gif +0 -0
- data/html/images/authorails_logo.gif +0 -0
- data/html/images/delete.png +0 -0
- data/html/images/edit.png +0 -0
- data/html/images/minus.gif +0 -0
- data/html/images/plus.gif +0 -0
- data/html/images/rails.png +0 -0
- data/html/images/show.png +0 -0
- data/html/images/spinner.gif +0 -0
- data/html/index.html +273 -0
- data/html/javascripts/application.js +2 -0
- data/html/javascripts/controls.js +833 -0
- data/html/javascripts/dragdrop.js +942 -0
- data/html/javascripts/effects.js +1088 -0
- data/html/javascripts/live_validation.js +4 -0
- data/html/javascripts/prototype.js +2515 -0
- data/html/robots.txt +1 -0
- data/lib/authorails/version.rb +9 -0
- data/lib/binding_of_caller.rb +85 -0
- data/lib/breakpoint.rb +553 -0
- data/lib/breakpoint_client.rb +196 -0
- data/lib/code_statistics.rb +107 -0
- data/lib/commands.rb +17 -0
- data/lib/commands/about.rb +2 -0
- data/lib/commands/breakpointer.rb +1 -0
- data/lib/commands/console.rb +25 -0
- data/lib/commands/destroy.rb +6 -0
- data/lib/commands/generate.rb +6 -0
- data/lib/commands/ncgi/listener +86 -0
- data/lib/commands/ncgi/tracker +69 -0
- data/lib/commands/performance/benchmarker.rb +24 -0
- data/lib/commands/performance/profiler.rb +50 -0
- data/lib/commands/plugin.rb +918 -0
- data/lib/commands/process/inspector.rb +68 -0
- data/lib/commands/process/reaper.rb +149 -0
- data/lib/commands/process/spawner.rb +209 -0
- data/lib/commands/process/spinner.rb +57 -0
- data/lib/commands/runner.rb +48 -0
- data/lib/commands/server.rb +39 -0
- data/lib/commands/servers/base.rb +19 -0
- data/lib/commands/servers/lighttpd.rb +94 -0
- data/lib/commands/servers/mongrel.rb +65 -0
- data/lib/commands/servers/webrick.rb +59 -0
- data/lib/commands/update.rb +4 -0
- data/lib/console_app.rb +27 -0
- data/lib/console_sandbox.rb +6 -0
- data/lib/console_with_helpers.rb +23 -0
- data/lib/dispatcher.rb +173 -0
- data/lib/fcgi_handler.rb +225 -0
- data/lib/initializer.rb +702 -0
- data/lib/rails_generator.rb +43 -0
- data/lib/rails_generator/base.rb +261 -0
- data/lib/rails_generator/commands.rb +581 -0
- data/lib/rails_generator/generated_attribute.rb +42 -0
- data/lib/rails_generator/generators/applications/app/USAGE +16 -0
- data/lib/rails_generator/generators/applications/app/app_generator.rb +341 -0
- data/lib/rails_generator/generators/components/controller/USAGE +30 -0
- data/lib/rails_generator/generators/components/controller/controller_generator.rb +37 -0
- data/lib/rails_generator/generators/components/controller/templates/controller.rb +10 -0
- data/lib/rails_generator/generators/components/controller/templates/functional_test.rb +18 -0
- data/lib/rails_generator/generators/components/controller/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/controller/templates/view.rhtml +2 -0
- data/lib/rails_generator/generators/components/integration_test/USAGE +14 -0
- data/lib/rails_generator/generators/components/integration_test/integration_test_generator.rb +16 -0
- data/lib/rails_generator/generators/components/integration_test/templates/integration_test.rb +10 -0
- data/lib/rails_generator/generators/components/mailer/USAGE +18 -0
- data/lib/rails_generator/generators/components/mailer/mailer_generator.rb +34 -0
- data/lib/rails_generator/generators/components/mailer/templates/fixture.rhtml +3 -0
- data/lib/rails_generator/generators/components/mailer/templates/mailer.rb +13 -0
- data/lib/rails_generator/generators/components/mailer/templates/unit_test.rb +37 -0
- data/lib/rails_generator/generators/components/mailer/templates/view.rhtml +3 -0
- data/lib/rails_generator/generators/components/migration/USAGE +14 -0
- data/lib/rails_generator/generators/components/migration/migration_generator.rb +7 -0
- data/lib/rails_generator/generators/components/migration/templates/migration.rb +7 -0
- data/lib/rails_generator/generators/components/model/USAGE +26 -0
- data/lib/rails_generator/generators/components/model/model_generator.rb +38 -0
- data/lib/rails_generator/generators/components/model/templates/fixtures.yml +11 -0
- data/lib/rails_generator/generators/components/model/templates/migration.rb +13 -0
- data/lib/rails_generator/generators/components/model/templates/model.rb +2 -0
- data/lib/rails_generator/generators/components/model/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/observer/USAGE +15 -0
- data/lib/rails_generator/generators/components/observer/observer_generator.rb +16 -0
- data/lib/rails_generator/generators/components/observer/templates/observer.rb +2 -0
- data/lib/rails_generator/generators/components/observer/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/plugin/USAGE +35 -0
- data/lib/rails_generator/generators/components/plugin/plugin_generator.rb +38 -0
- data/lib/rails_generator/generators/components/plugin/templates/README +4 -0
- data/lib/rails_generator/generators/components/plugin/templates/Rakefile +22 -0
- data/lib/rails_generator/generators/components/plugin/templates/USAGE +8 -0
- data/lib/rails_generator/generators/components/plugin/templates/generator.rb +8 -0
- data/lib/rails_generator/generators/components/plugin/templates/init.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/install.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/plugin.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/tasks.rake +4 -0
- data/lib/rails_generator/generators/components/plugin/templates/uninstall.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/unit_test.rb +8 -0
- data/lib/rails_generator/generators/components/resource/resource_generator.rb +76 -0
- data/lib/rails_generator/generators/components/resource/templates/USAGE +18 -0
- data/lib/rails_generator/generators/components/resource/templates/controller.rb +2 -0
- data/lib/rails_generator/generators/components/resource/templates/fixtures.yml +11 -0
- data/lib/rails_generator/generators/components/resource/templates/functional_test.rb +20 -0
- data/lib/rails_generator/generators/components/resource/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/resource/templates/migration.rb +13 -0
- data/lib/rails_generator/generators/components/resource/templates/model.rb +2 -0
- data/lib/rails_generator/generators/components/resource/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/scaffold/USAGE +32 -0
- data/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +208 -0
- data/lib/rails_generator/generators/components/scaffold/templates/controller.rb +134 -0
- data/lib/rails_generator/generators/components/scaffold/templates/form.rhtml +3 -0
- data/lib/rails_generator/generators/components/scaffold/templates/form_scaffolding.rhtml +35 -0
- data/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb +102 -0
- data/lib/rails_generator/generators/components/scaffold/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/scaffold/templates/layout.rhtml +17 -0
- data/lib/rails_generator/generators/components/scaffold/templates/partial_list_elements.rhtml +127 -0
- data/lib/rails_generator/generators/components/scaffold/templates/style.css +74 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_edit.rhtml +75 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_list.rhtml +30 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_new.rhtml +8 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_show.rhtml +37 -0
- data/lib/rails_generator/generators/components/scaffold_resource/USAGE +29 -0
- data/lib/rails_generator/generators/components/scaffold_resource/scaffold_resource_generator.rb +92 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/controller.rb +79 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/fixtures.yml +11 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/functional_test.rb +57 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/layout.rhtml +17 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb +13 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/model.rb +2 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/style.css +74 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_edit.rhtml +19 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_index.rhtml +24 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_new.rhtml +18 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_show.rhtml +10 -0
- data/lib/rails_generator/generators/components/session_migration/USAGE +15 -0
- data/lib/rails_generator/generators/components/session_migration/session_migration_generator.rb +18 -0
- data/lib/rails_generator/generators/components/session_migration/templates/migration.rb +16 -0
- data/lib/rails_generator/generators/components/web_service/USAGE +28 -0
- data/lib/rails_generator/generators/components/web_service/templates/api_definition.rb +5 -0
- data/lib/rails_generator/generators/components/web_service/templates/controller.rb +8 -0
- data/lib/rails_generator/generators/components/web_service/templates/functional_test.rb +19 -0
- data/lib/rails_generator/generators/components/web_service/web_service_generator.rb +29 -0
- data/lib/rails_generator/lookup.rb +209 -0
- data/lib/rails_generator/manifest.rb +53 -0
- data/lib/rails_generator/options.rb +143 -0
- data/lib/rails_generator/scripts.rb +83 -0
- data/lib/rails_generator/scripts/destroy.rb +7 -0
- data/lib/rails_generator/scripts/generate.rb +7 -0
- data/lib/rails_generator/scripts/update.rb +12 -0
- data/lib/rails_generator/simple_logger.rb +46 -0
- data/lib/rails_generator/spec.rb +44 -0
- data/lib/railties_path.rb +1 -0
- data/lib/ruby_version_check.rb +17 -0
- data/lib/rubyprof_ext.rb +35 -0
- data/lib/tasks/databases.rake +187 -0
- data/lib/tasks/documentation.rake +82 -0
- data/lib/tasks/framework.rake +112 -0
- data/lib/tasks/log.rake +9 -0
- data/lib/tasks/misc.rake +4 -0
- data/lib/tasks/pre_namespace_aliases.rake +53 -0
- data/lib/tasks/rails.rb +8 -0
- data/lib/tasks/statistics.rake +18 -0
- data/lib/tasks/testing.rake +120 -0
- data/lib/tasks/tmp.rake +37 -0
- data/lib/test_help.rb +19 -0
- data/lib/webrick_server.rb +166 -0
- data/libs/migration_helper.rb +11 -0
- data/migrations/001_create_logins.rb +22 -0
- data/migrations/002_create_admins.rb +16 -0
- data/migrations/003_create_roles.rb +13 -0
- data/migrations/004_create_tables.rb +11 -0
- data/migrations/005_create_attr_types.rb +13 -0
- data/migrations/006_create_table_fields.rb +22 -0
- data/migrations/007_create_tab_operations.rb +13 -0
- data/migrations/008_create_attr_operations.rb +13 -0
- data/migrations/009_create_tab_permissions.rb +21 -0
- data/migrations/010_create_attr_permissions.rb +21 -0
- data/migrations/011_create_relations.rb +23 -0
- data/migrations/012_create_scaffolds.rb +11 -0
- data/migrations/013_create_relation_permissions.rb +20 -0
- data/models/admin.rb +38 -0
- data/models/attr_operation.rb +3 -0
- data/models/attr_permission.rb +5 -0
- data/models/attr_type.rb +3 -0
- data/models/login.rb +51 -0
- data/models/relation.rb +5 -0
- data/models/relation_permission.rb +4 -0
- data/models/role.rb +13 -0
- data/models/scaffold.rb +2 -0
- data/models/tab_operation.rb +3 -0
- data/models/tab_permission.rb +5 -0
- data/models/table.rb +18 -0
- data/models/table_field.rb +6 -0
- data/plugins/redhillonrails_core/CHANGELOG +186 -0
- data/plugins/redhillonrails_core/MIT-LICENSE +20 -0
- data/plugins/redhillonrails_core/README +152 -0
- data/plugins/redhillonrails_core/init.rb +1 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/base.rb +54 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/abstract_adapter.rb +46 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/column.rb +21 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb +26 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/index_definition.rb +11 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/mysql_adapter.rb +94 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/mysql_column.rb +8 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/postgresql_adapter.rb +131 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/schema_statements.rb +23 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/sqlite3_adapter.rb +9 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/table_definition.rb +27 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/schema.rb +25 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/schema_dumper.rb +58 -0
- data/plugins/redhillonrails_core/lib/redhillonrails_core.rb +19 -0
- data/plugins/redhillonrails_core/tasks/db/comments.rake +9 -0
- data/plugins/schema_validations/CHANGELOG +149 -0
- data/plugins/schema_validations/MIT-LICENSE +20 -0
- data/plugins/schema_validations/README +54 -0
- data/plugins/schema_validations/about.yml +5 -0
- data/plugins/schema_validations/init.rb +1 -0
- data/plugins/schema_validations/install.rb +1 -0
- data/plugins/schema_validations/lib/red_hill_consulting/schema_validations/active_record/base.rb +108 -0
- data/plugins/schema_validations/lib/schema_validations.rb +1 -0
- data/plugins/svn/README +69 -0
- data/plugins/svn/Rakefile +22 -0
- data/plugins/svn/assets/javascripts/live_validation.js +4 -0
- data/plugins/svn/assets/stylesheets/live_validation.css +28 -0
- data/plugins/svn/init.rb +2 -0
- data/plugins/svn/install.rb +1 -0
- data/plugins/svn/lib/form_helpers.rb +49 -0
- data/plugins/svn/lib/live_validations.rb +73 -0
- data/plugins/svn/tasks/live_validation_tasks.rake +17 -0
- data/plugins/svn/test/form_helpers_test.rb +214 -0
- data/plugins/svn/test/live_validations_test.rb +124 -0
- data/plugins/svn/test/resource.rb +17 -0
- data/plugins/svn/uninstall.rb +1 -0
- data/stylesheets/live_validation.css +28 -0
- data/stylesheets/print.css +72 -0
- data/stylesheets/scaffold.css +175 -0
- data/views/accounts/index.rhtml +12 -0
- data/views/accounts/show.rhtml +5 -0
- data/views/admin/_form.rhtml +15 -0
- data/views/admin/add_user.rhtml +21 -0
- data/views/admin/change_pwd.rhtml +23 -0
- data/views/admin/edit_user.rhtml +12 -0
- data/views/admin/index.rhtml +19 -0
- data/views/begin/index.rhtml +0 -0
- data/views/layouts/general.rhtml +59 -0
- data/views/layouts/login.rhtml +18 -0
- data/views/login/change_pwd.rhtml +21 -0
- data/views/login/expired_pwd.rhtml +19 -0
- data/views/login/index.rhtml +15 -0
- data/views/permission/index.rhtml +17 -0
- data/views/permission/relations.rhtml +28 -0
- data/views/permission/roles.rhtml +45 -0
- data/views/permission/tables.rhtml +53 -0
- data/views/roles/_form.rhtml +8 -0
- data/views/roles/edit.rhtml +11 -0
- data/views/roles/list.rhtml +28 -0
- data/views/roles/new.rhtml +8 -0
- data/views/roles/show.rhtml +14 -0
- data/views/shared_views/_advanced_search.rhtml +35 -0
- data/views/shared_views/_child_elements.rhtml +30 -0
- data/views/shared_views/_childs.rhtml +20 -0
- data/views/shared_views/_father_elements.rhtml +27 -0
- data/views/shared_views/_fathers.rhtml +21 -0
- data/views/shared_views/_fathers_header.rhtml +58 -0
- data/views/shared_views/_many_to_many_elements.rhtml +50 -0
- data/views/shared_views/_many_to_manys.rhtml +18 -0
- data/views/shared_views/_one_to_one_elements.rhtml +24 -0
- data/views/shared_views/_one_to_one_father_elements.rhtml +26 -0
- data/views/shared_views/_one_to_ones.rhtml +42 -0
- data/views/shared_views/_one_to_ones_header.rhtml +117 -0
- data/views/shared_views/edit_child.rhtml +89 -0
- data/views/shared_views/edit_many.rhtml +75 -0
- data/views/tables/_cascade.rhtml +10 -0
- data/views/tables/_field.rhtml +10 -0
- data/views/tables/_fk.rhtml +9 -0
- data/views/tables/_form.rhtml +9 -0
- data/views/tables/_join_table_name.rhtml +16 -0
- data/views/tables/_relation_types.rhtml +5 -0
- data/views/tables/_tables.rhtml +15 -0
- data/views/tables/attributes.rhtml +15 -0
- data/views/tables/edit.rhtml +11 -0
- data/views/tables/edit_field.rhtml +18 -0
- data/views/tables/list.rhtml +39 -0
- data/views/tables/new.rhtml +8 -0
- data/views/tables/new_field.rhtml +25 -0
- data/views/tables/relations.rhtml +24 -0
- data/views/tables/show.rhtml +24 -0
- metadata +518 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2006 RedHill Consulting, Pty. Ltd.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
= RedHill on Rails Core
|
|
2
|
+
|
|
3
|
+
RedHill on Rails Core is a plugin that features to support other RedHill on Rails plugins. Those features include:
|
|
4
|
+
|
|
5
|
+
* Creating and dropping views;
|
|
6
|
+
* Creating and removing foreign-keys;
|
|
7
|
+
* Obtaining indexes directly from a model class; and
|
|
8
|
+
* Determining when <code>Schema.define()</code> is running.
|
|
9
|
+
|
|
10
|
+
=== View Support
|
|
11
|
+
|
|
12
|
+
The plugin provides a mechanism for creating and dropping views as well as
|
|
13
|
+
preserving views when performing a schema dump:
|
|
14
|
+
|
|
15
|
+
create_view :normal_customers, "SELECT * FROM customers WHERE status = 'normal'"
|
|
16
|
+
drop_view :normal_customers
|
|
17
|
+
|
|
18
|
+
=== Foreign Key Support
|
|
19
|
+
|
|
20
|
+
The plugin provides two mechanisms for adding foreign keys as well as
|
|
21
|
+
preserving foreign keys when performing a schema dump. (Using SQL-92 syntax and
|
|
22
|
+
as such should be compatible with most databases that support foreign-key
|
|
23
|
+
constraints.)
|
|
24
|
+
|
|
25
|
+
The first mechanism for creating foreign-keys allows you to add a foreign key
|
|
26
|
+
when defining a table. For example:
|
|
27
|
+
|
|
28
|
+
create_table :orders do |t|
|
|
29
|
+
...
|
|
30
|
+
t.foreign_key :customer_id, :customers, :id
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
You also have the option of specifying what to do on delete/update using
|
|
34
|
+
<code>:on_delete</code>/<code>:on_update</code>, respectively to one of: <code>:cascade</code>; <code>:restrict</code>; and <code>:set_null</code>:
|
|
35
|
+
|
|
36
|
+
create_table :orders do |t|
|
|
37
|
+
...
|
|
38
|
+
t.foreign_key :customer_id, :customers, :id, :on_delete => :set_null, :on_update => :cascade
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
The second method allows you to create arbitrary foreign-keys at any time:
|
|
42
|
+
|
|
43
|
+
add_foreign_key(:orders, :customer_id, :customers, :id, :on_delete => :set_null, :on_update => :cascade)
|
|
44
|
+
|
|
45
|
+
In either case, if your database supports deferred foreign keys (for example PostgreSQL) you can specify this as well:
|
|
46
|
+
|
|
47
|
+
t.foreign_key :customer_id, :customers, :id, :deferrable => true
|
|
48
|
+
add_foreign_key(:orders, :customer_id, :customers, :id, :deferrable => true)
|
|
49
|
+
|
|
50
|
+
By default, the foreign key will be assigned a name by the underlying database. However, if this doesn't suit
|
|
51
|
+
your needs, you can override the default assignment using the <code>:name</code> option:
|
|
52
|
+
|
|
53
|
+
add_foreign_key(:orders, :customer_id, :customers, :id, :on_delete => :set_null, :on_update => :cascade, <strong>:name => :orders_customer_id_foreign_key<strong>)
|
|
54
|
+
|
|
55
|
+
You can also query the foreign keys for a model yourself by calling <code>foreign_keys()</code>:
|
|
56
|
+
|
|
57
|
+
Order.foreign_keys
|
|
58
|
+
|
|
59
|
+
Or for an arbitrary table by calling <code>foreign_keys(table_name)</code> on a database adapter.
|
|
60
|
+
|
|
61
|
+
Either method returns an array of the following meta-data:
|
|
62
|
+
|
|
63
|
+
* +name+ - The name of the foreign key constraint;
|
|
64
|
+
* +table_name+ - The table for which the foreign-key was generated;
|
|
65
|
+
* +column_names+ - The column names in the table;
|
|
66
|
+
* +references_table_name+ - The table referenced by the foreign-key; and
|
|
67
|
+
* +references_column_names+ - The columns names in the referenced table.
|
|
68
|
+
|
|
69
|
+
If you need to drop a foreign-key, use:
|
|
70
|
+
|
|
71
|
+
remove_foreign_key :orders, :orders_ordered_by_id_fkey
|
|
72
|
+
|
|
73
|
+
The plugin also ensures that all foreign keys are output when performing a
|
|
74
|
+
schema dump. This happens automatically when running <code>rake migrate</code> or
|
|
75
|
+
<code>rake db:schema:dump</code>. This has particular implications when running
|
|
76
|
+
unit tests that contain fixtures. To ensure the test data is correctly reset after
|
|
77
|
+
each test, you should list your fixtures in order of parent->child. For example:
|
|
78
|
+
|
|
79
|
+
fixtures :customers, :products, :orders, :order_lines
|
|
80
|
+
|
|
81
|
+
Rails will then set-up and tear-down the fixtures in the correct sequence.
|
|
82
|
+
|
|
83
|
+
Some databases (PostgreSQL and MySQL for example) allow you to set a comment for a
|
|
84
|
+
table. You can do this for existing tables by using:
|
|
85
|
+
|
|
86
|
+
set_table_comment :orders, "All pending and processed orders"
|
|
87
|
+
|
|
88
|
+
or even at the time of creation:
|
|
89
|
+
|
|
90
|
+
create_table :orders, :comment => "All pending and processed orders" do |t|
|
|
91
|
+
...
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
You can clear table comments using:
|
|
95
|
+
|
|
96
|
+
clear_table_comment :orders
|
|
97
|
+
|
|
98
|
+
There is also a rake tasks to show all database tables and their comments:
|
|
99
|
+
|
|
100
|
+
rake db:comments
|
|
101
|
+
|
|
102
|
+
The plugin fully supports and understands the following active-record
|
|
103
|
+
configuration properties:
|
|
104
|
+
|
|
105
|
+
* <code>config.active_record.pluralize_table_names</code>
|
|
106
|
+
* <code>config.active_record.table_name_prefix</code>
|
|
107
|
+
* <code>config.active_record.table_name_suffix</code>
|
|
108
|
+
|
|
109
|
+
=== Model Indexes
|
|
110
|
+
|
|
111
|
+
ActiveRecord::Base already provides a method on connection for obtaining the
|
|
112
|
+
indexes for a given table. This plugin now makes it possible to obtain the
|
|
113
|
+
indexes for a given model--<code>ActiveRecord::Base</code>--class. For example:
|
|
114
|
+
|
|
115
|
+
Invoice.indexes
|
|
116
|
+
|
|
117
|
+
Would return all the indexes for the +invoices+ table.
|
|
118
|
+
|
|
119
|
+
=== Schema Defining
|
|
120
|
+
|
|
121
|
+
The plugin also adds a method--<code>defining?()</code>--to
|
|
122
|
+
<code>ActiveRecord::Schema</code> to indicate when <code>define()</code> is running. This is necessary
|
|
123
|
+
as some migration plugins must change their behaviour accordingly.
|
|
124
|
+
|
|
125
|
+
=== Case-insensitive Indexes
|
|
126
|
+
|
|
127
|
+
For PostgreSQL, you can add an option <code>:case_sensitive => false</code> to <code>add_index</code>
|
|
128
|
+
which will generate an expression index of the form:
|
|
129
|
+
|
|
130
|
+
LOWER(column_name)
|
|
131
|
+
|
|
132
|
+
This means finder queries of the form:
|
|
133
|
+
|
|
134
|
+
WHERE LOWER(column_name) = LOWER(?)
|
|
135
|
+
|
|
136
|
+
are able to use the indexes rather require, in the worst case, full-table scans.
|
|
137
|
+
|
|
138
|
+
Note also that this ties in well with Rails built-in support for case-insensitive searching:
|
|
139
|
+
|
|
140
|
+
validates_uniqueness_of :name, :case_sensitive => false
|
|
141
|
+
|
|
142
|
+
=== See Also
|
|
143
|
+
|
|
144
|
+
* Foreign Key Associations (foreign_key_associations)
|
|
145
|
+
* Foreign Key Migrations (foreign_key_migrations)
|
|
146
|
+
* Row Version Migrations (row_version_migrations)
|
|
147
|
+
* Schema Validations (schema_validations)
|
|
148
|
+
|
|
149
|
+
=== License
|
|
150
|
+
|
|
151
|
+
This plugin is copyright 2006 by RedHill Consulting, Pty. Ltd. and is released
|
|
152
|
+
under the MIT license.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'redhillonrails_core' unless defined?(RedHillConsulting::Core)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module RedHillConsulting::Core::ActiveRecord
|
|
2
|
+
module Base
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.extend(ClassMethods)
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
module ClassMethods
|
|
8
|
+
def self.extended(base)
|
|
9
|
+
class << base
|
|
10
|
+
alias_method_chain :columns, :redhillonrails_core
|
|
11
|
+
alias_method_chain :abstract_class?, :redhillonrails_core
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def base_class?
|
|
16
|
+
self == base_class
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def abstract_class_with_redhillonrails_core?
|
|
20
|
+
abstract_class_without_redhillonrails_core? || !(name =~ /^Abstract/).nil?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def columns_with_redhillonrails_core
|
|
24
|
+
unless @columns
|
|
25
|
+
columns_without_redhillonrails_core
|
|
26
|
+
cols = columns_hash
|
|
27
|
+
indexes.each do |index|
|
|
28
|
+
column_name = index.columns.reverse.detect { |name| name !~ /_id$/ } || index.columns.last
|
|
29
|
+
column = cols[column_name]
|
|
30
|
+
column.case_sensitive = index.case_sensitive?
|
|
31
|
+
column.unique_scope = index.columns.reject { |name| name == column_name } if index.unique
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
@columns
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def pluralized_table_name(table_name)
|
|
38
|
+
ActiveRecord::Base.pluralize_table_names ? table_name.to_s.pluralize : table_name
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def indexes
|
|
42
|
+
@indexes ||= connection.indexes(table_name, "#{name} Indexes")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def foreign_keys
|
|
46
|
+
@foreign_keys ||= connection.foreign_keys(table_name, "#{name} Foreign Keys")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def reverse_foreign_keys
|
|
50
|
+
connection.reverse_foreign_keys(table_name, "#{name} Reverse Foreign Keys")
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module RedHillConsulting::Core::ActiveRecord::ConnectionAdapters
|
|
2
|
+
module AbstractAdapter
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.module_eval do
|
|
5
|
+
alias_method_chain :drop_table, :redhillonrails_core
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def create_view(view_name, definition)
|
|
10
|
+
execute "CREATE VIEW #{view_name} AS #{definition}"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def drop_view(view_name)
|
|
14
|
+
execute "DROP VIEW #{view_name}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def views(name = nil)
|
|
18
|
+
[]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def view_definition(view_name, name = nil)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def foreign_keys(table_name, name = nil)
|
|
25
|
+
[]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def reverse_foreign_keys(table_name, name = nil)
|
|
29
|
+
[]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def add_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {})
|
|
33
|
+
foreign_key = ForeignKeyDefinition.new(options[:name], table_name, column_names, ActiveRecord::Migrator.proper_table_name(references_table_name), references_column_names, options[:on_update], options[:on_delete], options[:deferrable])
|
|
34
|
+
execute "ALTER TABLE #{table_name} ADD #{foreign_key}"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def remove_foreign_key(table_name, foreign_key_name)
|
|
38
|
+
execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{foreign_key_name}"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def drop_table_with_redhillonrails_core(name, options = {})
|
|
42
|
+
reverse_foreign_keys(name).each { |foreign_key| remove_foreign_key(foreign_key.table_name, foreign_key.name) }
|
|
43
|
+
drop_table_without_redhillonrails_core(name, options)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module RedHillConsulting::Core::ActiveRecord::ConnectionAdapters
|
|
2
|
+
module Column
|
|
3
|
+
attr_accessor :unique_scope
|
|
4
|
+
attr_accessor :case_sensitive
|
|
5
|
+
alias case_sensitive? case_sensitive
|
|
6
|
+
|
|
7
|
+
def unique?
|
|
8
|
+
!unique_scope.nil?
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def required_on
|
|
12
|
+
if null
|
|
13
|
+
nil
|
|
14
|
+
elsif default.nil?
|
|
15
|
+
:save
|
|
16
|
+
else
|
|
17
|
+
:update
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module RedHillConsulting::Core::ActiveRecord::ConnectionAdapters
|
|
2
|
+
class ForeignKeyDefinition < Struct.new(:name, :table_name, :column_names, :references_table_name, :references_column_names, :on_update, :on_delete, :deferrable)
|
|
3
|
+
ACTIONS = { :cascade => "CASCADE", :restrict => "RESTRICT", :set_null => "SET NULL", :set_default => "SET DEFAULT", :no_action => "NO ACTION" }.freeze
|
|
4
|
+
|
|
5
|
+
def to_dump
|
|
6
|
+
dump = "add_foreign_key"
|
|
7
|
+
dump << " #{table_name.inspect}, [#{Array(column_names).collect{ |name| name.inspect }.join(', ')}]"
|
|
8
|
+
dump << ", #{references_table_name.inspect}, [#{Array(references_column_names).collect{ |name| name.inspect }.join(', ')}]"
|
|
9
|
+
dump << ", :on_update => :#{on_update}" if on_update
|
|
10
|
+
dump << ", :on_delete => :#{on_delete}" if on_delete
|
|
11
|
+
dump << ", :deferrable => #{deferrable}" if deferrable
|
|
12
|
+
dump << ", :name => #{name.inspect}" if name
|
|
13
|
+
dump
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def to_sql
|
|
17
|
+
sql = name ? "CONSTRAINT #{name} " : ""
|
|
18
|
+
sql << "FOREIGN KEY (#{Array(column_names).join(", ")}) REFERENCES #{references_table_name} (#{Array(references_column_names).join(", ")})"
|
|
19
|
+
sql << " ON UPDATE #{ACTIONS[on_update]}" if on_update
|
|
20
|
+
sql << " ON DELETE #{ACTIONS[on_delete]}" if on_delete
|
|
21
|
+
sql << " DEFERRABLE" if deferrable
|
|
22
|
+
sql
|
|
23
|
+
end
|
|
24
|
+
alias :to_s :to_sql
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module RedHillConsulting::Core::ActiveRecord::ConnectionAdapters
|
|
2
|
+
module IndexDefinition
|
|
3
|
+
def case_sensitive?
|
|
4
|
+
@case_sensitive.nil? ? true : @case_sensitive
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def case_sensitive=(case_sensitive)
|
|
8
|
+
@case_sensitive = case_sensitive
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
module RedHillConsulting::Core::ActiveRecord::ConnectionAdapters
|
|
2
|
+
module MysqlAdapter
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.class_eval do
|
|
5
|
+
alias_method_chain :remove_column, :redhillonrails_core
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def set_table_comment(table_name, comment)
|
|
10
|
+
execute "ALTER TABLE #{table_name} COMMENT='#{quote_string(comment)}'"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def clear_table_comment(table_name)
|
|
14
|
+
execute "ALTER TABLE #{table_name} COMMENT=''"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def remove_foreign_key(table_name, foreign_key_name)
|
|
18
|
+
execute "ALTER TABLE #{table_name} DROP FOREIGN KEY #{foreign_key_name}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def remove_column_with_redhillonrails_core(table_name, column_name)
|
|
22
|
+
foreign_keys(table_name).select { |foreign_key| foreign_key.column_names.include?(column_name.to_s) }.each do |foreign_key|
|
|
23
|
+
remove_foreign_key(table_name, foreign_key.name)
|
|
24
|
+
end
|
|
25
|
+
remove_column_without_redhillonrails_core(table_name, column_name)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def foreign_keys(table_name, name = nil)
|
|
29
|
+
results = execute("SHOW CREATE TABLE `#{table_name}`", name)
|
|
30
|
+
|
|
31
|
+
foreign_keys = []
|
|
32
|
+
|
|
33
|
+
results.each do |row|
|
|
34
|
+
row[1].each do |line|
|
|
35
|
+
if line =~ /^ CONSTRAINT [`"](.+?)[`"] FOREIGN KEY \([`"](.+?)[`"]\) REFERENCES [`"](.+?)[`"] \((.+?)\)( ON DELETE (.+?))?( ON UPDATE (.+?))?,?$/
|
|
36
|
+
name = $1
|
|
37
|
+
column_names = $2
|
|
38
|
+
references_table_name = $3
|
|
39
|
+
references_column_names = $4
|
|
40
|
+
on_update = $8
|
|
41
|
+
on_delete = $6
|
|
42
|
+
on_update = on_update.downcase.gsub(' ', '_').to_sym if on_update
|
|
43
|
+
on_delete = on_delete.downcase.gsub(' ', '_').to_sym if on_delete
|
|
44
|
+
|
|
45
|
+
foreign_keys << ForeignKeyDefinition.new(name,
|
|
46
|
+
table_name, column_names.gsub('`', '').split(', '),
|
|
47
|
+
references_table_name, references_column_names.gsub('`', '').split(', '),
|
|
48
|
+
on_update, on_delete)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
foreign_keys
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def reverse_foreign_keys(table_name, name = nil)
|
|
57
|
+
@@schema ||= nil
|
|
58
|
+
@@schema_version ||= 0
|
|
59
|
+
temp = execute("select version from schema_info limit 1", name);
|
|
60
|
+
current_version = 0
|
|
61
|
+
temp.each { | row | current_version = row[0] }
|
|
62
|
+
if @@schema.nil? || @@schema_version != current_version
|
|
63
|
+
@@schema_version = current_version
|
|
64
|
+
ans = execute(<<-SQL, name)
|
|
65
|
+
SELECT constraint_name, table_name, column_name, referenced_table_name, referenced_column_name
|
|
66
|
+
FROM information_schema.key_column_usage
|
|
67
|
+
WHERE table_schema = SCHEMA()
|
|
68
|
+
AND referenced_table_schema = table_schema
|
|
69
|
+
ORDER BY constraint_name, ordinal_position;
|
|
70
|
+
SQL
|
|
71
|
+
@@schema = []
|
|
72
|
+
ans.each do | row |
|
|
73
|
+
@@schema << [row[0], row[1], row[2], row[3], row[4]]
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
results = @@schema
|
|
77
|
+
current_foreign_key = nil
|
|
78
|
+
foreign_keys = []
|
|
79
|
+
|
|
80
|
+
results.each do |row|
|
|
81
|
+
next if row[3] != table_name
|
|
82
|
+
if current_foreign_key != row[0]
|
|
83
|
+
foreign_keys << ForeignKeyDefinition.new(row[0], row[1], [], row[3], [])
|
|
84
|
+
current_foreign_key = row[0]
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
foreign_keys.last.column_names << row[2]
|
|
88
|
+
foreign_keys.last.references_column_names << row[4]
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
foreign_keys
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
module RedHillConsulting::Core::ActiveRecord::ConnectionAdapters
|
|
2
|
+
module PostgresqlAdapter
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.class_eval do
|
|
5
|
+
alias_method_chain :indexes, :redhillonrails_core
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def set_table_comment(table_name, comment)
|
|
10
|
+
execute "COMMENT ON TABLE #{table_name} IS '#{quote_string(comment)}'"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def clear_table_comment(table_name)
|
|
14
|
+
execute "COMMENT ON TABLE #{table_name} IS NULL"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def add_index(table_name, column_name, options = {})
|
|
18
|
+
column_names = Array(column_name)
|
|
19
|
+
index_name = index_name(table_name, :column => column_names)
|
|
20
|
+
|
|
21
|
+
if Hash === options # legacy support, since this param was a string
|
|
22
|
+
index_type = options[:unique] ? "UNIQUE" : ""
|
|
23
|
+
index_name = options[:name] || index_name
|
|
24
|
+
else
|
|
25
|
+
index_type = options
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
quoted_column_names = column_names.map { |e| options[:case_sensitive] == false && e.to_s !~ /_id$/ ? "LOWER(#{quote_column_name(e)})" : quote_column_name(e) }
|
|
29
|
+
|
|
30
|
+
execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{table_name} (#{quoted_column_names.join(", ")})"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def indexes_with_redhillonrails_core(table_name, name = nil)
|
|
34
|
+
indexes = indexes_without_redhillonrails_core(table_name, name)
|
|
35
|
+
result = query(<<-SQL, name)
|
|
36
|
+
SELECT c2.relname, i.indisunique, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)
|
|
37
|
+
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
|
|
38
|
+
WHERE c.relname = '#{table_name}'
|
|
39
|
+
AND c.oid = i.indrelid AND i.indexrelid = c2.oid
|
|
40
|
+
AND i.indisprimary = 'f'
|
|
41
|
+
AND i.indexprs IS NOT NULL
|
|
42
|
+
ORDER BY 1
|
|
43
|
+
SQL
|
|
44
|
+
|
|
45
|
+
result.each do |row|
|
|
46
|
+
if row[2]=~ /\((.*LOWER\([^:]+(::text)?\).*)\)$/i
|
|
47
|
+
indexes.delete_if { |index| index.name == row[0] }
|
|
48
|
+
column_names = $1.split(", ").map do |name|
|
|
49
|
+
name = $1 if name =~ /^LOWER\(([^:]+)(::text)?\)$/i
|
|
50
|
+
name = $1 if name =~ /^"(.*)"$/
|
|
51
|
+
name
|
|
52
|
+
end
|
|
53
|
+
index = ActiveRecord::ConnectionAdapters::IndexDefinition.new(table_name, row[0], row[1] == "t", column_names)
|
|
54
|
+
index.case_sensitive = false
|
|
55
|
+
indexes << index
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
indexes
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def foreign_keys(table_name, name = nil)
|
|
63
|
+
load_foreign_keys(<<-SQL, name)
|
|
64
|
+
SELECT f.conname, pg_get_constraintdef(f.oid), t.relname
|
|
65
|
+
FROM pg_class t, pg_constraint f
|
|
66
|
+
WHERE f.conrelid = t.oid
|
|
67
|
+
AND f.contype = 'f'
|
|
68
|
+
AND t.relname = '#{table_name}'
|
|
69
|
+
SQL
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def reverse_foreign_keys(table_name, name = nil)
|
|
73
|
+
load_foreign_keys(<<-SQL, name)
|
|
74
|
+
SELECT f.conname, pg_get_constraintdef(f.oid), t2.relname
|
|
75
|
+
FROM pg_class t, pg_class t2, pg_constraint f
|
|
76
|
+
WHERE f.confrelid = t.oid
|
|
77
|
+
AND f.conrelid = t2.oid
|
|
78
|
+
AND f.contype = 'f'
|
|
79
|
+
AND t.relname = '#{table_name}'
|
|
80
|
+
SQL
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def views(name = nil)
|
|
84
|
+
schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
|
|
85
|
+
query(<<-SQL, name).map { |row| row[0] }
|
|
86
|
+
SELECT viewname
|
|
87
|
+
FROM pg_views
|
|
88
|
+
WHERE schemaname IN (#{schemas})
|
|
89
|
+
SQL
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def view_definition(view_name, name = nil)
|
|
93
|
+
result = query(<<-SQL, name)
|
|
94
|
+
SELECT pg_get_viewdef(oid)
|
|
95
|
+
FROM pg_class
|
|
96
|
+
WHERE relkind = 'v'
|
|
97
|
+
AND relname = '#{view_name}'
|
|
98
|
+
SQL
|
|
99
|
+
row = result.first
|
|
100
|
+
row.first unless row.nil?
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
private
|
|
104
|
+
|
|
105
|
+
def load_foreign_keys(sql, name = nil)
|
|
106
|
+
foreign_keys = []
|
|
107
|
+
|
|
108
|
+
query(sql, name).each do |row|
|
|
109
|
+
if row[1] =~ /^FOREIGN KEY \((.+?)\) REFERENCES (.+?)\((.+?)\)( ON UPDATE (.+?))?( ON DELETE (.+?))?( (DEFERRABLE|NOT DEFERRABLE))?$/
|
|
110
|
+
name = row[0]
|
|
111
|
+
from_table_name = row[2]
|
|
112
|
+
column_names = $1
|
|
113
|
+
references_table_name = $2
|
|
114
|
+
references_column_names = $3
|
|
115
|
+
on_update = $5
|
|
116
|
+
on_delete = $7
|
|
117
|
+
deferrable = $9 == "DEFERRABLE"
|
|
118
|
+
on_update = on_update.downcase.gsub(' ', '_').to_sym if on_update
|
|
119
|
+
on_delete = on_delete.downcase.gsub(' ', '_').to_sym if on_delete
|
|
120
|
+
|
|
121
|
+
foreign_keys << ForeignKeyDefinition.new(name,
|
|
122
|
+
from_table_name, column_names.split(', '),
|
|
123
|
+
references_table_name.sub(/^"(.*)"$/, '\1'), references_column_names.split(', '),
|
|
124
|
+
on_update, on_delete, deferrable)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
foreign_keys
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|