How to add PostgreSQL comments on objects with rails/rake? - rake

I would like to add comments on objects such as database itself, tables, etc while using rake. For example, when I issue rake db:create I'd like to automagically add a comment via PostgreSQL extended SQL statement (if the adapter doesn't support this directly), like
COMMENT ON DATABASE myapp_development IS 'Rails DB for project at c:/some/path/myapp/';
I'd like a similar behavior for ActiveRecord::Migration, if possible (by extracting RDoc?) for tables and columns.
It occurs to me that this may be more like a feature request rather than something that could be done quickly. Is this already possible to do, and how can I do it?

You should be able to just execute the raw SQL where you need it. Try this:
sql = "COMMENT ON DATABASE myapp_development IS 'Rails DB for project at c:/some/path/myapp/';"
records_array = ActiveRecord::Base.connection.execute(sql)
Note that you can use string interpolation (e.g. "#{value}") to include variable values if you need. You have all the expressive power of Ruby and the ability to run any native SQL that you need.
The MigrationComments gem gives inline support for comments on migration objects. To install it, simply add this to your Gemfile:
gem 'migration_comments'
Once installed, you can include both inline comments:
def self.up
create_table :users, comment: "User records are stored in this table." do |t|
t.string :canonical_email, comment: "This column contains the email address that has been URL decoded and lowercased, to ensure system-wide uniqueness"
end
end
and standalone comment changes:
def self.up
change_table :users do |t|
t.comment "User records are stored in this table."
t.change_comment :canonical_email, "This column contains the email address that has been URL decoded and lowercased, to ensure system-wide uniqueness"
end
end
You can find the PgComment gem that will give you commenting with an ActiveRecord-like syntax. Simply add this to your Gemfile:
gem 'pg_comment'
And you'll be able to do cool things like this:
set_table_comment :users, "User records are stored in this table."
set_column_comment(:users, :canonical_email, "This column contains the email address that has been URL decoded and lowercased, to ensure system-wide uniqueness")
Unfortunately, there's no set_database_comment with either gem, so database comments (and other unsupported Postgres objects) would require use of the raw SQL solution shown at the top of the answer.

It turned out it is easier than I thought. One can create ./lib/tasks/annotate_db.rake, e.g., from Rails template file, with the following content
namespace :db do
def update_db_comment
Rails.configuration.database_configuration.each do |key, hash|
db = hash['database']
if not db.nil?
sql = "COMMENT ON DATABASE #{db} IS 'Rails #{Rails::VERSION::STRING} #{key} DB for project at #{Rails.root} on #{Socket.gethostbyname(Socket.gethostname).first}';"
# puts sql
records_array = ActiveRecord::Base.connection.execute(sql)
end
end
end
task :create do
update_db_comment
end
end
# Rake::Task['db:create'].enhance(['db:set_db_comment'])

Related

Object reference exception when importing content in Episerver

We are using Optimizely/Episerver CMS 11.20. When trying to export a page hierarchy from our production environment and then import the resulting ExportedFile.episerverdata file to our acceptance test environment I get the following error:
[Importing content 70725_133679] Exception: Object reference not set to an instance of an object.
I have no idea what 70725_133679 is referring to. Is there a way to look this up, e.g. via an SQL query against the "EPi" database?
It refers to a specific version of some content (which could be just about anything).
You could try to browse to https://yoursite/EPiServer/CMS/#context=epi.cms.contentdata:///70725_133679 (note the ID at the end) to see which content it is.
Got another answer on a Optimizely forum (thanks to Surjit Bharath):
The following SQL against the EPi database gives information about the referenced content:
select * from tblContent where pkID = 70725 
select * from tblWorkContent where pkID = 133679
This too points to a submit button. I have yet to understand why that block would cause an exception during import, but now I at least have a place to start digging.

remove databases with non utf-8 characters in mongodb

Good afternoon
How can I remove a database with a non utf-8 character?
See in attach show the database is showed by show dbs command
Thanks for the attention
Regardsenter image description here
Alexandre Bunn
Usually you would do
use [db];
db.dropDatabase();
What the use command actually do is to put the database object to db. With UTF-8 database names you may find it impossible to run the use command. As an alternative way to get the db object, you can run db.getSisterDB([db name]) then drop it.
So this should do the trick:
var db2 = db.getSisterDB(emptyDbs[0].name);
db2.dropDatabase();
There's another method getSiblingDB which returns the same db object. If you are wondering what's the difference between them, well, no difference.
replset:PRIMARY> db.getSiblingDB
function (name) {
return this.getMongo().getDB(name);
}
replset:PRIMARY> db.getSisterDB
function (name) {
return this.getMongo().getDB(name);
}
EDIT:
As the solution above doesn't work for you. I think the last thing you can try is to go to dbpath, find the files named by the UTF-8 characters and delete them. You probably won't file any file under that name. Because when you see (empty) follow a db name, it usually means the db is deleted. If so, just restart the instance would fix the problem.
Remember to stop the database before doing this. And do backup before doing anything.
This way only works for MMAPv1 storage engine.

how to change mysql_query to mysqli_guery

I am using in a script mysqli_query but i think there is something wrong with the syntaxes. The script did work before, but changing everything form mysql_query to mysqli_query, data is not put anymore correct in the database.
This was the original query:
mysql_query( "INSERT INTO download_manager (SET filename='".mysqli_real_escape_string($_GET['file'])."'),
ON DUPLICATE KEY UPDATE downloads=downloads+1");
I changed it with mysqli_query this way:
mysqli_query($link, "INSERT INTO download_manager (SET filename='".mysqli_real_escape_string($_GET['file'])."'),
ON DUPLICATE KEY UPDATE downloads=downloads+1");
Can someone tell me what i did wrong?
Update:
My connection looks like this:
$link = #mysqli_connect($db_host, $db_user, $db_pass, $db_database) or die ("Error " . mysqli_error($link));
mysql_set_charset('utf8');
Ensure that the handle stored in $link was generated from a call to mysqli_connect and not mysql_connect:
http://php.net/manual/en/function.mysqli-connect.php
If that fails, check for an error:
die(mysqli_error($link))
Troubleshooting
The mysql and mysqli PHP modules are distinct, and you can't mix functions between the modules.
Enable error reporting to ensure you are able to see useful error messages.
Ensure the database credentials and connection details are valid.
Use mysqli_error after mysqli_xxx function calls to detect issues within the mysqli module.
Use the # token sparingly to avoid hiding useful errors.

typo3: issue with changing file:ext_tables_static+adt.sql

In file: ext/quick/ext_tables_static+adt.sql
...
INSERT INTO `tx_quick_string` (`name`, `vlaue`) VALUES
('catalog', 'this is group one');
...
I want to change the value to 'this is group 1'. This is what I did:
a.changed it in this file:ext/quick/ext_tables_static+adt.sql
b.changed it in typo3/phpmyadmin->table tx_quick_string
It seems work. But I have some questions:
1.Does this file ext_tables_static+adt.sql only take effect when install/uninstall extension?
2.Is there anything else I need to do after I changed the value in ext_tables_static+adt.sql and typo3/phpmyadmin->table tx_quick_string? Do I need to update the extension in EM?
Yes, only when you install the extension.
No, if you already did the db update manually, there is nothing more to do.
If the extension you mentioned is not maintained by you, the files may get reverted during the next update of this extension, keep that in mind!
Apart from the things you asked I strongly advise you to NOT use the phpmyadmin extension of TYPO3. It has security problems on a kind of regular basis. So the better and more secure solution is to either use the db management tool your hosting company provides (they will keep it running and updated) or to put a separate tool somewhere else and secure it with a password (like htaccess restriction or something).

Catalyst Not Accepting DBIx Generated Schema

I am using Catalyst::Plugin::AutoCRUD and am generating a DBIx schema using the instructions provided in the linked CPAN page. Specifically, I copy/pasted the command listed there and changed only the details relevant to my database ('pg' => 'mysql', different username/pw, etc).
I now have a schema DBIC::Database::foo::Schema. Schema is both a file containing *.pm's for each table in my DB and also it's own Schema.pm.
My config file contains the following entry:
<Model::AutoCRUD::DBIC>
schema_class Database::foo::Schema
connect_info dbi:mysql:dbname=foo
connect_info user
connect_info pass
<connect_info>
AutoCommit 1
</connect_info>
</Model::AutoCRUD::DBIC>
When I go to start the AutoCRUD server, I get the following error message:
Couldn't instantiate component "DemoApp::Model::AutoCRUD::DBIC", "Attribute (schema_class)
does not pass the type constraint because: Validation failed for
'Catalyst::Model::DBIC::Schema::Types::SchemaClass' with value Database::foo::Schema at
/Library/Perl/5.12/darwin-thread-multi-2level/Moose/Meta/Attribute.pm line 1275.
As I am new to Catalyst and this plugin, I don't know how to resolve this issue. Google has not been very helpful - I found this discussion, but from what I can tell the issue was that Catalyst was being pointed towards the wrong *.pm (although I could be misreading this).
In case this is helpful, here are the contents of Schema.pm:
use utf8;
package DBIC::Database::foo::Schema;
# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE
use strict;
use warnings;
use base 'DBIx::Class::Schema';
__PACKAGE__->load_namespaces;
# Created by DBIx::Class::Schema::Loader v0.07024 # 2012-05-20 07:25:21
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:cevz/k4rUWIcEhMl29r0QA
# You can replace this text with custom code or comments, and it will be preserved on regeneration
1;
Please help!
Your schema is named DBIC::Database::Foo::Schema but in the config file you have Database::foo::Schema. The names are case sensitive so either change the name of your Schema path and files or correct the config.
Completely rebuilding the DBIC classes from the Catalyst manual solved the problem. While I cannot pinpoint what was unacceptable to Moose in the first set of classes, the second set of classes had one additional problem: the line __PACKAGE__->meta->make_immutable; was generated for every class (i.e. in each *.pm). Commenting it out and restarting Catalyst yielded a functioning CRUD app.